快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

spring-retry简单使用方法

在分布式系统中,为了保证数据分布式事务的强一致性,大家在调用RPC接口或者发送MQ时,针对可能会出现网络抖动请求超时情况采取一下重试操作。大家用的最多的重试方式就是MQ了,但是如果你的项目中没有引入MQ,那就不方便了,本文主要介绍一下如何使用Spring Retry实现重试操作。

创新互联服务项目包括青山网站建设、青山网站制作、青山网页制作以及青山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,青山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到青山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

1. 添加maven依赖

 
  org.springframework.retry 
  spring-retry 
  1.1.2.RELEASE 
 
 
  org.aspectj 
  aspectjweaver 
  1.5.4 
 

2. 在启动里添加重试配置

@SpringBootApplication 
@EnableRetry 
public class Application { 
 
  public static void main(String[] args) { 
    SpringApplication.run(Application.class, args); 
  } 
 
} 

3. 编写Service

@Service 
public class RemoteService { 
 
  private static final Logger logger = LoggerFactory.getLogger(TestController.class); 
 
  @Retryable(value= {BusinessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 2)) 
  public void call() throws Exception { 
    logger.info("do something..."); 
    throw new BusinessException("RPC调用异常"); 
  } 
  @Recover 
  public void recover(BusinessException e) { 
    logger.info(" --------------------------- "); 
    logger.info(e.getMessage()); 
  } 
} 

4. 编写Controller

@RestController 
@RequestMapping("/test") 
public class TestController { 
 
  private static final Logger logger = LoggerFactory.getLogger(TestController.class); 
 
  @Autowired 
  private RemoteService remoteService; 
 
  @RequestMapping("/test") 
  public String login() throws Exception { 
    remoteService.call(); 
    return String.valueOf("11"); 
  } 

5. 访问http://localhost:8080/test/test

6. 测试日志

2017-07-25 19:28:07 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something... 
2017-07-25 19:28:12 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something... 
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something... 
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:24)]  ---------------------------   
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:25)] RPC调用异常 

7. 相关配置说明

@EnableRetry能否重试当proxyTargetClass属性为true时,使用CGLIB代理。默认使用标准JAVA注解。在spring Boot中此参数写在程序入口即可。

@Retryable 标注此注解的方法在发生异常时会进行重试
            value:指定处理的异常类

            include:指定处理的异常类和value一样,默认为空,当exclude也为空时,默认所有异常

            exclude:指定异常不处理,默认空,当include也为空时,默认所有异常

            maxAttempts:最大重试次数。默认3次

            backoff: 重试等待策略。默认使用@Backoff注解

@Backoff 重试等待策略
            不设置参数时,默认使用FixedBackOffPolicy(指定等待时间),重试等待1000ms

            设置delay,使用FixedBackOffPolicy(指定等待时间),重试等待填写的时间

            设置delay和maxDealy时,重试等待在这两个值之间均态分布

            设置delay、maxDealy、multiplier,使用 ExponentialBackOffPolicy(指数级重试间隔的实现 ),multiplier即指定延迟倍数,比如delay=5000l,multiplier=2,则第一次重试为5秒,第二次为10秒,第三次为20秒……

@Recover 用于@Retryable重试失败后处理方法,此注解注释的方法参数一定要是@Retryable抛出的异常,否则无法识别,可以在该方法中进行日志处理。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前题目:spring-retry简单使用方法
URL标题:http://6mz.cn/article/pechjc.html

其他资讯