hystrix-go
==========
[![Build Status](https://travis-ci.org/afex/hystrix-go.png?branch=master)](https://travis-ci.org/afex/hystrix-go)
[![GoDoc Documentation](http://godoc.org/github.com/afex/hystrix-go/hystrix?status.png)](https://godoc.org/github.com/afex/hystrix-go/hystrix)
[Hystrix](https://github.com/Netflix/Hystrix) is a great project from Netflix.
> Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
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](https://github.com/Netflix/Hystrix/wiki)
For API documentation, refer to [GoDoc](https://godoc.org/github.com/afex/hystrix-go/hystrix)
How to use
----------
```go
import "github.com/afex/hystrix-go/hystrix"
```
### Execute code as a Hystrix command
Define your application logic which relies on external systems, passing your function to ```hystrix.Go```. When that system is healthy this will be the only thing which executes.
```go
hystrix.Go("my_command", func() error {
// talk to other services
return nil
}, nil)
```
### Defining fallback behavior
If you want code to execute during a service outage, pass in a second function to ```hystrix.Go```. Ideally, the logic here will allow your application to gracefully handle external services being unavailable.
This triggers when your code returns an error, or whenever it is unable to complete based on a [variety of health checks](https://github.com/Netflix/Hystrix/wiki/How-it-Works).
```go
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 output
Calling ```hystrix.Go``` is like launching a goroutine, except you receive a channel of errors you can choose to monitor.
```go
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 API
Since calling a command and immediately waiting for it to finish is a common pattern, a synchronous API is available with the `hystrix.Do` function which returns a single error.
```go
err := hystrix.Do("my_command", func() error {
// talk to other services
return nil
}, nil)
```
### Configure settings
During application boot, you can call ```hystrix.ConfigureCommand()``` to tweak the settings for each command.
```go
hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
Timeout: 1000,
MaxConcurrentRequests: 100,
ErrorPercentThreshold: 25,
})
```
You can also use ```hystrix.Configure()``` which accepts a ```map[string]CommandConfig```.
### 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](https://github.com/Netflix/Hystrix/tree/master/hystrix-dashboard) to start streaming events, your commands will automatically begin appearing.
```go
hystrixStreamHandler := hystrix.NewStreamHandler()
hystrixStreamHandler.Start()
go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
```
### Send circuit metrics to Statsd
```go
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)
```
FAQ
---
**What happens if my run function panics? Does hystrix-go trigger the fallback?**
No. hystrix-go does not use ```recover()``` so panics will kill the process like normal.
Build and Test
--------------
- Install vagrant and VirtualBox
- Clone the hystrix-go repository
- Inside the hystrix-go directory, run ```vagrant up```, then ```vagrant ssh```
- ```cd /go/src/github.com/afex/hystrix-go```
- ```go test ./...```
没有合适的资源?快使用搜索试试~ 我知道了~
Netflix 的 Hystrix 延迟和容错库(适用于 Go).zip
共35个文件
go:26个
txt:2个
md:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 9 浏览量
2024-12-02
19:56:27
上传
评论
收藏 37KB ZIP 举报
温馨提示
Netflix 的 Hystrix 延迟和容错库(适用于 Go)hystrix-go Hystrix是 Netflix 的一个很棒的项目。Hystrix 是一个延迟和容错库,旨在隔离对远程系统、服务和第三方库的访问点,阻止级联故障,并在不可避免的故障的复杂分布式系统中实现恢复能力。我认为 Hystrix 模式(程序员定义的回退和自适应健康监测)适用于任何分布式系统。Go 例程和通道是很好的并发原语,但不能直接帮助我们的应用程序在故障期间保持可用。hystrix-go 旨在让 Go 程序员轻松构建具有与基于 Java 的 Hystrix 库类似执行语义的应用程序。有关 Hystrix 工作原理的更多信息,请参阅Java Hystrix wiki有关 API 文档,请参阅GoDoc如何使用import "github.com/afex/hystrix-go/hystrix"将代码作为 Hystrix 命令执行定义依赖于外部系统的应用程序逻辑,并将函数传递给hystrix.Go。当该系统正常运行时,这将是唯一执行的操作。hystrix.Go("m
资源推荐
资源详情
资源评论
收起资源包目录
Netflix 的 Hystrix 延迟和容错库(适用于 Go).zip (35个子文件)
plugins
statsd_collector_test.go 939B
graphite_aggregator.go 4KB
statsd_collector.go 6KB
datadog_collector.go 5KB
.travis.yml 165B
标签.txt 2B
LICENSE 1KB
loadtest
service
main.go 2KB
README.md 158B
资源内容.txt 1KB
hystrix
settings.go 3KB
hystrix_test.go 17KB
pool_metrics.go 855B
metric_collector
metric_collector.go 2KB
default_metric_collector.go 5KB
logger.go 243B
eventstream.go 11KB
metrics.go 3KB
hystrix.go 8KB
eventstream_test.go 6KB
metrics_test.go 896B
doc.go 3KB
settings_test.go 2KB
circuit_test.go 3KB
rolling
rolling.go 2KB
rolling_timing.go 3KB
rolling_test.go 1KB
rolling_timing_test.go 2KB
pool.go 667B
circuit.go 5KB
pool_test.go 1KB
Vagrantfile 365B
.gitignore 9B
README.md 4KB
scripts
vagrant.sh 691B
共 35 条
- 1
资源评论
徐浪老师
- 粉丝: 8455
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 量化交易-RSI策略(vectorbt实现)
- Java答题期末考试必须考
- 组播报文转发原理的及图解实例
- 青龙燕铁衣-数据集.zip
- 指针扫描和内存遍历二合一工具
- 基于JavaScript的在线考试系统(编号:65965158)(1).zip
- 五相电机双闭环矢量控制模型-采用邻近四矢量SVPWM-MATLAB-Simulink仿真模型包括: (1)原理说明文档(重要):包括扇区判断、矢量作用时间计算、矢量作用顺序及切时间计算、PWM波的生成
- Linux下的cursor安装包
- springboot-教务管理系统(编号:62528147).zip
- 3dmmods_倾城系列月白_by_白嫖萌新.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功