在微服務架構中,根據業務來拆分紅一個個的服務,服務與服務之間能夠相互調用(RPC),在Spring Cloud能夠用RestTemplate+Ribbon和Feign來調用。爲了保證其高可用,單個服務一般會集羣部署。因爲網絡緣由或者自身的緣由,服務並不能保證100%可用,若是單個服務出現問題,調用這個服務就會出現線程阻塞,此時如有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,致使服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統形成災難性的嚴重後果,這就是服務故障的「雪崩」效應。html
爲了解決這個問題,業界提出了斷路器模型。git
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.github
. —-摘自官網spring
Netflix開源了Hystrix組件,實現了斷路器模式,SpringCloud對這一組件進行了整合。 在微服務架構中,一個請求須要調用多個服務是很是常見的,以下圖:瀏覽器
較底層的服務若是出現故障,會致使連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被打開。微信
斷路打開後,可用避免連鎖故障,fallback方法能夠直接返回一個固定值。網絡
這篇文章基於上一篇文章的工程,首先啓動上一篇文章的工程,啓動eureka-server 工程;啓動service-hi工程,它的端口爲8762。架構
改造serice-ribbon 工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依賴:app
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>ide
在程序的啓動類ServiceRibbonApplication 加@EnableHystrix註解開啓Hystrix:
1 @SpringBootApplication 2 @EnableEurekaClient 3 @EnableDiscoveryClient 4 @EnableHystrix 5 public class ServiceRibbonApplication { 6 7 public static void main(String[] args) { 8 SpringApplication.run( ServiceRibbonApplication.class, args ); 9 } 10 11 @Bean 12 @LoadBalanced 13 RestTemplate restTemplate() { 14 return new RestTemplate(); 15 } 16 17 }
改造HelloService類,在hiService方法上加上@HystrixCommand註解。該註解對該方法建立了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字符串,字符串爲」hi,」+name+」,sorry,error!」,代碼以下:
@Service
public class HelloService {
@Autowired
RestTemplate 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
此時關閉 service-hi 工程,當咱們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:
hi ,forezp,orry,error!
這就說明當 service-hi 工程不可用的時候,service-ribbon調用 service-hi的API接口時,會執行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞。
Feign是自帶斷路器的,在D版本的Spring Cloud以後,它沒有默認打開。須要在配置文件中配置打開它,在配置文件加如下代碼:
feign.hystrix.enabled=true
基於service-feign工程進行改造,只須要在FeignClient的SchedualServiceHi接口的註解中加上fallback的指定類就好了:
1 @FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class) 2 public interface SchedualServiceHi { 3 @RequestMapping(value = "/hi",method = RequestMethod.GET) 4 String sayHiFromClientOne(@RequestParam(value = "name") String name); 5 }
SchedualServiceHiHystric須要實現SchedualServiceHi 接口,並注入到Ioc容器中,代碼以下:
1 @Component 2 public class SchedualServiceHiHystric implements SchedualServiceHi { 3 @Override 4 public String sayHiFromClientOne(String name) { 5 return "sorry "+name; 6 } 7 }
啓動四servcie-feign工程,瀏覽器打開http://localhost:8765/hi?name=forezp,注意此時service-hi工程沒有啓動,網頁顯示:
sorry forezp
打開service-hi工程,再次訪問,瀏覽器顯示:
hi forezp,i am from port:8762
這證實斷路器起到做用了。
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter4
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
感謝您的關注!可加QQ1羣:135430763,QQ2羣:454796847,QQ3羣:187424846。QQ羣進羣密碼:xttblog,想加微信羣的朋友,能夠微信搜索:xmtxtt,備註:「xttblog」,添加助理微信拉你進羣。備註錯誤不會贊成好友申請。再次感謝您的關注!後續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合做可添加助理微信進行溝通!