您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spring重試支持Spring Retry的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
第一步、引入maven依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry --> <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.8.6</version> </dependency>
第二步、添加@Retryable和@Recover注解
package hello; import org.springframework.remoting.RemoteAccessException; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; @Service public class RemoteService { @Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1)) public void call() throws Exception { System.out.println("do something..."); throw new RemoteAccessException("RPC調用異常"); } @Recover public void recover(RemoteAccessException e) { System.out.println(e.getMessage()); } }
@Retryable注解
被注解的方法發生異常時會重試
value:指定發生的異常進行重試
include:和value一樣,默認空,當exclude也為空時,所有異常都重試
exclude:指定異常不重試,默認空,當include也為空時,所有異常都重試
maxAttemps:重試次數,默認3
backoff:重試補償機制,默認沒有
@Backoff注解
delay:指定延遲后重試
multiplier:指定延遲的倍數,比如delay=5000l,multiplier=2時,第一次重試為5秒后,第二次為10秒,第三次為20秒
@Recover
當重試到達指定次數時,被注解的方法將被回調,可以在該方法中進行日志處理。需要注意的是發生的異常和入參類型一致時才會回調
第三步、SpringBoot方式啟動容器、測試
添加@EnableRetry注解,啟用重試功能
package hello; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication @EnableRetry public class Application { public static void main(String[] args) throws Exception { ApplicationContext annotationContext = new AnnotationConfigApplicationContext("hello"); RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class); remoteService.call(); } }
運行結果:
16:50:51.012 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something…
16:50:51.025 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:50:56.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something…
16:50:56.026 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:51:01.026 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something…
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:51:01.027 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC調用異常
關于“Spring重試支持Spring Retry的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。