springboot整合spring-retry的實現示例
本系統調用外圍系統接口(http+json),但是發現有時外圍系統服務不太穩定,有時候會出現返回一串xml或者gateway bad的信息,導致調用失敗,基于這一原因,采用基于springboot,整合spring-retry的重試機制到系統工程中,demo已經放到github上。
2、解決方案簡要說明:demo工程基于springboot,為了方便驗證,采用swagger進行測試驗證。
2.1 pom文件<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.laowang</groupId> <artifactId>springretry</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springretry</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!--retry--><dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId></dependency><!--swagger--><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration> <excludes><exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId></exclude> </excludes></configuration> </plugin></plugins> </build></project>
重點說明:aop的gav必須有,否則會跑不起來。
<!--retry--><dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId></dependency>2.2 applicat啟動類
package com.laowang.springretry;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.retry.annotation.EnableRetry;import springfox.documentation.swagger2.annotations.EnableSwagger2;@EnableRetry@EnableSwagger2@SpringBootApplicationpublic class SpringretryApplication { public static void main(String[] args) {SpringApplication.run(SpringretryApplication.class, args); }}
說明:兩個標簽而已
@EnableRetry@EnableSwagger22.3 controller類
/** * @description: TODO * @author Administrator * @date 2021/6/2 14:55 * @version 1.0 */package com.laowang.springretry.controller;import com.laowang.springretry.service.RetryService;import io.swagger.annotations.Api;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@Api('重試測試類')@RestControllerpublic class RetryController { @Autowired RetryService retryService; @GetMapping('/testRetry') public String testRetry(int code) throws Exception {int result = retryService.retryTest(code);return 'result:' + result; }}2.4 service測試類(重點)
/** * @description: TODO * @author Administrator * @date 2021/6/2 12:23 * @version 1.0 */package com.laowang.springretry.service;import org.springframework.retry.annotation.Backoff;import org.springframework.retry.annotation.Recover;import org.springframework.retry.annotation.Retryable;import org.springframework.stereotype.Service;import java.time.LocalTime;@Servicepublic class RetryServiceImpl implements RetryService { @Override @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5)) public int retryTest(int code) throws Exception {System.out.println('retryTest被調用,時間:' + LocalTime.now());if (code == 0) { throw new Exception('異常拋出!');}System.out.println('retryTest被調用,情況對頭了!');return 200; } @Recover public int recover(Exception e) {System.out.println('回調方法執行,可以記錄日志到數據庫!!??!');//記日志到數據庫 或者調用其余的方法return 400; }}
**說明:**三個標簽
@Retryable注解被注解的方法發生異常時會重試value:指定發生的異常進行重試include:和value一樣,默認空,當exclude也為空時,所有異常都重試exclude:指定異常不重試,默認空,當include也為空時,所有異常都重試maxAttemps:重試次數,默認3backoff:重試補償機制,默認沒有
@Backoff注解說明delay:指定延遲后重試multiplier:指定延遲的倍數,比如delay=2000,multiplier=1.5時,第二次重試與第一次執行間隔:2秒;第三次重試與第二次重試間隔:3秒;第四次重試與第三次重試間隔:4.5秒。。。
@Recover當重試到達指定次數時,被注解的方法將被回調,可以在該方法中進行日志處理。需要注意的是發生的異常和入參類型一致時才會回調
2.5 項目啟動執行運行application,啟動成功,默認端口號:8080
(1)swagger訪問地址:
http://localhost:8080/swagger-ui.html
(2)先驗證成功返回
先測試正常調用試試,code=1
調用返回:
(3)重試機制:code=0(重點)
為了更好的說明問題,參數配置增大一些:
@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 2))
執行效果
說明:
從執行效果看,總共執行5次,第二次跟第一次之間是2秒;第三次跟第二次之間是2*2=4秒;第四次與第三次之間是:2 乘以2乘以2=8秒,第五次與第四次之間是:2 乘以2乘以2乘以2=16秒,符合預期。
執行完成后,進入 @Recover標簽內容,可以進行日志記錄,以便后續定位問題。
github項目地址:https://github.com/ruanjianlaowang/springretry
到此這篇關于springboot整合spring-retry的實現示例的文章就介紹到這了,更多相關springboot整合spring-retry內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. React+umi+typeScript創建項目的過程2. ASP中常用的22個FSO文件操作函數整理3. ASP編碼必備的8條原則4. ASP調用WebService轉化成JSON數據,附json.min.asp5. 三個不常見的 HTML5 實用新特性簡介6. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析7. SharePoint Server 2019新特性介紹8. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析10. php測試程序運行速度和頁面執行速度的代碼
