springboot整合spring-retry的实现示例

本文将结合实例代码,介绍springboot整合spring-retry的实现示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

目录

2、解决方

2.1 pom文件

2.2 applicat启动类

2.3 controller类

2.4 service测试类(重点)

2.5 项目启动

2.6 使用swagger进行验证

1、背景

本系统调用外围系统接口(http+json),但是发现有时外围系统服务不太稳定,有时候会出现返回一串xml或者gateway bad的信息,导致调用失败,基于这一原因,采用基于springboot,整合spring-retry的重试机制到系统工程中,demo已经放到github上。

2、解决方

简要说明:demo工程基于springboot,为了方便验证,采用swagger进行测试验证。

2.1 pom文件

4.0.0org.springframework.bootspring-boot-starter-parent2.5.0com.laowangspringretry0.0.1-SNAPSHOTspringretryDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.retryspring-retryorg.aspectjaspectjweaverio.springfoxspringfox-swagger22.7.0io.springfoxspringfox-swagger-ui2.7.0org.springframework.bootspring-boot-maven-pluginorg.projectlomboklombok

重点说明:aop的gav必须有,否则会跑不起来。

org.springframework.retryspring-retryorg.aspectjaspectjweaver

2.2 applicat启动类

package com.laowang.springretry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.retry.annotation.EnableRetry; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableRetry @EnableSwagger2 @SpringBootApplication public class SpringretryApplication { public static void main(String[] args) { SpringApplication.run(SpringretryApplication.class, args); } }

说明:两个标签而已

@EnableRetry @EnableSwagger2

2.3 controller类

/** * @description: Todo * @author Administrator * @date 2021/6/2 14:55 * @version 1.0 */ package com.laowang.springretry.controller; import com.laowang.springretry.service.RetryService; import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Api("重试测试类") @RestController public class RetryController { @Autowired RetryService retryService; @GetMapping("/testRetry") public String testRetry(int code) throws Exception { int result = retryService.retryTest(code); return "result:" + result; } }

2.4 service测试类(重点)

/** * @description: Todo * @author Administrator * @date 2021/6/2 12:23 * @version 1.0 */ package com.laowang.springretry.service; 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; @Service public class RetryServiceImpl implements RetryService { @Override @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5)) public int retryTest(int code) throws Exception { System.out.println("retryTest被调用,时间:" + LocalTime.Now()); if (code == 0) { throw new Exception("异常抛出!"); } System.out.println("retryTest被调用,情况对头了!"); return 200; } @Recover public int recover(Exception e) { System.out.println("回调方法执行,可以记录日志到数据库!!!!"); //记日志到数据库 或者调用其余的方法 return 400; } }

**说明:**三个标签

@Retryable注解

被注解的方法发生异常时会重试

value:指定发生的异常进行重试

include:和value一样,认空,当exclude也为空时,所有异常都重试

exclude:指定异常不重试,认空,当include也为空时,所有异常都重试

maxAttemps:重试次数认3

backoff:重试补偿机制,认没有

@Backoff注解说明

delay:指定延迟后重试

multiplier:指定延迟的倍数,比如delay=2000,multiplier=1.5时,第二次重试与第一次执行间隔:2秒;第三次重试与第二次重试间隔:3秒;第四次重试与第三次重试间隔:4.5秒。。。

@Recover

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

2.5 项目启动

执行运行application,启动成功,认端口号:8080

2.6 使用swagger进行验证

(1)swagger访问地址:

http://localhost:8080/swagger-ui.html

(2)先验证成功返回

先测试正常调用试试,code=1

调用返回

(3)重试机制:code=0(重点)

为了更好的说明问题,参数配置增大一些:

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

执行效果

说明:

从执行效果看,总共执行5次,第二次跟第一次之间是2秒;第三次跟第二次之间是2*2=4秒;第四次与第三次之间是:2 乘以2乘以2=8秒,第五次与第四次之间是:2 乘以2乘以2乘以2=16秒,符合预期。

执行完成后,进入 @Recover标签内容,可以进行日志记录,以便后续定位问题。

github项目地址:https://github.com/ruanjianlaowang/springretry

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...