-
需要的包
go get github.com/afex/hystrix-go/hystrix
-
需要先进行包装
// go-micro main.go 里面 New Service 添加上熔断器即可 service := micro.NewService( micro.Name("go.micro.api.cartApi"), micro.Version("latest"), micro.Address(":8085"), //添加consul注册中心 micro.Registry(consul), //添加链路追踪 micro.WrapClient(opentracing2.NewClientWrapper(opentracing.GlobalTracer())), //添加熔断 micro.WrapClient(NewClientHystrixWrapper()), ) //下面对熔断器包装 type clientWrapper struct { client.Client } func (c *clientWrapper) Call(ctx context.Context,req client.Request,rsp interface{},opts ...client.CallOption) error { return hystrix.Do(req.Service()+"."+req.Endpoint(), func() error { //run 正常执行逻辑 fmt.Println("req.Service()+req.Endpoint()值:") fmt.Println(req.Service()+"."+req.Endpoint()) return c.Client.Call(ctx,req,rsp,opts ...) }, func(err error) error { // fmt.Println(err) return err }) } func NewClientHystrixWrapper() client.Wrapper { return func(i client.Client) client.Client { return &clientWrapper{i} } }
原创文章,作者:星辰,如若转载,请注明出处:http://www.z88j.com/39297.html