Skip to content

Commit

Permalink
SentinelDegrade重构
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Apr 17, 2022
1 parent 1ff5254 commit f50cfbb
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 223 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.22</version>
<version>2.3.0</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.22</version>
<version>2.3.0</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.22</version>
<version>2.3.0</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.22</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
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>2.2.22</version>
<version>2.3.0</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 @@ -29,7 +29,6 @@
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean;
import com.github.lianjiatech.retrofit.spring.boot.core.ServiceInstanceChooser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalAndNetworkInterceptorFinder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
Expand Down Expand Up @@ -145,11 +144,6 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
this.applicationContext = applicationContext;
}

@Bean
public RetrofitDegradeRuleInitializer retrofitDegradeRuleInitializer() {
return new RetrofitDegradeRuleInitializer(retrofitProperties.getDegrade());
}

@Configuration
@Import({AutoConfiguredRetrofitScannerRegistrar.class})
@ConditionalOnMissingBean(RetrofitFactoryBean.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercept;
import com.github.lianjiatech.retrofit.spring.boot.annotation.InterceptMark;
import com.github.lianjiatech.retrofit.spring.boot.annotation.Intercepts;
Expand All @@ -36,13 +38,9 @@
import com.github.lianjiatech.retrofit.spring.boot.config.LogProperty;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitConfigBean;
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeStrategy;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRule;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegradeRuleInitializer;
import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.SentinelDegradeInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseLoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
Expand Down Expand Up @@ -123,8 +121,19 @@ public T getObject() throws Exception {
}

private void loadDegradeRules() {
DegradeProperty degrade = retrofitProperties.getDegrade();
if (!degrade.isEnable()) {
return;
}
if (degrade.getDegradeType() == DegradeType.SENTINEL) {
loadSentinelDegradeRules();
}
}

private void loadSentinelDegradeRules() {
// 读取熔断配置
Method[] methods = retrofitInterface.getMethods();
List<DegradeRule> rules = new ArrayList<>();
for (Method method : methods) {
if (method.isDefault()) {
continue;
Expand All @@ -134,28 +143,29 @@ private void loadDegradeRules() {
continue;
}
// 获取熔断配置
Degrade degrade;
if (method.isAnnotationPresent(Degrade.class)) {
degrade = method.getAnnotation(Degrade.class);
} else {
degrade = retrofitInterface.getAnnotation(Degrade.class);
}

if (degrade == null) {
SentinelDegrade sentinelDegrade = getSentinelDegrade(method);
if (sentinelDegrade == null) {
continue;
}
String resourceName = retrofitConfigBean.getResourceNameParser().parseResourceName(method, environment);
DegradeRule degradeRule = new DegradeRule()
.setCount(sentinelDegrade.count())
.setTimeWindow(sentinelDegrade.timeWindow())
.setGrade(sentinelDegrade.grade());
degradeRule.setResource(resourceName);
rules.add(degradeRule);
}
DegradeRuleManager.loadRules(rules);
}

DegradeStrategy degradeStrategy = degrade.degradeStrategy();
BaseResourceNameParser resourceNameParser = retrofitConfigBean.getResourceNameParser();
String resourceName = resourceNameParser.parseResourceName(method, environment);

RetrofitDegradeRule degradeRule = new RetrofitDegradeRule();
degradeRule.setCount(degrade.count());
degradeRule.setDegradeStrategy(degradeStrategy);
degradeRule.setTimeWindow(degrade.timeWindow());
degradeRule.setResourceName(resourceName);
RetrofitDegradeRuleInitializer.addRetrofitDegradeRule(degradeRule);
private SentinelDegrade getSentinelDegrade(Method method) {
SentinelDegrade sentinelDegrade;
if (method.isAnnotationPresent(SentinelDegrade.class)) {
sentinelDegrade = method.getAnnotation(SentinelDegrade.class);
} else {
sentinelDegrade = retrofitInterface.getAnnotation(SentinelDegrade.class);
}
return sentinelDegrade;
}

/**
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.lianjiatech.retrofit.spring.boot.degrade;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author 陈添明
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface SentinelDegrade {

/**
* RT模式下为慢调用临界 RT(超出该值计为慢调用);异常比例/异常数模式下为对应的阈值
*/
double count();

/**
* 熔断时长,单位为 s
*/
int timeWindow() default 5;

/**
* 降级策略(0:平均响应时间;1:异常比例;2:异常数量)
*/
int grade() default 0;
}
Loading

0 comments on commit f50cfbb

Please sign in to comment.