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.

63 lines
2.1 KiB

2 years ago
package main
import (
2 years ago
common "git.mashibing.com/msb_47094/shopping-comm"
2 years ago
consul "github.com/asim/go-micro/plugins/registry/consul/v4"
opentracing2 "github.com/go-micro/plugins/v4/wrapper/trace/opentracing"
"github.com/opentracing/opentracing-go"
"go-micro.dev/v4"
"go-micro.dev/v4/registry"
2 years ago
"goproduct/domain/repository"
"goproduct/domain/service"
"goproduct/handler"
"goproduct/proto"
"log"
"time"
)
func main() {
//0 配置中心
consulConfig, err := common.GetConsulConfig(common.ConsulStr, common.ProductFileKey)
2 years ago
if err != nil {
log.Println("consulConfig err :", err)
}
// 1.consul注册中心
consulReist := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{common.ConsulReistStr}
2 years ago
})
2 years ago
//链路追踪实列化 注意addr是 jaeper地址 端口号6831
t, io, err := common.NewTracer("shop-product", common.ConsulIp+":6831")
2 years ago
if err != nil {
log.Fatal(err)
}
defer io.Close()
//设置全局的Tracing
opentracing.SetGlobalTracer(t)
2 years ago
repcService := micro.NewService(
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*30),
2 years ago
micro.Name("shop-product"),
micro.Address(":8082"),
2 years ago
micro.Version("v1"),
micro.Registry(consulReist),
2 years ago
//引包
micro.WrapHandler(opentracing2.NewHandlerWrapper(opentracing.GlobalTracer())),
2 years ago
)
//2.初始化db
db, _ := common.GetMysqlFromConsul(consulConfig)
//3.创建服务实例
2 years ago
productDataService := service.NewProductDataService(repository.NewProductRepository(db))
2 years ago
//4.注册handler
2 years ago
proto.RegisterPageHandler(repcService.Server(), &handler.ProductHandler{productDataService})
proto.RegisterShowProductDetailHandler(repcService.Server(), &handler.ProductHandler{productDataService})
proto.RegisterShowProductSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
proto.RegisterShowDetailSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
2 years ago
proto.RegisterUpdateSkuHandler(repcService.Server(), &handler.ProductHandler{productDataService})
2 years ago
//5.启动服务
if err := repcService.Run(); err != nil {
log.Println("start user service err :", err)
}
}