1.  场景

 
     系统方法调用时无状态的,同时因为网络原因,或者系统暂时故障,进行的重试
 
2. maven 依赖
 
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.dalong.spring.io</groupId>
  5. <artifactId>springretrydemo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>1.4.2.RELEASE</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.retry</groupId>
  15. <artifactId>spring-retry</artifactId>
  16. <version>1.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.apache.geronimo.bundles</groupId>
  25. <artifactId>aspectjweaver</artifactId>
  26. <version>1.6.8_2</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. <plugin>
  40. <artifactId>maven-compiler-plugin</artifactId>
  41. <version>3.1</version><!--$NO-MVN-MAN-VER$ -->
  42. <configuration>
  43. <source>1.8</source>
  44. <target>1.8</target>
  45. </configuration>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>
3. 定义 service  并标注retry 策略
 
     注解驱动,方法驱动
 

  1. @Service
  2. publicclassFirstService{
  3. @Retryable(value=RemoteAccessException.class,maxAttempts=3,backoff=@Backoff(delay =5000l,multiplier =1))
  4. publicString service(String value){
  5. // ... do something
  6. System.out.println("do something...");
  7. if(null==value||"".equals(value)){
  8. thrownewRemoteAccessException("RPC调用异常");
  9. }
  10. else{
  11. return"dalong demo info";
  12. }
  13. }
  14. @Recover
  15. publicString recover(RemoteAccessException e){
  16. // ... panic
  17. System.out.println(e.getMessage());
  18. System.out.println("rong method");
  19. return e.getMessage();
  20. }
  21. }
  使用retrytemplate
 

  1. @Service
  2. publicclassMyService2{
  3. @Autowired
  4. publicUserOperator userOperator;
  5. publicUserInfo getUserinfo()throwsTimeoutException{
  6. RetryTemplate template =newRetryTemplate();
  7. TimeoutRetryPolicy policy =newTimeoutRetryPolicy();
  8. policy.setTimeout(1000L);
  9. template.setRetryPolicy(policy);
  10. UserInfo result = template.execute(newRetryCallback<UserInfo,TimeoutException>(){
  11. @Override
  12. publicUserInfo doWithRetry(RetryContext context)throwsTimeoutException{
  13. // TODO Auto-generated method stub
  14. UserInfo info =null;
  15. info = userOperator.getUserinfo();
  16. return info;
  17. }
  18. },newRecoveryCallback<UserInfo>(){
  19. @Override
  20. publicUserInfo recover(RetryContext context)throwsException{
  21. // TODO Auto-generated method stub
  22. UserInfo inf =newUserInfo();
  23. inf.setDate(newDate());
  24. inf.setAge(333);
  25. inf.setInfo("default");
  26. return inf;
  27. }
  28. });
  29. return result;
  30. }
  31. }
UserOperator   服务

  1. @Service
  2. publicclassUserOperator{
  3. publicUserInfo getUserinfo()throwsTimeoutException{
  4. UserInfo info =newUserInfo();
  5. info.setDate(newDate());
  6. info.setAge(333);
  7. info.setInfo("dddddd");
  8. try{
  9. Thread.sleep(2000);
  10. }catch(InterruptedException e){
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. thrownewTimeoutException("timeout");
  14. }
  15. return info;
  16. }
  17. }
4. rest api  调用
 

  1. @Autowired
  2. publicMyService2 myService2;
  3. @RequestMapping(value ="/user", method =RequestMethod.GET)
  4. publicObject getuser()throwsTimeoutException{
  5. UserInfo info =null;
  6. info = myService2.getUserinfo();
  7. return info;
  8. // return "this is demo";
  9. }
5. 类似的解决方案
 
    netflix 公司的hystrix ,目前spring  cloud 已经进行了集成封装,也可以单独进行使用。
 
6. 重试策略的说明
 
   
  1. org.springframework.retry.policy.SimpleRetryPolicy 
    该策略定义了对指定的异常进行若干次重试。默认情况下,对Exception异常及其子类重试3次。如果创建SimpleRetryPolicy并指定重试异常map,可以选择性重试或不进行重试。下面的代码定义了对TimeOutException进行重试
  2. org.springframework.retry.policy.NeverRetryPolicy 
    执行一次待执行操作,若出现异常后不进行重试。
  3. org.springframework.retry.policy.AlwaysRetryPolicy 
    异常后一直重试直到成功。
  4. org.springframework.retry.policy.TimeoutRetryPolicy 
    在执行execute方法时从open操作开始到调用TimeoutRetryPolicy的canRetry方法这之间所经过的时间。这段时间未超过TimeoutRetryPolicy定义的超时时间,那么执行操作,否则抛出异常。
  5. org.springframework.retry.policy.ExceptionClassifierRetryPolicy

    根据产生的异常选择重试策略。

  6. org.springframework.retry.policy.CompositeRetryPolicy 
    用户指定一组策略,随后根据optimistic选项来确认如何重试。

 

spring retry 使用的更多相关文章

  1. Spring retry基本使用

    Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...

  2. 自己动手实践 spring retry 重试框架

    前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...

  3. Spring Retry

    最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...

  4. Spring retry实践

    在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...

  5. Spring异常重试框架Spring Retry

    Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...

  6. 【spring】spring retry介绍

    一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...

  7. 异常重试框架Spring Retry实践

    前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...

  8. Spring框架中一个有用的小组件:Spring Retry

    1.概述 Spring Retry 是Spring框架中的一个组件, 它提供了自动重新调用失败操作的能力.这在错误可能是暂时发生的(如瞬时网络故障)的情况下很有帮助. 在本文中,我们将看到使用Spri ...

  9. Spring Retry 在SpringBoot 中的应用

    Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...

  10. Spring Retry 重试

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...

随机推荐

  1. 第十章 嵌入式Linux的调试技术

    对调试工具进行简介.Linux中提供了一类工具,通过这些工具可以逐行跟踪程序的代码,用于测试用户空间程序的gdb.gdbserver和调试内核空间程序的kgdb. 用gdb调试用户空间程序:gdb可跟 ...

  2. 调试 zeromq 发现 accept 死循环

    起因:在群里一个同学说使用 zeromq 的时候出了点儿问题,问题描述如下“router连接十几万客户端后,然后把router杀死,重启,这时候zeromq的某个线程99%的cpu,卡死了,再也接受不 ...

  3. Unity中游戏的声音管理

    using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 用 ...

  4. utf8转gbk,libcurl中文乱码处理

    这两个转码在网页客户端处理用很常见,所使用的平台为VS2010,字符集采用多字节字符集 utf8转gbk string UTF8ToGBK(const std::string& strUTF8 ...

  5. 使用OpenCV&&C++进行模板匹配.

    一:课程介绍 1.1:学习目标 学会用imread载入图像,和imshow输出图像. 用nameWindow创建窗口,用createTrackbar加入滚动条和其回调函数的写法. 熟悉OpenCV函数 ...

  6. Longest Increasing Path in a Matrix -- LeetCode 329

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  7. vi & vim复制,粘贴,剪切文本

    我经常用vi编辑器,但基本上还是windows的习惯,没有系统的学过其功能,今天遇到了文本的复制这没有办法了,查看一下解决如下: 引用文本: ----------------------------- ...

  8. Android ——单元测试

    什么是单元测试 首先需要介绍一下什么是单元测试.很多人像我一样,本科并不是计算机专业出身的,如果在职的公司不要求做单元测试的话,可能对这个词并没有一个确切的概念.而即使是计算机专业出身,如果毕业以后写 ...

  9. 用GO扫描图片像素,复制图片

    关键是使用image.image/png.image/color包 // main.go package main import ( "fmt" "bufio" ...

  10. 写给笨蛋徒弟的学习手册(1)——完整C#项目中各个文件含义

    Bin 目录用来存放编译的结果,bin是二进制binrary的英文缩写,因为最初C编译的程序文件都是二进制文件,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin/R ...