在調用第三方接口或者使用mq時,會出現網絡抖動,連接超時等網絡異常,所以需要重試。為了使處理更加健壯並且不太容易出現故障,后續的嘗試操作,有時候會幫助失敗的操作最后執行成功。例如,由於網絡故障或數據庫更新中的DeadLockLoserException導致Web服務或RMI服務的遠程調用可能會在短暫等待后自行解決。 為了自動執行這些操作的重試,Spring Batch具有RetryOperations策略。不過該重試功能從Spring Batch 2.2.0版本中獨立出來,變成了Spring Retry模塊。
引入依賴
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency>
需要引入Spring-retry和aspectjweaver的依賴。
入口類
@SpringBootApplication @EnableRetry public class SpringbootRetryApplication { public static void main(String[] args) { SpringApplication.run(SpringbootRetryApplication.class, args); } }
入口類上開啟retry的攔截,使用@EnableRetry
注解。
Service
@Service public class PayService { private Logger logger = LoggerFactory.getLogger(getClass()); private final int totalNum = 100000; @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 1.5)) public int minGoodsnum(int num) throws Exception { logger.info("減庫存開始" + LocalTime.now()); try { int i = 1 / 0; } catch (Exception e) { logger.error("illegal"); } if (num <= 0) { throw new IllegalArgumentException("數量不對"); } logger.info("減庫存執行結束" + LocalTime.now()); return totalNum - num; } }
@Retryable
的參數說明:
- value:拋出指定異常才會重試
- include:和value一樣,默認為空,當exclude也為空時,默認所以異常
- exclude:指定不處理的異常
- maxAttempts:最大重試次數,默認3次
- backoff:重試等待策略,默認使用
@Backoff
,@Backoff
的value默認為1000L,我們設置為2000L;multiplier(指定延遲倍數)默認為0,表示固定暫停1秒后進行重試,如果把multiplier設置為1.5,則第一次重試為2秒,第二次為3秒,第三次為4.5秒。
測試類
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootRetryApplicationTests { @Autowired private PayService payService; @Test public void payTest() throws Exception { int store = payService.minGoodsnum(-1); System.out.println("庫存為:" + store); } }
運行的控制台結果如下:

可以看到,三次之后拋出了IllegalArgumentException
異常。
當重試耗盡時,RetryOperations可以將控制傳遞給另一個回調,即RecoveryCallback。Spring-Retry還提供了@Recover注解,用於@Retryable重試失敗后處理方法,此方法里的異常一定要是@Retryable方法里拋出的異常,否則不會調用這個方法。
@Recover public int recover(Exception e) { logger.warn("減庫存失敗!!!" + LocalTime.now()); return totalNum; }
在Service中,加上如上的方法之后,進行測試。

可以看到當三次重試執行完之后,會調用Recovery方法,也不會再次拋出異常。
總結
本文主要講了在Spring Boot項目中的Spring-Retry簡單應用,主要是基於注解配置一些重試的策略,使用比較簡單。主要的適用場景為在調用第三方接口或者使用mq時。由於會出現網絡抖動,連接超時等網絡異常,這時就需要重試。
原文鏈接:https://www.jianshu.com/p/cc7abf831900