当前位置: 移动技术网 > IT编程>开发语言>Java > Spring重试支持Spring Retry的方法

Spring重试支持Spring Retry的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文介绍了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调用异常

参考 :

补充

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网