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

6 篇文章 0 订阅
3 篇文章 0 订阅

spring-retry

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>
// 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

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.18</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.18</version>
</dependency>
// 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值