背景:

在项目开发中,有时候会出现接口调用失败,本身调用又是异步的,如果是因为一些网络问题请求超时,总想可以重试几次把任务处理掉。

一些RPC框架,比如dubbo都是有重试机制的,但是并不是每一个项目多会使用dubbo框架,常规的小项目有时候直接使用http进行不同项目之间的交互。

个人想法:

使用spring aop和自定义注解来,建立一套重试机制。

根据切入点和自定义注解,来完成重试工作。

exps:

定义一个注解:

 import org.springframework.stereotype.Component;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Component
public @interface RetryProcess {
//重试的次数
int value() default 1;
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicInteger; @Aspect
@Component
public class AspectExceptionInterceptor {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@AfterThrowing(pointcut=("execution(* com.tom.plus.ctl..*(..)) && @annotation(com.tom.plus.compent.RetryProcess)"))
public void tryAgain(JoinPoint point) {
logger.info("------------开始重试------------");
try {
Object object = point.getTarget();
Field field = object.getClass().getDeclaredField("threadLocal");
field.setAccessible(true);
ThreadLocal<AtomicInteger> threadLocal = (ThreadLocal<AtomicInteger>) field.get(object);
MethodSignature methodSignature = (MethodSignature) point.getSignature();
RetryProcess retryProcess = methodSignature.getMethod().getAnnotation(RetryProcess.class);
if (threadLocal.get().intValue() < retryProcess.value()) {
int index = threadLocal.get().incrementAndGet();
logger.info("开始重试第"+index);
MethodInvocationProceedingJoinPoint methodPoint = ((MethodInvocationProceedingJoinPoint) point);
methodPoint.proceed();
}
} catch (Throwable throwable) {
//logger.error("重试失败",throwable);
tryAgain(point);
}
}
}

测试代码:

@RetryProcess(value = 2)
@RequestMapping("/hero")
@ResponseBody
public String doIt2() {
//该接口会抛出异常,启动进行重试机制
testService.doProcess();
return "success";
}

spring boot下接口调用失败重试方案的更多相关文章

  1. Spring Boot 配置文件密码加密两种方案

    Spring Boot 配置文件密码加密两种方案 jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中.可以快速集成到 Spring Boot 项 ...

  2. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  3. Spring Boot下的一种导入Excel文件的代码框架

    1.前言 ​ Spring Boot下如果只是导入一个简单的Excel文件,是容易的.网上类似的文章不少,有的针对具体的实体类,代码可重用性不高:有的利用反射机制或自定义注解,开发了Excel导入工具 ...

  4. Spring Boot下Druid连接池+mybatis

      目前Spring Boot中默认支持的连接池有dbcp,dbcp2, hikari三种连接池.  引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连 ...

  5. 【ActiveMQ】2.spring Boot下使用ActiveMQ

    在spring boot下使用ActiveMQ,需要一下几个条件 1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.ht ...

  6. spring boot下使用logback或log4j生成符合Logstash标准的JSON格式

    spring boot下使用logback或log4j生成符合Logstash标准的JSON格式 一.依赖 由于配置中使用了json格式的日志输出,所以需要引入如下依赖 "net.logst ...

  7. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  8. 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合

    Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...

  9. 【spring boot】10.spring boot下的单元测试

    spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...

随机推荐

  1. poj 1741(树的点分治)

    Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  2. 关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案

    关于api-ms-win-crt-runtimel1-1-0.dll缺失的解决方案 目录 关于api-ms-win-crt-runtimel1-1-0dll缺失的解决方案 目录 安装VC redite ...

  3. iOS手势识别

    一.手势识别与触摸事件 1.如果想监听一个view上面的触摸事件,可选的做法是: (1)自定义一个view (2)实现view的touches方法,在方法内部实现具体处理代码 2.通过touches方 ...

  4. python 13:数字列表统计方法(min(list)、max(list)、sum(list))

    numbers = list(range(1,11)) print(numbers) print(min(numbers)) #获得列表最小值 print(max(numbers)) #获得列表最大值 ...

  5. Linux安装java jdk、mysql、tomcat

    安装javajdk 1.8 检查是否安装 rpm -qa | grep jdk rpm方式安装 下载java1.8 jdk http://download.oracle.com/otn-pub/jav ...

  6. php全局变量 超全局变量

    php中有许多超全局变量,这意味着它们在一个脚本的全部作用域中都可用.在函数或方法中无需执行 global $variable; 就可以访问它们. 这些超全局变量是: $GLOBALS    引用全局 ...

  7. 教材配套PPT初稿

    1-10章初稿,基本完整.有些粗糙,后面可能会稍作调整. 附更新情况如下: 1.增加了第10章内容: 2.第5章增加了一些内容: 3.第3章内容部分更新: 4.增加了第8-9章内容. 订正:更新了第8 ...

  8. Windows Live Writer 历史Blog修改的功能

    其实 WLW 有历史Blog修改的功能,我只是一直没有找到,就在打开“最近发布的日志”里面, 位于屏幕的右侧“打开”列表下. 最近发现记忆力越来越差了,BLOG看来是必须的了.

  9. Scala类型系统——高级类类型(higher-kinded types)

    高级类类型就是使用其他类型构造成为一个新的类型,因此也称为 类型构造器(type constructors).它的语法和高阶函数(higher-order functions)相似,高阶函数就是将其它 ...

  10. Coreldraw软件反盗版提示x8有优惠活动 cdr x8提示盗版怎么办?

    CorelDRAW X8装不上,我的悲伤有这么大,或者比这还大一点...♥♥♥如果你遇到这样的断了网,卸了装,装了卸,然后再安装的...╮(-_-)╭这样的保存和另存为都点不了,不敢关电脑的亦或是这样 ...