Spring Boot @retryable没有重试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot @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))

谢谢。

答案

对于要发现的方法的@Retryable注释,需要从初始化的上下文中正确调用它。方法是从spring上下文中调用bean还是通过其他方式调用?

如果测试这是你的跑步者使用SpringJunit4ClassRunner

另一答案

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

工作实例可以找到here

另一答案

我解决了我发现如果从你试图重试的方法返回一些东西,那么@Retryable()就不起作用了。

pom.xml中的maven依赖

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.5.RELEASE</version>
    </dependency>

Spring启动Application.java

@SpringBootApplication
@EnableTransactionManagement
@EnableRetry
public class Application 

     public static void main(String[] args) throws Exception 
       SpringApplication.run(Application.class, args);
     


在controller.java中

@RestController
public class JavaAllDataTypeController 

@Autowired
JavaAllDataTypeService JavaAllDataTypeService;


@RequestMapping(
        value = "/springReTryTest",
        method = RequestMethod.GET
)
public ResponseEntity<String> springReTryTest() 

    System.out.println("springReTryTest controller");

    try 
         JavaAllDataTypeService.springReTryTest();
     catch (Exception e) 
        e.printStackTrace();
    

    return new  ResponseEntity<String>("abcd", HttpStatus.OK);
  


在service.java中

@Service
@Transactional
public class JavaAllDataTypeService 

 // try the method 9 times with 2 seconds delay.
 @Retryable(maxAttempts=9,value=Exception.class,backoff=@Backoff(delay = 2000))
 public void springReTryTest() throws Exception 

    System.out.println("try!");
    throw new Exception();
  


输出:它'尝试9次然后抛出异常。

enter image description here

另一答案

它也适用于返回类型

@Service
public class RetryService 

private int count = 0;

// try the method 9 times with 2 seconds delay.
@Retryable(maxAttempts = 9, value = Exception.class, backoff = @Backoff(delay = 2000))
public String springReTryTest() throws Exception 
    count++;
    System.out.println("try!");

    if (count < 4)
        throw new Exception();
    else
        return "bla";
  


另一答案

我有与原始问题中描述的完全相同的问题。

在我的情况下,事实证明spring-boot-starter-aop依赖意外不包括在内。将它添加到我的pom.xml后,我的@Retryable方法按预期工作。

@Retryable方法返回值对我来说很好。

另一答案

另一种选择可能是RetryTemplate

@Bean
    public RetryTemplate retryTemplate() 
        RetryTemplate retryTemplate = new RetryTemplate();

        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(2000l);
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(2);
        retryTemplate.setRetryPolicy(retryPolicy);

        return retryTemplate;
    

retryTemplate.execute(new RetryCallback<Void, RuntimeException>() 
    @Override
    public Void doWithRetry(RetryContext arg0) 
        myService.templateRetryService();
        ...
    
);

为我工作了

source

以上是关于Spring Boot @retryable没有重试的主要内容,如果未能解决你的问题,请参考以下文章

spring-retry失败重试

spring-retry失败重试

spring 重试机制 retryable

spring 重试机制 retryable

Spring-Retry 没有从 HikariCP 获得新的数据库连接

java笔记SpringBoot中的@Retryable重试注解