main.go 1.01 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
package main

import (
	"flag"
	"fmt"
	"go-micro.dev/v4/logger"
	"golang.org/x/sync/errgroup"
	"gomicro-base/common/logz"
	"gomicro-base/service/demo/internal/conf"
	"gomicro-base/service/demo/internal/dao"
	"gomicro-base/service/demo/internal/server"
	"gomicro-base/service/demo/internal/service"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	flag.Parse()

	conf.LoadConfig()

	if conf.Conf.Server.Name == "" {
		panic("服务名不能为空")
	}

	// github.com/go-micro/plugins/v4/logger/zap
	logger.DefaultLogger = logz.NewLogger(conf.Conf.Log)
	defer logz.Flush()

	svc := service.New(dao.New(conf.Conf))
	microServer := server.NewMicroServer(conf.Conf, svc)

	eg := errgroup.Group{}
	eg.Go(func() error {
		return microServer.Run()
	})

	eg.Go(func() error {
		ch := make(chan os.Signal, 1)
		signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
		v := <-ch
		fmt.Println()
		logz.Warnf("捕获退出信号: %v", v)
		return nil
	})

	if err := eg.Wait(); err != nil {
		logz.Error(err)
	}

}