1.hystrix客戶端
netflix已經創建了一個名為hystrix的庫,實現了斷路器的模式。在microservice架構通常有多個層的服務調用。
低水平的服務的服務失敗會導致級聯故障一直給到用戶。當調用一個特定的服務達到一定閾值(默認5秒失敗20次),打開斷路器。在錯誤的情況下和一個開啟的斷路回滾應可以由開發人員提供。
有一個斷路器阻止級聯失敗并且允許關閉服務一段時間進行愈合。回滾會被其他hystrix保護調用,靜態數據或健全的空值。
代碼如下:
1
2
3
4
5
6
7
8
9
|
@springbootapplication @enablecircuitbreaker public class application { public static void main(string[] args) { new springapplicationbuilder(application. class ).web( true ).run(args); } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
@component public class storeintegration { @hystrixcommand (fallbackmethod = "defaultstores" ) public object getstores(map<string, object> parameters) { //do stuff that might fail } public object defaultstores(map<string, object> parameters) { return /* something useful */ ; } } |
@hystrixcommand是由netflix contrib 庫提供,叫做javanica。spring cloud自動包裝spring bean與注釋的代理連接到hystrix斷路器。斷路器計算何時打開和關閉斷路,并在失敗的情況下做什么。
配置@hystrixcommand可以使用commandproperties屬性的列表@hystrixproperty注釋。詳細請看https://github.com/netflix/hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration
https://github.com/netflix/hystrix/wiki/configuration
1.1 傳播安全上下文或者使用spring范圍
如果你想要一些線程本地上下文傳播到@hystrixcommand默認聲明將不會工作,因為它執行線程池中的命令(在超時的情況下)。
可以切換hystrix使用一些配置用相同的線程調用者,或直接在注釋,讓它使用不同的“隔離策略”(isolation strategy)。
例如:
1
2
3
4
5
6
|
@hystrixcommand (fallbackmethod = "stubmyservice" , commandproperties = { @hystrixproperty (name= "execution.isolation.strategy" , value= "semaphore" ) } ) ... |
詳細內容請參考https://github.com/netflix/hystrix/wiki/configuration
1.2 健康監控
連接的斷路器的狀態也暴露在調用應用程序的/health端點。
1
2
3
4
5
6
7
8
9
|
{ "hystrix" : { "opencircuitbreakers" : [ "storeintegration::getstoresbylocationlink" ], "status" : "circuit_open" }, "status" : "up" } |
1.3 hystrix metrics stream(hystrix指標流)
spring-boot-starter-actuator中實現了hystrix metrics stream。暴露/hystrix.stream作為一個管理端點。
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> |
2.hystrix dashboard
hystrix的主要好處之一是它收集關于每個hystrixcommand組指標。hystrix儀表板顯示每個斷路器的健康高效的方式。
運行hystrix儀表板需要在spring boot主類上標注@enablehystrixdashboard。然后訪問/ hystrix查看儀表盤,在hystrix客戶端應用使用/hystrix.stream監控。
2.1 turbine
看一個實例hystrix數據對于整個系統的健康不是很有用。turbine是一個應用程序,該應用程序匯集了所有相關的/hystrix.stream端點到 /turbine.stream用于hystrix儀表板。運行turbine使用@enableturbine注釋你的主類,使用spring-cloud-starter-turbine這個jar。配置請參考https://github.com/netflix/turbine/wiki/configuration-(1.x)
唯一的區別是turbine.instanceurlsuffix不需要端口號前綴,因為這是自動處理,除非turbine.instanceinsertport = false。
turbine.appconfig配置是一個eureka服務id列表,turbine將使用這個配置查詢實例。turbine stream在hystrix dashboard中使用如下的url配置:
http://my.turbine.server:8080/turbine.stream?cluster=,如果集群的名稱是default,集群參數可以忽略)。這個集群參數必須和turbine.aggregator.clusterconfig匹配。從eureka返回的值都是大寫的,因此我們希望下面的例子可以工作,如果一個app使用eureka注冊,并且被叫做customers:
1
2
3
4
|
turbine: aggregator: clusterconfig: customers appconfig: customers |
clustername可以使用spel表達式定義,在turbine.clusternameexpression。
默認值是appname,意思是eureka服務id最終將作為集群的key,例如customers的instanceinfo有一個customers的appname。另外一個例子是turbine.clusternameexpression=asgname,將從aws asg name獲取集群名稱。
另一個例子:
1
2
3
4
5
|
turbine: aggregator: clusterconfig: system,user appconfig: customers,stores,ui,admin clusternameexpression: metadata[ 'cluster' ] |
在這種情況下,集群名稱從4個服務從其元數據映射,期望包含“system”和“user”。
所有的app使用default,你需要一個文字表達式(使用單引號):
1
2
3
|
turbine: appconfig: customers,stores clusternameexpression: 'default' |
spring cloud提供一個spring-cloud-starter-turbine,所有依賴項你需要運行一個turbine服務器。使用@enableturbine創建一個spring boot應用。
2.2 turbine amqp
在某些環境中(如在paas),典型的turbine模型的指標從所有分布式hystrix命令不起作用。在這種情況下,你可能想要你hystrix命令推動指標turbine,和spring cloud,就要使用amqp消息傳遞。所有您需要做的是在客戶端添加一個依賴spring-cloud-netflix-hystrix-amqp并確保代rabbitmq可用。(有關詳細信息,請參閱彈簧引導文檔如何配置客戶端憑據,但它應該工作的當地代理或云計算)。
hystrix相關的其他文章:
hystrix緩存功能的使用:http://www.ythuaji.com.cn/article/166243.html
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/zhuchuangang/article/details/51289593