diff --git a/README.md b/README.md index da809a7..1bf4194 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.18 + 2.2.19 ``` @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 2.2.18 + 2.2.19 com.squareup.okhttp3 diff --git a/README_EN.md b/README_EN.md index 1a6e005..a4d9e8e 100644 --- a/README_EN.md +++ b/README_EN.md @@ -43,7 +43,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.18 + 2.2.19 ``` @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions. com.github.lianjiatech retrofit-spring-boot-starter - 2.2.18 + 2.2.19 com.squareup.okhttp3 diff --git a/pom.xml b/pom.xml index 986dfa2..d151134 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 2.2.18 + 2.2.19 retrofit-spring-boot-starter retrofit-spring-boot-starter @@ -237,4 +237,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java index 2129f4f..d6b10b6 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/BaseRetryInterceptor.java @@ -35,20 +35,42 @@ public Response intercept(Chain chain) throws IOException { // 获取重试配置 Retry retry = getRetry(method); - // 没有@Retry 且未开启全局重试 - if (retry == null && !enableGlobalRetry) { + if (!needRetry(retry)) { return chain.proceed(request); } + // 重试 int maxRetries = retry == null ? globalMaxRetries : retry.maxRetries(); int intervalMs = retry == null ? globalIntervalMs : retry.intervalMs(); RetryRule[] retryRules = retry == null ? globalRetryRules : retry.retryRules(); - // 最多重试10次 + // 最多重试100次 maxRetries = Math.min(maxRetries, LIMIT_RETRIES); return retryIntercept(maxRetries, intervalMs, retryRules, chain); } + private boolean needRetry(Retry retry) { + + if (enableGlobalRetry) { + // 开启全局重试的情况下 + // 没配置@Retry,需要重试 + if (retry == null) { + return true; + } + // 配置了@Retry,enable==true,需要重试 + if (retry.enable()) { + return true; + } + } else { + // 未开启全局重试 + // 配置了@Retry,enable==true,需要重试 + if (retry != null && retry.enable()) { + return true; + } + } + return false; + } + private Retry getRetry(Method method) { Retry retry; if (method.isAnnotationPresent(Retry.class)) { diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/Retry.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/Retry.java index 9c3705d..c797fbe 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/Retry.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/retry/Retry.java @@ -10,6 +10,13 @@ @Documented public @interface Retry { + /** + * 是否启用重试 + * + * @return 是否启用重试 + */ + boolean enable() default true; + /** * 最大重试次数,最大可设置为100 * The maximum number of retries, the maximum can be set to 100 diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java index 14ecb5e..8e0cec6 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi.java @@ -2,6 +2,7 @@ import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercept; import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.retry.Retry; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; import com.github.lianjiatech.retrofit.spring.boot.test.ex.TestErrorDecoder; @@ -104,6 +105,7 @@ public interface HttpApi { * @return . */ @POST("error") + @Retry(enable = false) Person error(@Body Person person); /** diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 0806f11..70495cb 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -69,4 +69,4 @@ retrofit: test: baseUrl: http://localhost:8080/api/test/ accessKeyId: root - accessKeySecret: 123456 \ No newline at end of file + accessKeySecret: 123456