Skip to content

Commit

Permalink
fix sentinel degrade bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Mar 21, 2023
1 parent 30894f3 commit 1aad782
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion 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>3.0.1</version>
<version>3.0.2</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,7 +51,6 @@ public boolean isEnableDegrade(Class<?> retrofitInterface) {
@Override
public void loadDegradeRules(Class<?> retrofitInterface) {
Method[] methods = retrofitInterface.getMethods();
List<DegradeRule> rules = new ArrayList<>();
for (Method method : methods) {
if (isDefaultOrStatic(method)) {
continue;
Expand All @@ -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) {
Expand Down Expand Up @@ -114,5 +112,4 @@ public Response intercept(Chain chain) throws IOException {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@
*/
@SpringBootTest(classes = RetrofitTestApplication.class)
@RunWith(SpringRunner.class)
@ActiveProfiles("sentinel")
public class CustomOkHttpTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,6 +13,7 @@
* @author 陈添明
*/
@RetrofitClient(baseUrl = "${test.baseUrl}", sourceOkHttpClient = "testSourceOkHttpClient")
@SentinelDegrade
public interface CustomOkHttpTestApi {

@GET("person")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ public interface HttpApi {
* 其它任意POJO类型: 将响应体内容适配成一个对应的POJO类型对象返回,如果http状态码不是2xx,直接抛错!
*/
@GET("person")
@SentinelDegrade
Result<Person> getPerson(@Query("id") Long id);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down

0 comments on commit 1aad782

Please sign in to comment.