package main import ( "gouser/common" "gouser/domain/repository" "gouser/domain/service" "gouser/handler" "gouser/proto" "log" "time" consul "github.com/asim/go-micro/plugins/registry/consul/v4" "go-micro.dev/v4" "go-micro.dev/v4/registry" ) const ( consulStr = "http://192.168.100.131:8500" consulReistStr = "192.168.100.131:8500" fileKey = "mysql-user" ) func main() { //0 配置中心 consulConfig, err := common.GetConsulConfig(consulStr, fileKey) if err != nil { log.Println("consulConfig err :", err) } // 1.consul注册中心 //注册到consul consulReg := consul.NewRegistry(func(options *registry.Options) { options.Addrs = []string{consulReistStr} }) repcService := micro.NewService( micro.RegisterTTL(time.Second*30), micro.RegisterInterval(time.Second*30), micro.Name("shop-user"), micro.Address(":8081"), micro.Version("v1"), micro.Registry(consulReg), ) //2.初始化db db, _ := common.GetMysqlFromConsul(consulConfig) //3.创建服务实例 userDataService := service.NewUserDataService(repository.NewUserRepository(db)) //4.注册handler proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService}) //5.启动服务 if err := repcService.Run(); err != nil { log.Println("start user service err :", err) } }