You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

2 years ago
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 (
2 years ago
consulStr = "http://192.168.100.131:8500"
consulReistStr = "192.168.100.131:8500"
fileKey = "mysql-user"
2 years ago
)
func main() {
//0 配置中心
consulConfig, err := common.GetConsulConfig(consulStr, fileKey)
if err != nil {
log.Println("consulConfig err :", err)
}
// 1.consul注册中心
2 years ago
//注册到consul
consulReg := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{consulReistStr}
2 years ago
})
repcService := micro.NewService(
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*30),
micro.Name("shop-user"),
micro.Address(":8081"),
micro.Version("v1"),
2 years ago
micro.Registry(consulReg),
2 years ago
)
//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)
}
}