github完整代码地址 我的博客git
hystrix是Netflix开源的一个JAVA项目,不过GitHub也有golang的实现版本hystrix-gogithub
hystrix并无自带一个仪表盘,没法直观的查看接口的健康情况。因此,咱们采用GitHub的一个开源实现hystrix-dashboard。golang
docker run --name hystrix-dashboard -d -p 8081:9002 mlabouardy/hystrix-dashboard:latest
复制代码
关于hystrix的工做原理,能够查阅相关资料,这里只讲解如何封装插件在micro API网关中使用。docker
err := hystrix.Do("my_command", func() error {
// talk to other services
return nil
}, nil)
复制代码
使用hystrix.Do() 同步API,第一个参数是command, 应该是与当前请求一一对应的一个名称,如入“GET-/test”。第二个参数传入一个函数,函数包含我咱们本身的错误逻辑,当请求失败时应该返回error。hystrix会根据咱们的失败率执行熔断策略。bash
// 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的几个默认配置参数,方便看效果app
// 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,
})
复制代码
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。post
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.ui
hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
复制代码