Spring Cloud實戰系列(四) - 熔斷器Hystrix

相關

相關

  1. Spring Cloud實戰系列(一) - 服務註冊與發現Eureka java

  2. Spring Cloud實戰系列(二) - 客戶端調用Rest + Ribbon spring

  3. Spring Cloud實戰系列(三) - 聲明式客戶端Feign 編程

  4. Spring Cloud實戰系列(四) - 熔斷器Hystrix 後端

  5. Spring Cloud實戰系列(五) - 服務網關Zuul 緩存

  6. Spring Cloud實戰系列(六) - 分佈式配置中心Spring Cloud Config網絡

  7. Spring Cloud實戰系列(七) - 服務鏈路追蹤Spring Cloud Sleuth多線程

  8. Spring Cloud實戰系列(八) - 微服務監控Spring Boot Admin架構

  9. Spring Cloud實戰系列(九) - 服務認證受權Spring Cloud OAuth 2.0 app

  10. Spring Cloud實戰系列(十) - 單點登陸JWT與Spring Security OAuth 框架

前言

在微服務框架 Spring Cloud 中,能夠用 RestTemplate 配合 RibbonFeign 實現 服務與服務 之間的 相互調用

爲了保證服務的 高可用單個服務 一般會採用 集羣部署。因爲 網絡緣由,服務並不能保證 100%可用性,若是 單個服務 出現問題,調用這個服務就會出現 線程阻塞,此時如有 大量的請求 涌入,Servlet 容器的 線程資源 會被耗盡,致使 服務癱瘓

服務與服務之間具備 依賴性,故障會傳播,致使整個微服務系統發生 雪崩

正文

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

下層 的服務若是出現故障,會致使 故障級聯效應。當對特定的服務的調用的 失敗次數 達到一個 閥值Hystrix520 次),斷路器 將會被打開。

斷路器 打開後 底層服務 將會隔斷,可用避免 故障級聯 問題,上層服務 調用 fallback 方法直接返回一個 固定值

1. 在Ribbon上使用熔斷器

pom.xml 文件中引入 hystrix起步依賴 spring-cloud-starter-hystrix

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
複製代碼

在應用的啓動類上使用 @EnableHystrix 開啓 hystrix 的功能。

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
複製代碼

使用註解 @HystrixCommand 標記調用失敗時須要熔斷的方法,fallbackMethod 屬性指定 降級方法方法名fallback

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public String requestForHiService(String name){
        return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
    }

    public String fallback(String name){
        return "Fallback method invoked, name is " + name;
    }
}
複製代碼

2. 在Feign上使用熔斷器

Feign 是自帶 斷路器 的,不過須要在 配置文件 中開啓 hystrix 的配置。

feign:
  hystrix:
    enabled: true
複製代碼

Hystrix 支持 降級回退 操做,當 發生熔斷出現錯誤 時,調用會執行 默認代碼路徑

@FeignClient(value = "service-hi", fallback = HelloServiceFallback.class)
public interface HelloService {
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHi(@RequestParam(value = "name") String name);
}
複製代碼

經過設置 fallback 屬性爲實現 降級回退,來啓用 @FeignClient失敗降級

@Service
public class HelloServiceFallback implements HelloService {

    @Override
    public String sayHi(String name) {
        return "Fallback method invoked, name is " + name;
    }
}
複製代碼

若是須要獲取致使 回退觸發 的緣由,能夠指定 @FeignClient 註解內部的 fallbackFactory 屬性,fallbackFactory 屬性和 fallback 屬性不能一塊兒使用。

@FeignClient(value = "service-hi", fallbackFactory = HelloServiceFallbackFactory.class)
public interface HelloService {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHi(@RequestParam(value = "name") String name);
}
複製代碼

而後提供一個 FallbackFactory實現類,實現類指定 泛型參數HelloService

@Component
public class HelloServiceFallbackFactory implements FallbackFactory<HelloService> {

    @Override
    public HelloService create(Throwable throwable) {
        return new HelloService() {
            @Override
            public String sayHi(String name) {
                return "Fallback method invoked, name is "
                         + name + ", message is " + throwable.getMessage();
            }
        };
    }
}
複製代碼

3. Hystrix Dashboard監控熔斷器的狀態

Hystrix Dashboard 是一個 監控熔斷器 情況的組件,提供了 數據監控圖形界面

3.1. 在Ribbon中使用Hystrix Dashboard

在加入 spring-cloud-starter-hystrix 依賴的基礎上,加入下面的依賴:

<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>
複製代碼

在應用程序 啓動類 已經加上 @EnableHystrix 的基礎上,加入 @EnableHystrixDashboard 註解,代碼以下:

@EnableHystrix
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {
}
複製代碼

啓動應用程序,訪問 http://localhost:8765/hystrix.stream,能夠查看 熔斷器數據指標。訪問 http://localhost:8765/hystrix,查看 Hystrix Dashboard 的界面。在界面上填寫 數據指標URL 地址,點擊 Monitor Stream 進入頁面,如圖所示:

3.2. 在Feign中使用Hystrix Dashboard

在加入 spring-cloud-starter-hystrix 依賴的基礎上,加入下面的依賴:

<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>
複製代碼

在啓動程序上加上 @EnableHystrixDashboard 註解 開啓 Hystrix Dashboard,完整代碼以下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableHystrix
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }
}
複製代碼

完成上面兩步配置,便可開啓 FeignHystrix Dashboard 功能。

參考

  • 方誌朋《深刻理解Spring Cloud與微服務構建》

歡迎關注技術公衆號: 零壹技術棧

零壹技術棧

本賬號將持續分享後端技術乾貨,包括虛擬機基礎,多線程編程,高性能框架,異步、緩存和消息中間件,分佈式和微服務,架構學習和進階等學習資料和文章。

相關文章
相關標籤/搜索