2.結合hystrix

一、簡介

當http服務接口響應超時或調用http服務發生異常時,需要結合hystrix進行降級。

二、引入

在這裏,結合spring-boot的方式,在pom中添加如下依賴:

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

三、使用

  1. 啓動入口就是spring-boot方式,只不過多了一個註解@EnableHystrix

    @SpringBootApplication
    @EnableHystrix
    public class HystrixServiceTest {
        public static void main(String[] args) {
            SpringApplication.run(HystrixServiceTest.class, args);
        }
    }
    

    通過查看EnableHystrix源碼:

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @EnableCircuitBreaker
    public @interface EnableHystrix {
    }
    

    可以發現其被EnableCircuitBreaker所標註,而spring-cloud-netflix-core中已經包含了該註解對應的自動配置類,如下:

    org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
    org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
    

    HystrixCircuitBreakerConfiguration將會採用aop和hystrix相結合的方式,對標註HystrixCommand的方法進行攔截

  2. 對需要降級的方法添加註解HystrixCommand,注意:方法體內異常不要捕獲,即使捕獲的話,需要再拋出,因爲hystrix根據異常會進行降級

    @HystrixCommand(fallbackMethod = "getUGCCountFallback")
    public String getUGCCount() {
        String result = restTemplate.getForObject(
                    "http://vstat.aa.com/dostat.do?method=getVideoPlayCount&v=85152140|85162109&rt=json",
                    String.class);
        return result;
    
    }
    public String getUGCCountFallback() {
        return "fallback";
    }
    

四、一些配置

  1. 常用配置

    1. fallbackMethod = “getUGCCountFallback”

      指定降級回調方法(注意,降級方法需要和原方法簽名一致,並且在同一個類內部)

      另外降級方法仍然可以指定fallbackMethod,即二級降級

    2. threadPoolKey=“CountThreadPool”

      指定線程池的名字,如果不提供,默認使用groupKey作爲線程池名字

    3. groupKey=“CountGroup”

      相同的groupKey使用同一線程池,通常,不同的遠程服務使用不同的groupKey

    4. commandKey = “getUGCCountCommand”

      標識調用服務中的某一方法

    5. ignoreExceptions = {BadRequestException.class}

      如果拋出某個特定的異常,不需要降級

    6. commandProperties = {@HystrixProperty(name=“execution.isolation.thread.timeoutInMilliseconds”, value = “500”)}

      方法執行超時時間,超過該時間則會引發降級

      注意:該超時時間默認爲1秒,此方法的超時時間要和業務超時結合,比如如果業務超時時間是2秒,那麼可能很多超過1秒的請求就被中斷了,直接執行降級

      建議業務方法超時<hystrix超時,這樣業務方法超時拋出異常,hystrix進行降級。

      如果業務方法超時>hystrix超時,並且業務方法不響應中斷,那麼可能導致hystrix線程池堵住。

      一些默認值參考com.netflix.hystrix.HystrixCommandProperties & HystrixPropertiesManager

    7. threadPoolProperties = {

      @HystrixProperty(name = “coreSize”, value = “30”),

      @HystrixProperty(name = “maxQueueSize”, value = “101”),

      @HystrixProperty(name = “keepAliveTimeMinutes”, value = “2”),

      @HystrixProperty(name = “queueSizeRejectionThreshold”, value = “15”),

      @HystrixProperty(name = “metrics.rollingStats.numBuckets”, value = “12”),

      @HystrixProperty(name = “metrics.rollingStats.timeInMilliseconds”, value = “1440”)}

      線程池配置

    8. @DefaultProperties是類級別的配置,它可以配置如下元素:

      groupKey, threadPoolKey, commandProperties, threadPoolProperties等等

      默認此類中的方法帶有@HystrixCommand的將會使用@DefaultProperties的配置,除非它自己指定

  2. 其餘配置請參考:

    https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration

    https://github.com/Netflix/Hystrix/wiki/Configuration

發佈了62 篇原創文章 · 獲贊 23 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章