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

 此文章基於:

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

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

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

 

三.斷路器(Hystrix

 

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

 

爲了解決這個問題,業界提出了斷路器模型。架構

 

 

1、斷路器簡介

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

 

 

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

 

斷路打開後,可用避免連鎖故障fallback方法能夠直接返回一個固定值。微服務

 

2、準備工做

 

啓動demo1 工程;啓動demo2工程,它的端口爲8763post

 

 

3、ribbon使用斷路器

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

 項目架構:

 

改造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
@EnableHystrix public 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!」

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

註解解析:

@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調用 demo2API接口時,會執行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞

 

4、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容器中,

@Component
public 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

這證實斷路器起到做用了。

 

 

 

 

                          未完,待續。。。

相關文章
相關標籤/搜索