package gomicro import ( "context" "github.com/afex/hystrix-go/hystrix" "github.com/micro/go-micro/v2/client" ) type HystrixWrapper struct { client.Client fuseResponse } type fuseResponse func(rsp interface{}) error func NewHystrixWrapper(c client.Client) client.Client { return &HystrixWrapper{Client: c} } func (c *HystrixWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { name := req.Service() + "." + req.Endpoint() hystrix.ConfigureCommand(name, hystrix.CommandConfig{ Timeout: 5000, // 超时时间(毫秒) MaxConcurrentRequests: 1000, ErrorPercentThreshold: 25, SleepWindow: 5000, }) return hystrix.Do(name, func() error { return c.Client.Call(ctx, req, rsp) }, func(e error) error { if c.fuseResponse != nil { return c.fuseResponse(rsp) } return nil }) } /*func SendCircuitMetrics() { hystrixStreamHandler := hystrix.NewStreamHandler() hystrixStreamHandler.Start() go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler) c, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{ StatsdAddr: "localhost:8125", Prefix: "myapp.hystrix", }) if err != nil { log.Fatalf("could not initialize statsd client: %v", err) } metricCollector.Registry.Register(c.NewStatsdCollector) }*/