簡介
在分布式系統中,服務與服務之間依賴錯綜復雜,一種不可避免的情況就是某些服務將會出現失敗。Hystrix是一個庫,它提供了服務與服務之間的容錯功能,主要體現在延遲容錯和容錯,從而做到控制分布式系統中的聯動故障。Hystrix通過隔離服務的訪問點,阻止聯動故障,并提供故障的解決方案,從而提高了這個分布式系統的彈性。
面對的問題: 一個應用一般會依賴多個服務,每個服務由于網絡不可靠,機房的不可靠等等不穩定的因素,總會導致服務的故障,如果我們不對這些故障做處理,就會進而導致整個系統的不可用。
服務雪崩:
一個正常的用戶進入調用微服務A然后調用B在調用C然后離開,而當其中微服務C出現故障,導致用戶停留
B,越來越多的用戶進入,請求,停留B在最終導致B的資源被耗盡,不可用,進而A也慢慢不可用。這一系
列鏈式反應就像雪崩一樣影響越來越大,稱為"服務雪崩"
服務熔斷
參考:www.ythuaji.com.cn/article/190136.html
應對雪崩效應的一種微服務鏈路保護機制
當調用鏈路的某個微服務不可用或者響應時間太長時,會進行服務熔斷,不再有該節點微服務的調用,快速返回錯誤的響應信息。當檢測到該節點微服務調用響應正常后,恢復調用鏈路。
在Spring Cloud中通過Hystrix實現。Hystrix會監控微服務間調用的狀況,當失敗的調用到一定閾值,缺省是5秒內20次調用失敗,就會啟動熔斷機制。
服務熔斷解決如下問題:
- 當所依賴的對象不穩定時,能夠起到快速失敗的目的;
- 快速失敗后,能夠根據一定的算法動態試探所依賴對象是否恢復
實踐
項目搭建
根據 實驗環境搭建中的項目copy建立一個新模塊名為springcloud-provider-dept-hystrix。
導入依賴
1
2
3
4
5
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-hystrix</ artifactId > < version >1.4.6.RELEASE</ version > </ dependency > |
使用
在Controller層,使用@HystrixCommand來實現熔斷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Target ({ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) @Inherited @Documented public @interface HystrixCommand { String groupKey() default "" ; String commandKey() default "" ; String threadPoolKey() default "" ; String fallbackMethod() default "" ; HystrixProperty[] commandProperties() default {}; HystrixProperty[] threadPoolProperties() default {}; Class<? extends Throwable>[] ignoreExceptions() default {}; ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER; HystrixException[] raiseHystrixExceptions() default {}; String defaultFallback() default "" ; } |
在@HystrixCommand中有 defaultFallback() 指定默認的備用方法(default ""), fallbackMethod() 指定失敗后進行的備用方法(default "")
當正常的方法調用失敗后(5秒內20次調用失敗),認為是出故障了就進行熔斷,快速返回錯誤信息(調用備選方法)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@RestController public class DeptController { @Autowired private DeptImpl deptimpl; @RequestMapping ( "/dev/{id}" ) @HystrixCommand (fallbackMethod = "HystrixGet" ) //指明備用方法 public Dept DeptqueryByID( @PathVariable ( "id" ) Long id) { Dept dept = deptimpl.queryByID(id); System.out.println(dept); if (dept== null ) { throw new RuntimeException( "id--->" + id + "用戶不存在" ); } return dept; } public Dept HystrixGet( @PathVariable ( "id" ) Long id) { Dept dept= new Dept(); dept.setDeptnumber(id.intValue()); dept.setDname( "id" +id+ "用戶不存在" ); dept.setD_source( "no~~~~~~" ); return dept; } } |
在啟動類上添加@EnableCircuitBreaker開啟熔斷支持
1
2
3
4
5
6
7
8
|
@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker //開啟熔斷支持 public class HApplication { public static void main(String[] args) { SpringApplication.run(HApplication. class ,args); } } |
服務降級
即在服務器壓力劇增的情況下,關閉一些很少被調用的服務,騰出一些資源,保證正常運行。
如淘寶雙十一關閉退款通道。
實踐
在原本的FeignClient指明fallbackFactory
1
2
3
4
5
6
7
8
9
10
11
|
@FeignClient (value = "PROVIDER-NAME" ,fallbackFactory = SerciceFallbackFactory. class ) public interface DeptClientService { @RequestMapping (value = "/dev/add" ) boolean add(Dept dept); @RequestMapping (value = "/dev/{id}" ) Dept queryByID( @PathVariable ( "id" ) Long id ); @PostMapping (value = "/dev/list" ) List<Dept> queryAll(); } |
定義自己的FallbackFactory
報錯注意import feign.hystrix.FallbackFactory;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import feign.hystrix.FallbackFactory; @Component public class SerciceFallbackFactory implements FallbackFactory { public DeptClientService create(Throwable cause) { return new DeptClientService() { public boolean add(Dept dept) { return false ; } //定義返回的錯誤信息 public Dept queryByID(Long id) { Dept dept = new Dept(); dept.setD_source( "服務降級" ); dept.setDname( "fail" ); dept.setDeptnumber(- 1 ); return dept; } public List<Dept> queryAll() { return null ; } }; } } |
在客戶端的配置文件中添加
1
2
3
4
|
#開啟降級 feign: hystrix: enabled: true |
結果:在我們關閉服務端后再次訪問服務時
服務熔斷與服務降級的區別
- 服務熔斷是在服務端進行的,而服務降級是在客戶端進行的
- 服務熔斷的原因:發生故障,服務降級:為整體負荷考慮,保證核心業務的運行
服務監控 Dashboard
建立項目
導入依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-hystrix</ artifactId > < version >1.4.6.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-hystrix-dashboard</ artifactId > < version >1.4.6.RELEASE</ version > </ dependency > |
配置文件
1
2
3
4
5
6
7
|
server: port: 9001 hystrix: dashboard: # Hystrix Dashboard會通過proxyUrl解析到host部分,然后通過配置的proxyStreamAllowList。判定是否允許被訪問 proxy-stream-allow-list: "localhost" |
開啟監控支持
1
2
3
4
5
6
7
8
|
@SpringBootApplication @EnableHystrixDashboard //開啟 @EnableDiscoveryClient public class DashboardApplication { public static void main(String[] args) { SpringApplication.run(DashboardApplication. class ,args); } } |
運行后訪問:http://localhost:9001/hystrix
根據提示在spingcloud-provider-dept-hystrix服務端添加bean
1
2
3
4
5
6
|
@Bean public ServletRegistrationBean hystrixMetricsStreamServlet(){ ServletRegistrationBean servlet = new ServletRegistrationBean( new HystrixMetricsStreamServlet()); servlet.addUrlMappings( "/actuator/hystrix.stream" ); return servlet; } |
運行后訪問 http://localhost:8081/actuator/hystrix.stream 可以獲得一些服務的信息
注意: 需要調用一次標注有 @HystrixCommand 方法才會有數據顯示,只會監控有 @HystrixCommand 的方法
我們也可以通過在http://localhost:9001/hystrix 輸入
按下按鈕開啟對該服務的監控
以上就是SpringCloud Hystrix的使用的詳細內容,更多關于SpringCloud Hystrix的資料請關注服務器之家其它相關文章!
原文鏈接:https://juejin.cn/post/6951311432801583112