在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:afex/hystrix-go开源软件地址:https://github.com/afex/hystrix-go开源编程语言:Go 99.3%开源软件介绍:hystrix-goHystrix is a great project from Netflix.
I think the Hystrix patterns of programmer-defined fallbacks and adaptive health monitoring are good for any distributed system. Go routines and channels are great concurrency primitives, but don't directly help our application stay available during failures. hystrix-go aims to allow Go programmers to easily build applications with similar execution semantics of the Java-based Hystrix library. For more about how Hystrix works, refer to the Java Hystrix wiki For API documentation, refer to GoDoc How to useimport "github.com/afex/hystrix-go/hystrix" Execute code as a Hystrix commandDefine your application logic which relies on external systems, passing your function to hystrix.Go("my_command", func() error {
// talk to other services
return nil
}, nil) Defining fallback behaviorIf you want code to execute during a service outage, pass in a second function to This triggers when your code returns an error, or whenever it is unable to complete based on a variety of health checks. hystrix.Go("my_command", func() error {
// talk to other services
return nil
}, func(err error) error {
// do this when services are down
return nil
}) Waiting for outputCalling output := make(chan bool, 1)
errors := hystrix.Go("my_command", func() error {
// talk to other services
output <- true
return nil
}, nil)
select {
case out := <-output:
// success
case err := <-errors:
// failure
} Synchronous APISince calling a command and immediately waiting for it to finish is a common pattern, a synchronous API is available with the err := hystrix.Do("my_command", func() error {
// talk to other services
return nil
}, nil) Configure settingsDuring application boot, you can call hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
Timeout: 1000,
MaxConcurrentRequests: 100,
ErrorPercentThreshold: 25,
}) You can also use Enable dashboard metricsIn 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) Send circuit metrics to Statsdc, 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) FAQWhat happens if my run function panics? Does hystrix-go trigger the fallback? No. hystrix-go does not use Build and Test
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论