你真正的瞭解 hystrix-go 嘛

我是一隻可愛的土撥鼠,專注於分享 Go 職場、招聘和求職,解 Gopher 之憂!歡迎關注我。

歡迎大家加入 Go 招聘交流羣,來這裏找志同道合的小夥伴!跟土撥鼠們一起交流學習。

Hystrix[1] 是 Netflix 的一個非常棒的項目。

Hystrix 是一個延遲和容錯庫,旨在隔離對遠程系統、服務和第三方庫的訪問點,防止級聯故障,並在故障不可避免的複雜分佈式系統中實現彈性。

我認爲程序員定義的回退 (fallbacks) 和自適應健康監控的 Hystrix 模式適用於任何分佈式系統。Goroutines 和 channels 是很好的併發原語,但不能直接幫助我們的應用程序在故障期間保持可用。

hystrix-go[2] 旨在讓 Go 程序員輕鬆構建具有與基於 Java 的 Hystrix 庫類似的執行語義的應用程序。

有關 Hystrix 工作原理的更多信息,請參閱 Java Hystrix wiki[3]

有關 API 文檔,請參閱 pkg.go.dev[4]

如何使用

import "github.com/afex/hystrix-go/hystrix"

將代碼作爲 Hystrix 命令執行

定義依賴於外部系統的應用程序邏輯,將函數傳遞給hystrix.Go. 當該系統正常健康時,會執行此方法。

hystrix.Go("my_command", func() error {
 // talk to other services
 return nil
}, nil)

定義回退行爲

如果你希望在服務中斷期間執行代碼,可以將第二個函數傳遞給hystrix.Go. 理想情況下,這兒的邏輯可以使應用程序優雅地處理不可用的外部服務。

當代碼返回一個錯誤時,或者當它基於各種健康檢查 [5] 無法完成時,就會觸發此事件。

hystrix.Go("my_command", func() error {
 // talk to other services
 return nil
}, func(err error) error {
 // do this when services are down
 return nil
})

等待輸出

調用hystrix.Go就像啓動一個 goroutine,你會收到一個可以 select 監控的 error channel。

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
}

同步 API

由於調用命令並立即等待它完成是種常見的模式,因此 hystrix 提供了一個同步 API。hystrix.Do函數返回一個錯誤。

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

配置設置

在應用程序啓動期間,你可以調用hystrix.ConfigureCommand()來調整每個命令的設置。

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

你也可以使用hystrix.Configure(),它接受一個map[string]CommandConfig的 map。

如何啓用儀表板指標

在你的 main.go 中,在端口上註冊事件流 HTTP 處理程序並在 goroutine 中啓動它。一旦你爲 Hystrix 儀表板 [6] 配置了渦輪機以開始流式傳輸事件,你的命令將自動開始出現。

在 main.go 中,在端口上註冊事件流 HTTP 處理程序,並在 goroutine 中啓動它。一旦爲 Hystrix 儀表板配置了渦輪機 (turbine) 以啓動流事件,命令將自動開始顯示。

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

將 circuit 指標發送到 Statsd

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

如果我的運行函數發生了 panic 會怎麼樣?hystrix-go 會觸發回退嗎?

不,hystrix-go 不使用recover(),所以 panic 會像平常一樣殺死進程。

如何構建和測試

相關的庫

談到熔斷限流的話題,想必大家也想到了阿里開源的面向分佈式、多語言異構化服務架構的流量治理組件 sentinel-go[7] ,關於 sentinel-go 中滑動窗口的設計,感興趣的同學可以查看深度解析 Sentinel Go 滑動窗口統計結構的設計

另外關於 hstrix-go 源碼分析的章節,可以查看小土之前轉發阿松的微服務架構下的熔斷框架:hystrix-go

參考資料

[1]

Hystrix: https://github.com/Netflix/Hystrix

[2]

hystrix-go: https://github.com/afex/hystrix-go/

[3]

Java Hystrix wiki: https://github.com/Netflix/Hystrix/wiki

[4]

pkg.go.dev: https://pkg.go.dev/github.com/afex/hystrix-go/hystrix

[5]

各種健康檢查: https://github.com/Netflix/Hystrix/wiki/How-it-Works

[6]

Hystrix 儀表板: https://github.com/Netflix/Hystrix/tree/master/hystrix-dashboard

[7]

sentinel-go: https://github.com/alibaba/sentinel-golang

Go 招聘 Golang 相關求職和招聘,以及面試題、經驗分享,Go 語言其他知識和職場也是值得分享的。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/zlWI27R7wPEJM-lRLcu8EQ