|
|
|
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"
|
|
|
|
redisKey = "redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 & redis
|
|
|
|
//mysql
|
|
|
|
db, _ := common.GetMysqlFromConsul(consulConfig)
|
|
|
|
//redis
|
|
|
|
consulRedisConfig, err := common.GetConsulConfig(consulStr, redisKey)
|
|
|
|
red, _ := common.GetRedisFromConsul(consulRedisConfig)
|
|
|
|
//测试 缓存
|
|
|
|
//common.SetUserToken(red, "uuid1111", []byte("tokenxxx"), time.Duration(1)*time.Hour)
|
|
|
|
//val := common.GetUserToken(red, "uuid1111")
|
|
|
|
//fmt.Println(">>>>>>>>>>>>>>>>>>>>> ", val)
|
|
|
|
//3.创建服务实例
|
|
|
|
userDataService := service.NewUserDataService(repository.NewUserRepository(db, red))
|
|
|
|
//4.注册handler
|
|
|
|
proto.RegisterLoginHandler(repcService.Server(), &handler.User{userDataService})
|
|
|
|
proto.RegisterGetUserTokenHandler(repcService.Server(), &handler.User{userDataService})
|
|
|
|
//5.启动服务
|
|
|
|
if err := repcService.Run(); err != nil {
|
|
|
|
log.Println("start user service err :", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|