Skip to content

Commit

Permalink
在开启全局重试的情况下,支持指定某些接口手动关闭重试
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Jan 19, 2022
1 parent 26900fb commit f55eaab
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.18</version>
<version>2.2.19</version>
</dependency>
```

Expand All @@ -67,7 +67,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.18</version>
<version>2.2.19</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.18</version>
<version>2.2.19</version>
</dependency>
```

Expand All @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.18</version>
<version>2.2.19</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.18</version>
<version>2.2.19</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down Expand Up @@ -237,4 +237,4 @@

</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,6 +105,7 @@ public interface HttpApi {
* @return .
*/
@POST("error")
@Retry(enable = false)
Person error(@Body Person person);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ retrofit:
test:
baseUrl: http://localhost:8080/api/test/
accessKeyId: root
accessKeySecret: 123456
accessKeySecret: 123456

0 comments on commit f55eaab

Please sign in to comment.