溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring-retry簡單使用方法

發(fā)布時(shí)間:2020-10-20 01:36:42 來源:腳本之家 閱讀:171 作者:積木A 欄目:編程語言

在分布式系統(tǒng)中,為了保證數(shù)據(jù)分布式事務(wù)的強(qiáng)一致性,大家在調(diào)用RPC接口或者發(fā)送MQ時(shí),針對(duì)可能會(huì)出現(xiàn)網(wǎng)絡(luò)抖動(dòng)請(qǐng)求超時(shí)情況采取一下重試操作。大家用的最多的重試方式就是MQ了,但是如果你的項(xiàng)目中沒有引入MQ,那就不方便了,本文主要介紹一下如何使用Spring Retry實(shí)現(xiàn)重試操作。

1. 添加maven依賴

<dependency> 
  <groupId>org.springframework.retry</groupId> 
  <artifactId>spring-retry</artifactId> 
  <version>1.1.2.RELEASE</version> 
</dependency> 
<dependency> 
  <groupId>org.aspectj</groupId> 
  <artifactId>aspectjweaver</artifactId> 
  <version>1.5.4</version> 
</dependency> 

2. 在啟動(dòng)里添加重試配置

@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調(diào)用異常"); 
  } 
  @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. 測(cè)試日志

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調(diào)用異常 

7. 相關(guān)配置說明

@EnableRetry能否重試。當(dāng)proxyTargetClass屬性為true時(shí),使用CGLIB代理。默認(rèn)使用標(biāo)準(zhǔn)JAVA注解。在spring Boot中此參數(shù)寫在程序入口即可。

@Retryable 標(biāo)注此注解的方法在發(fā)生異常時(shí)會(huì)進(jìn)行重試
            value:指定處理的異常類

            include:指定處理的異常類和value一樣,默認(rèn)為空,當(dāng)exclude也為空時(shí),默認(rèn)所有異常

            exclude:指定異常不處理,默認(rèn)空,當(dāng)include也為空時(shí),默認(rèn)所有異常

            maxAttempts:最大重試次數(shù)。默認(rèn)3次

            backoff: 重試等待策略。默認(rèn)使用@Backoff注解

@Backoff 重試等待策略
            不設(shè)置參數(shù)時(shí),默認(rèn)使用FixedBackOffPolicy(指定等待時(shí)間),重試等待1000ms

            設(shè)置delay,使用FixedBackOffPolicy(指定等待時(shí)間),重試等待填寫的時(shí)間

            設(shè)置delay和maxDealy時(shí),重試等待在這兩個(gè)值之間均態(tài)分布

            設(shè)置delay、maxDealy、multiplier,使用 ExponentialBackOffPolicy(指數(shù)級(jí)重試間隔的實(shí)現(xiàn) ),multiplier即指定延遲倍數(shù),比如delay=5000l,multiplier=2,則第一次重試為5秒,第二次為10秒,第三次為20秒……

@Recover 用于@Retryable重試失敗后處理方法,此注解注釋的方法參數(shù)一定要是@Retryable拋出的異常,否則無法識(shí)別,可以在該方法中進(jìn)行日志處理。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI