Spring Cloud學習:03斷路器(Hystrix)

1 Hystrix介紹

Spring Cloud Hystrix是分佈式系統處理超時和錯誤的機制,以下圖所示,分佈式系統中某個用戶請求依賴A、H、I、P服務。html

當此請求併發超過50的時候,服務I處理速度變慢,可是服務I仍是被調用。java

大量請求會阻塞在Tomcat服務器上,影響其它整個服務。在複雜的分佈式架構的應用程序有不少的依賴,都會不可避免地在某些時候失敗。高併發的依賴失敗時若是沒有隔離措施,當前應用服務就有被拖垮的風險。laravel

Spring Cloud Hystrix就是隔離措施的一種實現,能夠設置在某種超時或者失敗情形下斷開依賴調用或者返回指定邏輯,從而提升分佈式系統的穩定性。git

2 測試Hystrix

2.1 Ribbon使用Hystrix

2.1.1改造ribbon-service模塊,在pom.xml文件中添加spring-cloud-starter-hystrix起步依賴github

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

2.1.2 在啓動類上添加@EnableHystrix註解開啓Hystrix斷路器功能spring

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonServiceApplication {

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

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

2.1.3 在測試接口上添加@HystrixCommand註解爲該方法添加斷路器功能,使用fallbackMethod參數指定服務異常時的熔斷方法,同時添加一個服務異常時調用的方法。服務器

@RestController
public class DemoController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/one")
    @HystrixCommand(fallbackMethod = "serviceError")
    public String one(){
        return restTemplate.getForObject("http://ONE-SERVICE/one",String.class);
    }

    /**
     *服務異常調用方法
     * @return
     */
    public String serviceError(){
        return "service has error...";
    }

}

2.1.4 啓動服務註冊中心、one-service、ribbon-service,服務正常時訪問http://localhost:8764/one顯示:網絡

關閉one-service服務,訪問http://localhost:8764/one 顯示:架構

2.2 Feign使用Hystrix

2.2.1 Feign已經包含斷路器功能,但默認未打開,能夠在YML配置文件中配置打開:併發

server:
    port: 8765

eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/

spring:
    application:
      name: feign-service #指明應用名稱(服務與服務相互調用根據name屬性)
      
feign:
    hystrix:
      enabled: true #開啓feign斷路器功能

2.2.2 添加Feign接口斷路器類,必須實現Feign接口

@Component
public class FeignDemoServiceHystrix implements FeignDemoService {

    @Override
    public String invocateOneService() {
        return "feign service has error...";
    }
}

2.2.3 在Feign接口的@FeignClient註解上添加fallback參數指定斷路器類

@FeignClient(value = "one-service", fallback = FeignDemoServiceHystrix.class)
public interface FeignDemoService {

    @RequestMapping(value = "/one", method = RequestMethod.GET)
    String invocateOneService();
}

2.2.4 啓動服務註冊中心、one-service、feign-service,服務正常時訪問http://localhost:8765/one顯示:

關閉one-service服務,訪問http://localhost:8765/one 顯示:

3 Hystrix Dashboard

改造ribbon-service模塊,添加斷路器儀表盤功能

3.1 在pom.xml中添加起步依賴:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

3.2 在啓動類上添加@EnableHystrixDashboard註解開啓斷路器儀表盤功能

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonServiceApplication {

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

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

3.3 訪問http://localhost:8764/hystrix以下圖:

參數解釋:

Delay:該參數用來控制服務器上輪詢監控信息的延遲時間,默認爲2000毫秒,咱們能夠經過配置該屬性來下降客戶端的網絡和CPU消耗。

Title:該參數對應了Monitor Stream監控頁面Hystrix Stream以後的內容,默認會使用具體監控實例的URL,咱們能夠經過配置該信息來展現更合適的標題。

3.4 點擊Monitor Stream按鈕,並請求http://localhost:8764/one,能夠查看到監控數據:

本文源碼下載地址:

https://github.com/laravelshao/spring-cloud-learning/tree/master/setion03-hystrix

4 參考資料

Spring->How to Include Hystrix

GitHub->Netflix Hystrix

方誌朋->史上最簡單的SpringCloud教程

翟永超->Spring Cloud基礎教程

相關文章
相關標籤/搜索