【GO-Micro】hystrix 熔断及 dashboard 展示

本贴最后更新于 2105 天前,其中的信息可能已经渤澥桑田

github 完整代码地址

hystrix-go

hystrix 是 Netflix 开源的一个 JAVA 项目,不过 GitHub 也有 golang 的实现版本 hystrix-go

hystrix-dashboard

hystrix 并没有自带一个仪表盘,无法直观的查看接口的健康状况。所以,我们采用 GitHub 的一个开源实现 hystrix-dashboard。

docker run --name hystrix-dashboard -d -p 8081:9002 mlabouardy/hystrix-dashboard:latest

micro API 网关插件

关于 hystrix 的工作原理,可以查阅相关资料,这里只讲解如何封装插件在 micro API 网关中使用。

err := hystrix.Do("my_command", func() error { // talk to other services return nil }, nil)

使用 hystrix.Do() 同步 API,第一个参数是 command, 应该是与当前请求一一对应的一个名称,如入“GET-/test”。第二个参数传入一个函数,函数包含我我们自己的错误逻辑,当请求失败时应该返回 error。hystrix 会根据我们的失败率执行熔断策略。

封装 Handler

// BreakerWrapper hystrix breaker func BreakerWrapper(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { name := r.Method + "-" + r.RequestURI log.Println(name) err := hystrix.Do(name, func() error { sct := &status_code.StatusCodeTracker{ResponseWriter: w, Status: http.StatusOK} h.ServeHTTP(sct.WrappedResponseWriter(), r) if sct.Status >= http.StatusBadRequest { str := fmt.Sprintf("status code %d", sct.Status) log.Println(str) return errors.New(str) } return nil }, nil) if err != nil { log.Println("hystrix breaker err: ", err) return } }) } ... // 注册插件 plugin.Register(plugin.NewPlugin( plugin.WithName("breaker"), plugin.WithHandler( hystrix.BreakerWrapper, ), )) ...

在 hystrix.Do 中,首先执行 h.ServeHTTP,该函数返回后,即请求执行完成。我们判断 HTTP 状态码,如果大于 StatusBadRequest,则认为这次请求失败,返回一个错误,hystrix 会收集错误,如果错误率达到某个阀值,就会触发断路器。
在做实验时,可以直接在 main 函数里设置 hystrix 的几个默认配置参数,方便看效果

// hystrix-go/hystrix/settings.go // DefaultTimeout is how long to wait for command to complete, in milliseconds DefaultTimeout = 1000 // DefaultMaxConcurrent is how many commands of the same type can run at the same time DefaultMaxConcurrent = 10 // DefaultVolumeThreshold is the minimum number of requests needed before a circuit can be tripped due to health DefaultVolumeThreshold = 20 // DefaultSleepWindow is how long, in milliseconds, to wait after a circuit opens before testing for recovery DefaultSleepWindow = 5000 // DefaultErrorPercentThreshold causes circuits to open once the rolling measure of errors exceeds this percent of requests DefaultErrorPercentThreshold = 50

hystrix-go 库还提供为每个 commond 动态设置配置的接口,我们可以通过这个接口结合配置中心,动态调节服务。

hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{ Timeout: 1000, MaxConcurrentRequests: 100, ErrorPercentThreshold: 25, })

接入 hystrix-dashboard

docker run --name hystrix-dashboard -d -p 8081:9002 mlabouardy/hystrix-dashboard:latest

打开 http://localhost:8081/hystrix , 输入 http://{ip}:81/hystrix.stream , 此处 ip 为本机 ip,因为 hystrix-dashboard 是容器启动的,无法直接访问本机 127.0.0.1。

Enable dashboard metrics
In your main.go, register the event stream HTTP handler on a port and launch it in a goroutine. Once you configure turbine for your Hystrix Dashboard to start streaming events, your commands will automatically begin appearing.

hystrixStreamHandler := hystrix.NewStreamHandler() hystrixStreamHandler.Start() go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)

image.png

  • golang

    Go 语言是 Google 推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 Go,是因为过去 10 多年间软件开发的难度令人沮丧。Go 是谷歌 2009 发布的第二款编程语言。

    498 引用 • 1395 回帖 • 257 关注
  • Hystrix
    7 引用

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...