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