Circuit Breaker(熔断)实现之spring-retry、hystrix

spring-retry


    org.springframework.retry
    spring-retry


    org.aspectj
    aspectjweaver
    1.9.5
// springboot启动类
@EnableRetry
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

// 实现类正常方法
// openTimeout时间范围内失败maxAttempts次数后,熔断打开resetTimeout时长
@CircuitBreaker(maxAttempts = 3, openTimeout = 3000L, resetTimeout = 5000L )
public String normalMethod(String param) {
    log.info("======== normalMethod ======== " + param);
    if (true) {
        throw new RuntimeException("方法异常");
    }
    return param;
}

// 降级方法
@Recover
public String recoverMethod(Throwable t, String param) {
    log.info("======== recoverMethod ======== " + param);
    return param;
}

日志如下, 观察日志发现并没有half-open半开过程,熔断关闭后会每次重试maxAttempts次请求。还有一点,spring-retry不支持线程池、信号量隔离。

2019-12-20 18:43:30.552: ======== normalMethod ======== testParam
2019-12-20 18:43:30.553: ======== recoverMethod ======== testParam
2019-12-20 18:43:30.813: ======== normalMethod ======== testParam
2019-12-20 18:43:30.813: ======== recoverMethod ======== testParam
2019-12-20 18:43:31.456: ======== normalMethod ======== testParam
2019-12-20 18:43:31.456: ======== recoverMethod ======== testParam
2019-12-20 18:43:31.785: ======== recoverMethod ======== testParam
2019-12-20 18:43:33.589: ======== recoverMethod ======== testParam

2019-12-20 18:43:45.059: ======== normalMethod ======== testParam
2019-12-20 18:43:45.059: ======== recoverMethod ======== testParam
2019-12-20 18:43:45.579: ======== normalMethod ======== testParam
2019-12-20 18:43:45.579: ======== recoverMethod ======== testParam
2019-12-20 18:43:47.294: ======== normalMethod ======== testParam
2019-12-20 18:43:47.294: ======== recoverMethod ======== testParam
2019-12-20 18:43:47.757: ======== recoverMethod ======== testParam
2019-12-20 18:43:50.242: ======== recoverMethod ======== testParam

 hystrix


    com.netflix.hystrix
    hystrix-core
    1.5.18


    com.netflix.hystrix
    hystrix-javanica
    1.5.18
// springboot启动类省略
// 由于没有使用自动装配,需要自己定义相关Bean
@Configuration
public class HystrixConfig {

    @Bean
    public HystrixCommandAspect hystrixCommandAspect() {
        return new HystrixCommandAspect();
    }

    @Bean
    public HystrixShutdownHook hystrixShutdownHook() {
        return new HystrixShutdownHook();
    }

    private class HystrixShutdownHook implements DisposableBean {

        @Override
        public void destroy() throws Exception {
            // Just call Hystrix to reset thread pool etc.
            Hystrix.reset();
        }

    }
}

// 默认熔断规则是10秒钟内,20次请求,存在50%失败就熔断5秒钟时间
@HystrixCommand(fallbackMethod = "fallbackMethod",
		commandProperties = {@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
				@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "200"),
				@HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "200")})
public String normalMethod(String param) {
    log.info("======== normalMethod ======== " + param);
    if (true) {
        throw new RuntimeException("方法异常");
    }
    return param;
}

public String fallbackMethod(String param) {
    log.info("======== fallbackMethod ======== " + param);
    return param;
}

 日志如下, 观察日志发现在熔断开启5秒过后存在half-open半开过程,会有一次正常请求发送。

2019-12-20 18:45:01.079: ======== normalMethod ======== testParam
2019-12-20 18:45:01.085: ======== fallbackMethod ======== testParam
2019-12-20 18:45:01.322: ======== normalMethod ======== testParam
2019-12-20 18:45:01.323: ======== fallbackMethod ======== testParam
2019-12-20 18:45:01.954: ======== normalMethod ======== testParam
2019-12-20 18:45:01.954: ======== fallbackMethod ======== testParam
2019-12-20 18:45:02.532: ======== normalMethod ======== testParam
2019-12-20 18:45:02.533: ======== fallbackMethod ======== testParam
2019-12-20 18:45:03.063: ======== normalMethod ======== testParam
2019-12-20 18:45:03.063: ======== fallbackMethod ======== testParam
2019-12-20 18:45:03.505: ======== normalMethod ======== testParam
2019-12-20 18:45:03.506: ======== fallbackMethod ======== testParam
2019-12-20 18:45:03.972: ======== normalMethod ======== testParam
2019-12-20 18:45:03.972: ======== fallbackMethod ======== testParam
2019-12-20 18:45:04.414: ======== normalMethod ======== testParam
2019-12-20 18:45:04.414: ======== fallbackMethod ======== testParam
2019-12-20 18:45:04.865: ======== normalMethod ======== testParam
2019-12-20 18:45:04.866: ======== fallbackMethod ======== testParam
2019-12-20 18:45:05.625: ======== normalMethod ======== testParam
2019-12-20 18:45:05.625: ======== fallbackMethod ======== testParam
2019-12-20 18:45:06.253: ======== normalMethod ======== testParam
2019-12-20 18:45:06.253: ======== fallbackMethod ======== testParam
2019-12-20 18:45:06.806: ======== normalMethod ======== testParam
2019-12-20 18:45:06.806: ======== fallbackMethod ======== testParam
2019-12-20 18:45:07.377: ======== normalMethod ======== testParam
2019-12-20 18:45:07.378: ======== fallbackMethod ======== testParam
2019-12-20 18:45:07.938: ======== normalMethod ======== testParam
2019-12-20 18:45:07.938: ======== fallbackMethod ======== testParam
2019-12-20 18:45:08.335: ======== normalMethod ======== testParam
2019-12-20 18:45:08.336: ======== fallbackMethod ======== testParam
2019-12-20 18:45:08.843: ======== normalMethod ======== testParam
2019-12-20 18:45:08.843: ======== fallbackMethod ======== testParam
2019-12-20 18:45:09.404: ======== normalMethod ======== testParam
2019-12-20 18:45:09.404: ======== fallbackMethod ======== testParam
2019-12-20 18:45:09.940: ======== normalMethod ======== testParam
2019-12-20 18:45:09.940: ======== fallbackMethod ======== testParam
2019-12-20 18:45:10.466: ======== normalMethod ======== testParam
2019-12-20 18:45:10.466: ======== fallbackMethod ======== testParam
2019-12-20 18:45:10.889: ======== normalMethod ======== testParam
2019-12-20 18:45:10.889: ======== fallbackMethod ======== testParam
2019-12-20 18:45:11.438: ======== fallbackMethod ======== testParam
2019-12-20 18:45:11.946: ======== fallbackMethod ======== testParam
2019-12-20 18:45:12.414: ======== fallbackMethod ======== testParam
2019-12-20 18:45:12.971: ======== fallbackMethod ======== testParam

2019-12-20 18:45:16.774: ======== normalMethod ======== testParam
2019-12-20 18:45:16.774: ======== fallbackMethod ======== testParam
2019-12-20 18:45:17.245: ======== fallbackMethod ======== testParam
2019-12-20 18:45:17.718: ======== fallbackMethod ======== testParam
2019-12-20 18:45:18.189: ======== fallbackMethod ======== testParam
2019-12-20 18:45:18.622: ======== fallbackMethod ======== testParam

2019-12-20 18:45:33.752: ======== normalMethod ======== testParam
2019-12-20 18:45:33.752: ======== fallbackMethod ======== testParam
2019-12-20 18:45:34.669: ======== fallbackMethod ======== testParam
2019-12-20 18:45:35.354: ======== fallbackMethod ======== testParam
2019-12-20 18:45:35.968: ======== fallbackMethod ======== testParam
2019-12-20 18:45:36.592: ======== fallbackMethod ======== testParam

你可能感兴趣的:(Java,Spring,Cloud,Spring,Boot)