玩轉SpringCloud(F版本) 三.斷路器(Hystrix)RestTemplate+Ribbon和Feign兩種方式

來源:互聯網
上載者:User
 此文章基於:

玩轉SpringCloud 一.服務的註冊與發現(Eureka)

玩轉SpringCloud 二.服務消費者(1)ribbon+restTemplate

轉SpringCloud 二.服務消費者(2)feign

 

三. 斷路器(Hystrix)

 

在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用來調用。為了保證其高可用,單個服務通常會叢集部署。由於網路原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。

 

為瞭解決這個問題,業界提出了斷路器模型。

 

 

一、 斷路器簡介

Netflix開源了Hystrix組件,實現了斷路器模式,SpringCloud對這一組件進行了整合。 在微服務架構中,一個請求需要調用多個服務是非常常見的,如:

 

 

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

 

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

 

二、 準備工作

 

啟動demo1 工程;啟動demo2工程,它的連接埠為8763。

 

 

三、 在ribbon使用斷路器

基於 玩轉SpringCloud 二.服務消費者(1)ribbon+restTemplate

 項目架構:

 

改造demo3 工程的代碼,首先在pox.xml檔案中加入spring-cloud-starter-netflix-hystrix的起步依賴:

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

在程式的啟動類ServiceRibbonApplication 加@EnableHystrix註解開啟Hystrix:

@EnableEurekaClient@EnableDiscoveryClient@SpringBootApplication@EnableHystrixpublic class Demo3Application {   public static void main(String[] args) {      SpringApplication.run(Demo3Application.class, args);   }   @Bean   @LoadBalanced   RestTemplate restTemplate() {      return new RestTemplate();   }}

 註解解析:

@EnableHystrix

1.開啟熔斷機制

 

 

改造HelloService類,在hiService方法上加上@HystrixCommand註解。該註解對該方法建立了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字串,字串為”hi,”+name+”,sorry,error!”

@Servicepublic 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!";    }}

註解解析:

@HystrixCommand

1. 指定斷路後執行的方法,通過其調用服務,實現fallback方法

2. 實現在Spring Cloud中使用Hystrix組件:

 

 

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

 

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

 

 

這就說明當 demo2工程停用時候,demo3調用 demo2的API介面時,會執行快速失敗,直接返回一組字串,而不是等待響應逾時,這很好的控制了容器的線程阻塞。

 

四、 Feign中使用斷路器

基於 玩轉SpringCloud 二.服務消費者(2)feign

 項目架構:

 

基於demo4工程進行改造

Feign是內建斷路器的,在D版本的Spring Cloud之後,它沒有預設開啟。需要在設定檔中配置開啟它,在yml最後一行加入:

 

feign.hystrix.enabled: true

 

基於demo4工程進行改造,只需要在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);}

SchedualServiceHiHystric需要實現SchedualServiceHi 介面,並注入到Ioc容器中,

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

註解解析:

 

@Component

 

1. 把普通pojo執行個體化到spring容器中

 

2. 泛指各種組件,就是說當我們的類不屬於各種歸類的時候(不屬於@Controller、@Services等的時候),我們就可以使用@Component來標註這個類。

 

 

可能出現的錯誤:

 

那是因為

這裡必須寫實作類別的.class

 

啟動四demo4工程,瀏覽器開啟http://localhost:8765/hi?name=fsdm,注意此時demo2工程已經啟動,網頁顯示:

 

 

關閉demo2工程,重新訪問http://localhost:8765/hi?name=fsdm

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

 

 

 

 

                          未完,待續。。。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.