spring cloud微服务分布式云架构(四)-断路器(Hystrix)
在微服務(wù)架構(gòu)中,根據(jù)業(yè)務(wù)來拆分成一個(gè)個(gè)的服務(wù),服務(wù)與服務(wù)之間可以相互調(diào)用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調(diào)用。為了保證其高可用,單個(gè)服務(wù)通常會(huì)集群部署。由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證100%可用,如果單個(gè)服務(wù)出現(xiàn)問題,調(diào)用這個(gè)服務(wù)就會(huì)出現(xiàn)線程阻塞,此時(shí)若有大量的請(qǐng)求涌入,Servlet容器的線程資源會(huì)被消耗完畢,導(dǎo)致服務(wù)癱瘓。服務(wù)與服務(wù)之間的依賴性,故障會(huì)傳播,會(huì)對(duì)整個(gè)微服務(wù)系統(tǒng)造成災(zāi)難性的嚴(yán)重后果,這就是服務(wù)故障的“雪崩”效應(yīng)。Spring Cloud大型企業(yè)分布式微服務(wù)云架構(gòu)源碼請(qǐng)加一七九一七四三三八零
為了解決這個(gè)問題,業(yè)界提出了斷路器模型。
一、斷路器簡(jiǎn)介
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組件,實(shí)現(xiàn)了斷路器模式,SpringCloud對(duì)這一組件進(jìn)行了整合。 在微服務(wù)架構(gòu)中,一個(gè)請(qǐng)求需要調(diào)用多個(gè)服務(wù)是非常常見的,如下圖:
較底層的服務(wù)如果出現(xiàn)故障,會(huì)導(dǎo)致連鎖故障。當(dāng)對(duì)特定的服務(wù)的調(diào)用的不可用達(dá)到一個(gè)閥值(Hystric 是5秒20次) 斷路器將會(huì)被打開。
斷路打開后,可用避免連鎖故障,fallback方法可以直接返回一個(gè)固定值。
二、準(zhǔn)備工作
這篇文章基于上一篇文章的工程,首先啟動(dòng)上一篇文章的工程,啟動(dòng)eureka-server 工程;啟動(dòng)service-hi工程,它的端口為8762。
三、在ribbon使用斷路器
改造serice-ribbon 工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依賴:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency> 復(fù)制代碼在程序的啟動(dòng)類ServiceRibbonApplication 加@EnableHystrix注解開啟Hystrix:
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run( ServiceRibbonApplication.class, args );}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}}復(fù)制代碼改造HelloService類,在hiService方法上加上@HystrixCommand注解。該注解對(duì)該方法創(chuàng)建了熔斷器的功能,并指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個(gè)字符串,字符串為"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!";}}復(fù)制代碼啟動(dòng):service-ribbon 工程,當(dāng)我們?cè)L問http://localhost:8764/hi?name=forezp,瀏覽器顯示:
hi forezp,i am from port:8762 復(fù)制代碼此時(shí)關(guān)閉 service-hi 工程,當(dāng)我們?cè)僭L問http://localhost:8764/hi?name=forezp,瀏覽器會(huì)顯示:
hi ,forezp,orry,error! 復(fù)制代碼這就說明當(dāng) service-hi 工程不可用的時(shí)候,service-ribbon調(diào)用 service-hi的API接口時(shí),會(huì)執(zhí)行快速失敗,直接返回一組字符串,而不是等待響應(yīng)超時(shí),這很好的控制了容器的線程阻塞。
四、Feign中使用斷路器 Feign是自帶斷路器的,在D版本的Spring Cloud之后,它沒有默認(rèn)打開。需要在配置文件中配置打開它,在配置文件加以下代碼:
feign.hystrix.enabled=true 復(fù)制代碼基于service-feign工程進(jìn)行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定類就行了:
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class) public interface SchedualServiceHi {@RequestMapping(value = "/hi",method = RequestMethod.GET)String sayHiFromClientOne(@RequestParam(value = "name") String name); }復(fù)制代碼SchedualServiceHiHystric需要實(shí)現(xiàn)SchedualServiceHi 接口,并注入到Ioc容器中,代碼如下:
@Component public class SchedualServiceHiHystric implements SchedualServiceHi {@Overridepublic String sayHiFromClientOne(String name) {return "sorry "+name;} }復(fù)制代碼啟動(dòng)四servcie-feign工程,瀏覽器打開http://localhost:8765/hi?name=forezp,注意此時(shí)service-hi工程沒有啟動(dòng),網(wǎng)頁(yè)顯示:
sorry forezp 復(fù)制代碼打開service-hi工程,再次訪問,瀏覽器顯示:
hi forezp,i am from port:8762 復(fù)制代碼這證明斷路器起到作用了。
轉(zhuǎn)載于:https://juejin.im/post/5c92f111e51d457ad6257574
總結(jié)
以上是生活随笔為你收集整理的spring cloud微服务分布式云架构(四)-断路器(Hystrix)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十一、Shell 文件包含
- 下一篇: less学习笔记