Hystrix——SpringCloud

Hystrix——SpringCloud

轉載於 方誌朋的專欄 https://www.fangzhipeng.com/springcloud/2018/08/04/sc-f4-hystrix.html

故障傳播

微服務架構中,根據業務來拆分成一個個的服務,服務和服務之間可以相互調用(RPC)

在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。爲了保證其高可用,單個服務通常會集羣部署。

問題來了:

由於網絡原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。

服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。

應對: 爲了解決這個問題,業界提出了斷路器模型。

斷路器簡介

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.

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

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

在這裏插入圖片描述
斷路打開後,可用避免連鎖故障,fallback方法可以直接返回一個固定值。

實現

準備工作

這篇文章基於上一篇文章的工程,首先啓動上一篇文章的工程,啓動eureka-server 工程;啓動first-spring-cloud工程,它的端口爲8762。

在ribbon使用斷路器

  1. 引入依賴

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

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  1. 啓動類添加註解 @EnableHystrix註解開啓Hystrix
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
  1. 改造HelloService類,在hiService方法上加上@HystrixCommand註解。該註解對該方法創建了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字符串,字符串爲”hi,”+name+”,sorry,error!”,代碼如下:
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")   //這裏聲明斷路器,調用這個藉口的話,如果掛掉了,就調用 hiError方法
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

  //上面註解使用的 hiError方法
    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

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

hi ,forezp,orry,error!

這就說明,當ribbon中的service所調用的服務,first-spring-cloud不可用的時候,就會斷路,hystrix就會走hiError方法,快速失敗,直接返回一組字符串,而不是等待響應超時。 避免服務宕機

引申: Feign中使用斷路器

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

feign.hystrix.enabled=true

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

@FeignClient(value = "first-spring-cloud",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;
    }
}

注意這裏的 SchedualServiceHi 相當於,對應服務的ApiClient, 而 實現該接口的 SchedualServiceHiHystric 則相當於 Fallback, 是熔斷器。

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

sorry forezp

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

hi forezp,i am from port:8762

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章