package main import ( 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" "goproduct/common" "goproduct/domain/repository" "goproduct/domain/service" "goproduct/handler" "goproduct/proto" "log" "time" ) const ( consulStr = "http://192.168.100.131:8500" consulReistStr = "192.168.100.131:8500" fileKey = "mysql-product" QPS = 100 ) 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{consulReistStr} }) //链路追踪实列化 注意addr是 jaeper地址 端口号6831 t, io, err := common.NewTracer("shop-cart", "192.168.100.131: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(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}) //5.启动服务 if err := repcService.Run(); err != nil { log.Println("start cart service err :", err) } }