当前位置: 首页 > 编程笔记 >

Spring重试支持Spring Retry的方法

欧阳鸿哲
2023-03-14
本文向大家介绍Spring重试支持Spring Retry的方法,包括了Spring重试支持Spring Retry的方法的使用技巧和注意事项,需要的朋友参考一下

本文介绍了Spring重试支持Spring Retry的方法,分享给大家,具体如下:

第一步、引入maven依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.3.RELEASE</version>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.1.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.6</version>
</dependency>

第二步、添加@Retryable和@Recover注解

package hello;

import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RemoteService {
@Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))
public void call() throws Exception {
    System.out.println("do something...");
    throw new RemoteAccessException("RPC调用异常");
}
@Recover
public void recover(RemoteAccessException e) {
    System.out.println(e.getMessage());
}
}

@Retryable注解
被注解的方法发生异常时会重试
value:指定发生的异常进行重试
include:和value一样,默认空,当exclude也为空时,所有异常都重试
exclude:指定异常不重试,默认空,当include也为空时,所有异常都重试
maxAttemps:重试次数,默认3
backoff:重试补偿机制,默认没有

@Backoff注解
delay:指定延迟后重试
multiplier:指定延迟的倍数,比如delay=5000l,multiplier=2时,第一次重试为5秒后,第二次为10秒,第三次为20秒

@Recover
当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调

第三步、SpringBoot方式启动容器、测试

添加@EnableRetry注解,启用重试功能

package hello;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.retry.annotation.EnableRetry;

@SpringBootApplication
@EnableRetry
public class Application {

  public static void main(String[] args) throws Exception {
    ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello");
    RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);
    remoteService.call();
  }
}

运行结果:

16:50:51.012 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something…
16:50:51.025 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something…
16:50:56.026 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:51:01.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something…
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC调用异常

参考 :https://github.com/spring-projects/spring-retry

补充

对于非幂等的请求(比如新增,更新操作),千万不要使用重试,对数据一致性会造成很大影响。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 我正在尝试在HTTP GET请求消息中携带JSON数据,但我的Spring MVC服务器似乎无法从GET请求正文中检索JSON数据。

  • 我正在编写一个控制器来处理来自AJAX的帖子。我一直收到一个错误,那篇文章不受支持。我以前在尝试创建后控制器方法时从未遇到过以下错误,我希望有人能发现我在哪里搞砸了。 这是我为控制控制器中的帖子而编写的方法: 使用JQuery 1.10,这是请求它的Ajax调用: 我知道POST地址是正确的,因为将它转换成GET请求就可以了。此外,我还编写了其他POST请求,它们在同一个控制器类中也能正常工作。任

  • login.jsp 用户列表.jsp AppController.java 有2页:login.jsp-起始页,其中包括与登录和密码填充的形式-userlist.jsp结果列表“显示数据库中所有用户持久化”..首先显示登录页面,当我单击提交按钮时,我得到了这个错误:org . spring framework . web . servlet . pagenotfound-不支持请求方法“POST”

  • 我试图在这里输入代码`@RequestMapping(value=“/test”,method=RequestMethod.POST),但错误代码为 网状物xml是 springmvc.xmlindex.jsp I input submit botton brower为错误HTTP Status 405-请求方法'GET'不受支持类型状态报告消息请求方法'GET'不受支持描述指定的HTTP方法不允

  • 我正在使用cucumber测试来测试我的Spring启动应用程序,启用了Spring安全。除了当我用cucumber测试一些使用Spring安全的测试时,事情运行得很好。 失败。如果我像简单的jUnit测试那样隐蔽地运行它们,这些测试确实有效,但是当使用cucumber测试步骤运行时失败了。当我用cucumber测试运行同样的测试时,这个问题看起来像是Spring安全测试模拟行为没有被应用。 我的

  • 如何解决这个问题?

  • 日志将输出以下内容。 2017-10-10 14:49:40.946警告5750---[nio-8080-exec-4]O.s.web.servlet.PageNotFound:不支持请求方法“Get”