spring-retry
每当软件组件相互通信时,就有可能出现暂时的自我纠正错误。 这些故障包括服务的暂时不可用,网络连接的瞬时丢失或服务繁忙时出现的超时。 在这种情况下,适当的重试处理可以减少这些故障可能引起的问题。
在这篇文章中,我们将看到如何使用Spring Retry向Spring应用程序添加健壮的重试逻辑。 Spring Retry可能不是很了解,因为它没有在Spring文档概述中列出。 但是,您可以在Spring Initializr页面上找到它。
建立
要使用Spring Retry,我们需要在项目中添加以下依赖项:
<dependency><groupid>org.springframework.retry</groupid><artifactid>spring-retry</artifactid><version>1.1.2.RELEASE</version>
</dependency>
Spring Retry使用AOP,因此请确保Spring AOP可用:
<dependency><groupid>org.springframework</groupid><artifactid>spring-aop</artifactid><version>4.2.5.RELEASE</version>
</dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.8</version>
</dependency>
如果您使用的是Spring Boot ,那么可以改用spring-boot-starter-aop:
<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-aop</artifactid>
</dependency>
要启用Spring Retry,我们只需要将@EnableRetry添加到我们的应用程序配置类中:
@EnableRetry
@SpringBootApplication // or @Configuration if you are not using Spring Boot
public class RetryExampleApplication {// ...
}
使用批注添加重试处理
现在,我们准备向方法添加重试处理。 为此,我们仅需使用@Retryable注释适当的方法:
@Service
public class MyService {@Retryablepublic void simpleRetry() {// perform operation that is likely to fail}
}
带有@Retryable注释的方法可以像其他任何方法一样调用。 但是,每当可重试方法的执行因异常而失败时,Spring都会自动重试多达三遍。 默认情况下,Spring在方法调用之间使用1秒的延迟。 请注意,调用线程在重试处理期间会阻塞。
重试行为可以通过多种方式自定义。 例如:
@Service
public class MyService {@Retryable(value = {FooException.class, BarException.class}, maxAttempts = 5)public void retryWithException() {// perform operation that is likely to fail}@Recoverpublic void recover(FooException exception) {// recover from FooException}
}
在这里,我们告诉Spring仅在抛出FooException或BarException类型的Exception时应用重试处理。 其他异常不会导致重试。 maxAttempts = 5告诉Spring如果失败,最多重试该方法5次。
使用@Recover,我们为FooException定义了单独的恢复方法。 当可重试的方法因FooException而失败时,这使我们可以运行特殊的恢复代码。
使用RetryTemplate添加重试处理
除了注释之外,Spring Retry还提供了RetryTemplate,可用于在Java代码中定义重试处理。 与其他任何bean一样,可以在我们的配置类中简单地配置RetryTemplate:
@EnableRetry
@SpringBootApplication // or @Configuration if you are not using Spring Boot
public class RetryExampleApplication {@Beanpublic RetryTemplate retryTemplate() {SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();retryPolicy.setMaxAttempts(5);FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(1500); // 1.5 secondsRetryTemplate template = new RetryTemplate();template.setRetryPolicy(retryPolicy);template.setBackOffPolicy(backOffPolicy);return template;}// ...
}
RetryPolicy确定何时应重试操作。 SimpleRetryPolicy是一个RetryPolicy实现,可重试固定次数。
BackOffPolicy是一个策略接口,用于控制重试尝试之间的退避。 在继续之前,FixedBackOffPolicy会暂停一段固定的时间。 其他一些默认的BackOffPolicy实现是ExponentialBackOffPolicy(增加每次重试的退避时间)或NoBackOffPolicy(重试之间没有延迟)。
现在,我们可以将RetryTemplate注入我们的服务。 要使用重试处理来运行代码,我们只需调用RetryTemplate.execute():
@Service
public class RetryService {@Autowiredprivate RetryTemplate retryTemplate;public void withTemplate() {retryTemplate.execute(context -> {// perform operation that is likely to fail});}// ...
}
RetryTemplate.exeucte()以RetryCallback <T,E>作为参数。 RetryCallback是一个功能接口,因此可以使用Java 8 Lambda表达式来实现(如上所示)。
摘要
Spring重试提供了一种向Spring应用程序添加重试处理的简便方法。 可以使用批注(@Retryable和@Recover)或通过将RetryCallback传递给RetryTemplate来添加重试处理。
- 您可以在GitHub上找到完整的示例源代码。
翻译自: https://www.javacodegeeks.com/2016/03/retry-handling-spring-retry.html
spring-retry