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.

56 lines
1.3 KiB

2 years ago
  1. package gomicro
  2. import (
  3. "context"
  4. "github.com/afex/hystrix-go/hystrix"
  5. "github.com/micro/go-micro/v2/client"
  6. )
  7. type HystrixWrapper struct {
  8. client.Client
  9. fuseResponse
  10. }
  11. type fuseResponse func(rsp interface{}) error
  12. func NewHystrixWrapper(c client.Client) client.Client {
  13. return &HystrixWrapper{Client: c}
  14. }
  15. func (c *HystrixWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
  16. name := req.Service() + "." + req.Endpoint()
  17. hystrix.ConfigureCommand(name,
  18. hystrix.CommandConfig{
  19. Timeout: 5000, // 超时时间(毫秒)
  20. MaxConcurrentRequests: 1000,
  21. ErrorPercentThreshold: 25,
  22. SleepWindow: 5000,
  23. })
  24. return hystrix.Do(name, func() error {
  25. return c.Client.Call(ctx, req, rsp)
  26. }, func(e error) error {
  27. if c.fuseResponse != nil {
  28. return c.fuseResponse(rsp)
  29. }
  30. return nil
  31. })
  32. }
  33. /*func SendCircuitMetrics() {
  34. hystrixStreamHandler := hystrix.NewStreamHandler()
  35. hystrixStreamHandler.Start()
  36. go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
  37. c, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{
  38. StatsdAddr: "localhost:8125",
  39. Prefix: "myapp.hystrix",
  40. })
  41. if err != nil {
  42. log.Fatalf("could not initialize statsd client: %v", err)
  43. }
  44. metricCollector.Registry.Register(c.NewStatsdCollector)
  45. }*/