问题

Springboot @retryable 不重试


以下代码不会重试。我错过了什么?

@EnableRetry
@SpringBootApplication
public class App implements CommandLineRunner
{
    .........
    .........


    @Retryable()
    ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception
    {
        System.out.println("try!");
        throw new Exception();
        //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class);
    }

我在 pom.xml 中添加了以下内容。

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

我还尝试为@Retryable 提供不同的参数组合。

@Retryable(maxAttempts=10,value=Exception.class,backoff=@Backoff(delay = 2000,multiplier=2))

谢谢。

推荐答案

在 spring boot 2.0.2 Release 中,我观察到如果您在同一类中有可重试和调用的方法,则 @Retryable 不起作用。在调试时发现切入点没有正确构建。目前,这个问题的解决方法是我们需要在不同的类中编写方法并调用它。

工作示例可以在这里找到。