Springboot 整合Retry 实现重试机制,java架构师课程推荐

然后创建一个测试重试的TestRetryService.java:

/**

  • @Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

public interface TestRetryService {

int dignifiedTest(int code) throws Exception;

}

然后是TestRetryServiceImpl.java:

import com.mail.elegant.service.TestRetryService;

import org.springframework.retry.annotation.Backoff;

import org.springframework.retry.annotation.Recover;

import org.springframework.retry.annotation.Retryable;

import org.springframework.stereotype.Service;

import java.time.LocalTime;

/**

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

@Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

@Service

public class TestRetryServiceImpl implements TestRetryService {

@Override

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

public int dignifiedTest(int code) throws Exception{

System.out.println(“dignifiedTest被调用,时间:”+LocalTime.now());

if (code==0){

throw new Exception(“情况不对头!”);

}

System.out.println(“dignifiedTest被调用,情况对头了!”);

return 200;

}

@Recover

public int recover(Exception e){

System.out.println(“回调方法执行!!!!”);

//记日志到数据库 或者调用其余的方法

return 400;

}

}

到这里,已经整合完毕,最后剩下测试了,在测试前,我们先一起来看看代码里面的关键信息的意义:

可以看到代码里面,这个方法上面加上了注解 ,

@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5,maxDelay=360000L))

@Retryable : 注解方式标记当前方法会使用重试机制

里面的 value: 重试的触发机制,当遇到Exception异常的时候,触发;

maxAttempts: 重试的次数(包括第一次调用,也就是说如果设置3次,调用一次后,如果一直失败触发重试,那么还当前方法还会调用2次);

delay:重试的延迟时间,也就是距离上一次重试方法调用的间隔,单位毫秒

multiplier: delay间隔时间的倍数,也就是说,第一次重试间隔如果是2000ms,那第二次重试的时候就是2000ms 乘以这个倍数1.5,就是3000ms;

maxDelay:重试次数之间的最大时间间隔,默认为0,即忽略,如果小于delay的设置,则默认为30000L;

再来看下面的这个小方法:

@Recover

public int recover(Exception e){

System.out.println(“回调方法执行!!!!”);

//记日志到数据库 或者调用其余的方法

return 400;

}

这个方法用到了**@Recover**,也就是用注解方式标记当期方法为回调方法,可以看到传参里面写的是 Exception e,这个是作为回调的接头暗号(重试次数用完了,还是失败,我们抛出这个Exception e通知触发这个回调方法)。

PS:该回调方法与重试方法写在同一个实现类里面。

然后在启动类加上开启重试注解:


@EnableRetry

好了,基本简单讲解完毕,接下来测试看看什么效果,

创建一个TestController.java ,写过简单的测试方法:

import com.mail.elegant.service.TestRetryService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

/**

  • @Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

@RestController

public class TestController {

@Autowired

TestRetryService testRetryServiceImpl;

@GetMapping("/testRetry")

public String testRetry() throws Exception {

int code=0;

int result = testRetryServiceImpl.dignifiedTest(code);

return “result:”+result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值