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.
53 lines
1.2 KiB
53 lines
1.2 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 (
|
||
|
consulStr = "http://192.168.137.131:8500"
|
||
|
fileKey = "mysql-user"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
//0 配置中心
|
||
|
consulConfig, err := common.GetConsulConfig(consulStr, fileKey)
|
||
|
if err != nil {
|
||
|
log.Println("consulConfig err :", err)
|
||
|
}
|
||
|
// 1.consul注册中心
|
||
|
consulReist := consul.NewRegistry(func(options *registry.Options) {
|
||
|
options.Addrs = []string{consulStr}
|
||
|
})
|
||
|
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(consulReist),
|
||
|
)
|
||
|
//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)
|
||
|
}
|
||
|
|
||
|
}
|