日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)

發(fā)布時間:2024/9/27 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn):https://blog.csdn.net/forezp/article/details/69934399

最新版本:
史上最簡單的SpringCloud教程 | 第四篇:斷路器(Hystrix)(Finchley版本):https://blog.csdn.net/forezp/article/details/81040990

在微服務架構(gòu)中,根據(jù)業(yè)務來拆分成一個個的服務,服務與服務之間可以相互調(diào)用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調(diào)用。為了保證其高可用,單個服務通常會集群部署。由于網(wǎng)絡原因或者自身的原因,服務并不能保證100%可用,如果單個服務出現(xiàn)問題,調(diào)用這個服務就會出現(xiàn)線程阻塞,此時若有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統(tǒng)造成災難性的嚴重后果,這就是服務故障的“雪崩”效應。

為了解決這個問題,業(yè)界提出了斷路器模型。

一、斷路器簡介

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls. —-摘自官網(wǎng)

Netflix開源了Hystrix組件,實現(xiàn)了斷路器模式,SpringCloud對這一組件進行了整合。 在微服務架構(gòu)中,一個請求需要調(diào)用多個服務是非常常見的,如下圖:

較底層的服務如果出現(xiàn)故障,會導致連鎖故障。當對特定的服務的調(diào)用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被打開。

斷路打開后,可用避免連鎖故障,fallback方法可以直接返回一個固定值。

準備工作

這篇文章基于上一篇文章的工程,首先啟動上一篇文章的工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8762。

三、在ribbon使用斷路器

改造serice-ribbon 工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-hystrix的起步依賴:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>

在程序的啟動類ServiceRibbonApplication 加@EnableHystrix注解開啟Hystrix:

@SpringBootApplication @EnableDiscoveryClient @EnableHystrix public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();} }

改造HelloService類,在hiService方法上加上@HystrixCommand注解。該注解對該方法創(chuàng)建了熔斷器的功能,并指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字符串,字符串為”hi,”+name+”,sorry,error!”,代碼如下:

@Service public class HelloService {@AutowiredRestTemplate restTemplate;@HystrixCommand(fallbackMethod = "hiError")public String hiService(String name) {return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);}public String hiError(String name) {return "hi,"+name+",sorry,error!";} }

啟動:service-ribbon 工程,當我們訪問http://localhost:8764/hi?name=forezp,瀏覽器顯示:

hi forezp,i am from port:8762

此時關(guān)閉 service-hi 工程,當我們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:

hi forezp,i am from port:8762

此時關(guān)閉 service-hi 工程,當我們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:

hi ,forezp,orry,error!

這就說明當 service-hi 工程不可用的時候,service-ribbon調(diào)用 service-hi的API接口時,會執(zhí)行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞。

四、Feign中使用斷路器

Feign是自帶斷路器的,在D版本的Spring Cloud中,它沒有默認打開。需要在配置文件中配置打開它,在配置文件加以下代碼:

feign.hystrix.enabled=true

基于service-feign工程進行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定類就行了:

基于service-feign工程進行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定類就行了:

SchedualServiceHiHystric需要實現(xiàn)SchedualServiceHi 接口,并注入到Ioc容器中,代碼如下:

@Component public class SchedualServiceHiHystric implements SchedualServiceHi {@Overridepublic String sayHiFromClientOne(String name) {return "sorry "+name;} }

啟動四servcie-feign工程,瀏覽器打開http://localhost:8765/hi?name=forezp,注意此時service-hi工程沒有啟動,網(wǎng)頁顯示:

sorry forezp

打開service-hi工程,再次訪問,瀏覽器顯示:

hi forezp,i am from port:8762

這證明斷路器起到作用了。

五、Hystrix Dashboard (斷路器:Hystrix 儀表盤)

基于service-ribbon 改造,Feign的改造和這一樣。

首選在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依賴:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>

在主程序啟動類中加入@EnableHystrixDashboard注解,開啟hystrixDashboard:

@SpringBootApplication @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();} }

打開瀏覽器:訪問http://localhost:8764/hystrix,界面如下:

點擊monitor stream,進入下一個界面,訪問:http://localhost:8764/hi?name=forezp
此時會出現(xiàn)監(jiān)控界面:

本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter4

六、參考資料

circuit_breaker_hystrix
feign-hystrix
hystrix_dashboard

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。