Spring-Retry

Spring Retry框架——看这篇就够了

自作多情 提交于 2020-09-30 02:44:31
简介 软件架构从当初的单机,演变到后来的集群,再到后来的分布式应用。原本看似可以信任的服务调用,加上了网络因素就变得不再可靠。再考虑到一些调用链路的特殊性,又要保证性能,又要尽可能增加成功率,所以调用方必须肩负起重试的责任。 自己写,怎样实现? 重试并不复杂,首先来分析下重试的调用场景,可以想到业务当中不止一处会需要重试能力,并且业务其实更关乎自己的代码块被重试就可以了,而不在乎如何实现的重试。 所以变化的是一段可以重复执行的代码块,以及重试次数等。 ① 代码块可以用Java8支持的函数式编程解决 ② 次数可以用入参/配置实现 首先定义一个执行的模版,结合上面我们的分析,这个模版需要一个待实行的 方法块 ,以及 配置。 (次数等区分场景,用枚举定义,方便全局管控): @AllArgsConstructor @Getter public enum RetrySceneEnums { QUERY_USER_INFO("查询用户信息", 2), ; private String desc; private int retryTimes; } abstract class MyRetryTemplate<Req, Resp> { /** 配置 */ private IntegrationRetryEnums retryEnum; /** 方法块 */ private Function

How to configure RetryTemplate only for Http status code 500?

元气小坏坏 提交于 2020-08-25 04:04:28
问题 I'm using spring-retry (with java 8 lambda) to retry the failed REST calls. I want to retry only for those call which returned 500 error. But I'm not able to configure retrytemplate bean for that. Currently the bean is simple as follows: @Bean("restRetryTemplate") public RetryTemplate retryTemplate() { Map<Class<? extends Throwable>, Boolean> retryableExceptions= Collections.singletonMap(HttpServerErrorException.class, Boolean.TRUE); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3,

Spring @Retryable - how to log when it is invoked?

六月ゝ 毕业季﹏ 提交于 2020-08-03 14:20:29
问题 I use compile 'org.springframework.retry:spring-retry:1.2.2.RELEASE' with Spring Boot 1.5.9.RELEASE . Configured to retry my method and it works well: @Retryable(value = { IOException.class }, maxAttempts = 5, backoff = @Backoff(delay = 500)) public void someMethod(){...} How to output some specific message when retry occurs? 回答1: You can register a RetryListener : @Bean public List<RetryListener> retryListeners() { Logger log = LoggerFactory.getLogger(getClass()); return Collections

Spring @Retryable - how to log when it is invoked?

非 Y 不嫁゛ 提交于 2020-08-03 14:20:11
问题 I use compile 'org.springframework.retry:spring-retry:1.2.2.RELEASE' with Spring Boot 1.5.9.RELEASE . Configured to retry my method and it works well: @Retryable(value = { IOException.class }, maxAttempts = 5, backoff = @Backoff(delay = 500)) public void someMethod(){...} How to output some specific message when retry occurs? 回答1: You can register a RetryListener : @Bean public List<RetryListener> retryListeners() { Logger log = LoggerFactory.getLogger(getClass()); return Collections