Эх сурвалжийг харах

扩展core-retrofit增加retry参数status-code,默认配置408,503,504均需要重试,可以通过配置文件自定义修改

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 жил өмнө
parent
commit
2c9a1fe3e4

+ 14 - 0
core-retrofit/src/main/java/com/qmth/boot/core/retrofit/config/RetryProperties.java

@@ -4,6 +4,9 @@ import org.springframework.validation.annotation.Validated;
 
 import javax.validation.constraints.NotNull;
 import java.time.Duration;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
 @Validated
 public class RetryProperties {
@@ -14,6 +17,9 @@ public class RetryProperties {
     @NotNull
     private Duration interval = Duration.ofMillis(300);
 
+    @NotNull
+    private Set<Integer> statusCode = new HashSet<>(Arrays.asList(408, 503, 504));
+
     public Integer getCount() {
         return count;
     }
@@ -29,4 +35,12 @@ public class RetryProperties {
     public void setInterval(Duration interval) {
         this.interval = interval;
     }
+
+    public Set<Integer> getStatusCode() {
+        return statusCode;
+    }
+
+    public void setStatusCode(Set<Integer> statusCode) {
+        this.statusCode = statusCode;
+    }
 }

+ 1 - 1
core-retrofit/src/main/java/com/qmth/boot/core/retrofit/interceptor/RetryInterceptor.java

@@ -28,7 +28,7 @@ public class RetryInterceptor implements Interceptor {
     }
 
     private boolean needRetry(Response response, int count) {
-        if (response.isSuccessful() || response.code() != 503) {
+        if (response.isSuccessful() || !retryProperties.getStatusCode().contains(response.code())) {
             return false;
         }
         return count < retryProperties.getCount();