From 1aad7823a85cc6103bdfe8b577a4eb27a9a0f0c0 Mon Sep 17 00:00:00 2001 From: chentianming Date: Tue, 21 Mar 2023 20:29:24 +0800 Subject: [PATCH] fix sentinel degrade bug --- README.md | 2 +- README_EN.md | 2 +- pom.xml | 2 +- .../degrade/sentinel/SentinelRetrofitDegrade.java | 13 +++++-------- .../boot/test/custom/okhttp/CustomOkHttpTest.java | 2 ++ .../test/custom/okhttp/CustomOkHttpTestApi.java | 2 ++ .../CustomCircuitBreakerConfigRegistrar.java | 8 ++++++++ .../spring/boot/test/degrade/DegradeR4jTest.java | 4 ++++ .../boot/test/degrade/DegradeSentinelTest.java | 8 ++++++++ .../retrofit/spring/boot/test/http/HttpApi.java | 2 ++ .../retrofit/spring/boot/test/http/HttpApi2.java | 2 ++ 11 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index de2e236..4d59460 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter] com.github.lianjiatech retrofit-spring-boot-starter - 3.0.1 + 3.0.2 ``` diff --git a/README_EN.md b/README_EN.md index c7b802d..875ced0 100644 --- a/README_EN.md +++ b/README_EN.md @@ -41,7 +41,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 3.0.1 + 3.0.2 ``` diff --git a/pom.xml b/pom.xml index 7795b3c..49377a9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.lianjiatech retrofit-spring-boot-starter - 3.0.1 + 3.0.2 retrofit-spring-boot-starter retrofit-spring-boot-starter diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelRetrofitDegrade.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelRetrofitDegrade.java index f11edb4..0b0e9bc 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelRetrofitDegrade.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/degrade/sentinel/SentinelRetrofitDegrade.java @@ -2,17 +2,16 @@ import java.io.IOException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; +import java.util.Collections; import java.util.Objects; -import com.alibaba.csp.sentinel.Tracer; import org.springframework.core.annotation.AnnotatedElementUtils; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.ResourceTypeConstants; import com.alibaba.csp.sentinel.SphU; +import com.alibaba.csp.sentinel.Tracer; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; @@ -52,7 +51,6 @@ public boolean isEnableDegrade(Class retrofitInterface) { @Override public void loadDegradeRules(Class retrofitInterface) { Method[] methods = retrofitInterface.getMethods(); - List rules = new ArrayList<>(); for (Method method : methods) { if (isDefaultOrStatic(method)) { continue; @@ -72,10 +70,10 @@ public void loadDegradeRules(Class retrofitInterface) { : sentinelDegrade.timeWindow()) .setGrade(sentinelDegrade == null ? globalSentinelDegradeProperty.getGrade() : sentinelDegrade.grade()); - degradeRule.setResource(parseResourceName(method)); - rules.add(degradeRule); + String resourceName = parseResourceName(method); + degradeRule.setResource(resourceName); + DegradeRuleManager.setRulesForResource(resourceName, Collections.singleton(degradeRule)); } - DegradeRuleManager.loadRules(rules); } protected boolean needDegrade(SentinelDegrade sentinelDegrade) { @@ -114,5 +112,4 @@ public Response intercept(Chain chain) throws IOException { } } } - } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTest.java index 01d2fdb..a50c148 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTest.java @@ -9,6 +9,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import com.github.lianjiatech.retrofit.spring.boot.test.RetrofitTestApplication; @@ -25,6 +26,7 @@ */ @SpringBootTest(classes = RetrofitTestApplication.class) @RunWith(SpringRunner.class) +@ActiveProfiles("sentinel") public class CustomOkHttpTest { @Autowired diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTestApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTestApi.java index 3c669d5..ddf6d92 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTestApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/custom/okhttp/CustomOkHttpTestApi.java @@ -1,6 +1,7 @@ package com.github.lianjiatech.retrofit.spring.boot.test.custom.okhttp; import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegrade; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; @@ -12,6 +13,7 @@ * @author 陈添明 */ @RetrofitClient(baseUrl = "${test.baseUrl}", sourceOkHttpClient = "testSourceOkHttpClient") +@SentinelDegrade public interface CustomOkHttpTestApi { @GET("person") diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/CustomCircuitBreakerConfigRegistrar.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/CustomCircuitBreakerConfigRegistrar.java index 9cc6a41..1c4e4fd 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/CustomCircuitBreakerConfigRegistrar.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/CustomCircuitBreakerConfigRegistrar.java @@ -28,5 +28,13 @@ public void register(CircuitBreakerConfigRegistry registry) { .minimumNumberOfCalls(5) .permittedNumberOfCallsInHalfOpenState(5) .build()); + + + registry.register("testCircuitBreakerConfig2", CircuitBreakerConfig.custom() + .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.TIME_BASED) + .failureRateThreshold(100) + .minimumNumberOfCalls(5000) + .permittedNumberOfCallsInHalfOpenState(5000) + .build()); } } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jTest.java index 6aa1df8..d42b1db 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeR4jTest.java @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; +import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi2; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -35,6 +36,9 @@ public class DegradeR4jTest { @Autowired private DegradeR4jApi degradeR4jApi; + @Autowired + private HttpApi2 httpApi2; + private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setSerializationInclusion(JsonInclude.Include.NON_NULL); diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java index 9be2766..9c0b26c 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java @@ -4,6 +4,8 @@ import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; +import com.github.lianjiatech.retrofit.spring.boot.test.custom.okhttp.CustomOkHttpTestApi; +import com.github.lianjiatech.retrofit.spring.boot.test.http.HttpApi; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -40,6 +42,12 @@ public class DegradeSentinelTest { @Autowired private DegradeSentinelApi degradeSentinelApi; + @Autowired + private CustomOkHttpTestApi customOkHttpTestApi; + + @Autowired + private HttpApi httpApi; + private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setSerializationInclusion(JsonInclude.Include.NON_NULL); 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 d0b8cf3..c68aab9 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 @@ -4,6 +4,7 @@ import java.util.concurrent.CompletableFuture; import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegrade; import com.github.lianjiatech.retrofit.spring.boot.interceptor.Intercept; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; @@ -41,6 +42,7 @@ public interface HttpApi { * 其它任意POJO类型: 将响应体内容适配成一个对应的POJO类型对象返回,如果http状态码不是2xx,直接抛错! */ @GET("person") + @SentinelDegrade Result getPerson(@Query("id") Long id); /** diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi2.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi2.java index ed3f811..4df9b5b 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi2.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi2.java @@ -3,6 +3,7 @@ import java.util.Map; import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.degrade.resilience4j.Resilience4jDegrade; import com.github.lianjiatech.retrofit.spring.boot.interceptor.Intercept; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; @@ -21,6 +22,7 @@ @RetrofitClient(baseUrl = "${test.baseUrl}") @Intercept(handler = TimeStampInterceptor.class, include = "/a/b", exclude = "/c/d") @EnumIntercept(envEnum = EnvEnum.test) +@Resilience4jDegrade(circuitBreakerConfigName = "testCircuitBreakerConfig2") public interface HttpApi2 { @GET("person")