Skip to content

Commit

Permalink
RetrofitDegrade
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 1, 2022
1 parent 9ce0ba5 commit 3803fe9
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 131 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<junit.version>4.13.1</junit.version>
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
<slf4j-api.version>1.7.25</slf4j-api.version>
<spring-boot.version>2.0.0.RELEASE</spring-boot.version>
<spring-boot.version>2.0.9.RELEASE</spring-boot.version>
<okhttp3.version>3.14.9</okhttp3.version>
<okio.version>1.17.5</okio.version>
<sentinel.version>1.6.3</sentinel.version>
Expand All @@ -59,7 +59,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>${spring-boot.version}</version>
<version>2.0.4.RELEASE</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -166,6 +166,12 @@
<version>${sentinel.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;

import lombok.Data;

Expand All @@ -11,7 +11,7 @@
public class DegradeProperty {

/**
* 启用熔断降级
* 是否启用熔断降级
* enable degrade
*/
private boolean enable = false;
Expand All @@ -20,5 +20,5 @@ public class DegradeProperty {
* 熔断降级类型
* degrade type
*/
private DegradeType degradeType = DegradeType.SENTINEL;
private String degradeType = RetrofitDegrade.NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
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.DefaultResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.degrade.ResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegradeInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelRetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
Expand Down Expand Up @@ -70,7 +70,7 @@ public static PathMatchInterceptorBdfProcessor prototypeInterceptorBdfProcessor(
@Bean
@ConditionalOnMissingBean
public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) ResourceNameParser resourceNameParser,
@Autowired(required = false) DegradeInterceptor degradeInterceptor,
@Autowired(required = false) RetrofitDegrade retrofitDegrade,
@Autowired(required = false) List<GlobalInterceptor> globalInterceptors,
@Autowired(required = false) List<NetworkInterceptor> networkInterceptors,
ServiceChooseInterceptor serviceChooseInterceptor, RetryInterceptor retryInterceptor,
Expand All @@ -80,7 +80,7 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour
retrofitConfigBean.setGlobalInterceptors(globalInterceptors);
retrofitConfigBean.setNetworkInterceptors(networkInterceptors);
retrofitConfigBean.setResourceNameParser(resourceNameParser);
retrofitConfigBean.setDegradeInterceptor(degradeInterceptor);
retrofitConfigBean.setRetrofitDegrade(retrofitDegrade);
retrofitConfigBean.setServiceChooseInterceptor(serviceChooseInterceptor);
retrofitConfigBean.setRetryInterceptor(retryInterceptor);
retrofitConfigBean.setLoggingInterceptor(loggingInterceptor);
Expand Down Expand Up @@ -159,10 +159,10 @@ public ResourceNameParser resourceNameParser() {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "retrofit.degrade.degrade-type", havingValue = "sentinel")
@ConditionalOnProperty(name = "retrofit.degrade.degrade-type", havingValue = RetrofitDegrade.SENTINEL)
@ConditionalOnBean(ResourceNameParser.class)
public DegradeInterceptor degradeInterceptor(ResourceNameParser resourceNameParser) {
return new SentinelDegradeInterceptor(resourceNameParser);
public RetrofitDegrade retrofitDegrade(ResourceNameParser resourceNameParser) {
return new SentinelRetrofitDegrade(resourceNameParser);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Map;

import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.degrade.ResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ErrorDecoderInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
Expand Down Expand Up @@ -42,7 +42,7 @@ public class RetrofitConfigBean {

private ResourceNameParser resourceNameParser;

private DegradeInterceptor degradeInterceptor;
private RetrofitDegrade retrofitDegrade;

private LoggingInterceptor loggingInterceptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
import org.springframework.util.Assert;
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.config.DegradeProperty;
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.DegradeProxy;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;
import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegrade;
import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.Intercept;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.InterceptMark;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.Intercepts;
import com.github.lianjiatech.retrofit.spring.boot.util.AnnotationExtendUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.AppContextUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.BeanExtendUtils;
import com.github.lianjiatech.retrofit.spring.boot.util.RetrofitUtils;
Expand Down Expand Up @@ -66,53 +62,22 @@ public RetrofitFactoryBean(Class<T> retrofitInterface) {
@Override
public T getObject() throws Exception {
T source = createRetrofit().create(retrofitInterface);
if (!isEnableSentinelDegrade(retrofitProperties.getDegrade(), retrofitInterface)) {
if (!isEnableDegrade(retrofitProperties.getDegrade(), retrofitInterface)) {
return source;
}
// 启用代理
loadDegradeRules();
retrofitConfigBean.getRetrofitDegrade().loadDegradeRules(retrofitInterface);
return DegradeProxy.create(source, retrofitInterface, applicationContext);
}

public boolean isEnableSentinelDegrade(DegradeProperty degradeProperty, Class<?> retrofitInterface) {
public boolean isEnableDegrade(DegradeProperty degradeProperty, Class<?> retrofitInterface) {
if (!degradeProperty.isEnable()) {
return false;
}
return AnnotationExtendUtils.isAnnotationPresent(retrofitInterface, SentinelDegrade.class);
}

private void loadDegradeRules() {
if (retrofitProperties.getDegrade().getDegradeType() == DegradeType.SENTINEL) {
loadSentinelDegradeRules();
}
}

private void loadSentinelDegradeRules() {
// 读取熔断配置
Method[] methods = retrofitInterface.getMethods();
List<DegradeRule> rules = new ArrayList<>();
for (Method method : methods) {
if (method.isDefault()) {
continue;
}
int modifiers = method.getModifiers();
if (Modifier.isStatic(modifiers)) {
continue;
}
// 获取熔断配置
SentinelDegrade sentinelDegrade = AnnotationExtendUtils.findAnnotation(method, SentinelDegrade.class);
if (sentinelDegrade == null) {
continue;
}

DegradeRule degradeRule = new DegradeRule()
.setCount(sentinelDegrade.count())
.setTimeWindow(sentinelDegrade.timeWindow())
.setGrade(sentinelDegrade.grade());
degradeRule.setResource(retrofitConfigBean.getResourceNameParser().extractResourceNameCache(method));
rules.add(degradeRule);
RetrofitDegrade retrofitDegrade = retrofitConfigBean.getRetrofitDegrade();
if (retrofitDegrade == null) {
return false;
}
DegradeRuleManager.loadRules(rules);
return retrofitDegrade.isEnableDegrade(retrofitInterface);
}

@Override
Expand Down Expand Up @@ -140,8 +105,8 @@ private OkHttpClient createOkHttpClient()
throws IllegalAccessException, InstantiationException, InvocationTargetException {
OkHttpClient.Builder okHttpClientBuilder = createOkHttpClientBuilder();
RetrofitClient retrofitClient = retrofitInterface.getAnnotation(RetrofitClient.class);
if (isEnableSentinelDegrade(retrofitProperties.getDegrade(), retrofitInterface)) {
okHttpClientBuilder.addInterceptor(retrofitConfigBean.getDegradeInterceptor());
if (isEnableDegrade(retrofitProperties.getDegrade(), retrofitInterface)) {
okHttpClientBuilder.addInterceptor(retrofitConfigBean.getRetrofitDegrade());
}
if (StringUtils.hasText(retrofitClient.serviceId())) {
okHttpClientBuilder.addInterceptor(retrofitConfigBean.getServiceChooseInterceptor());
Expand Down

This file was deleted.

This file was deleted.

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

import okhttp3.Interceptor;

/**
* @author 陈添明
* @since 2022/4/30 3:34 下午
*/
public interface RetrofitDegrade extends Interceptor {

String NONE = "none";
String SENTINEL = "sentinel";

/**
* 对于指定Retrofit接口,是否允许降级
* @param retrofitInterface Retrofit接口
* @return 是否允许降级
*/
boolean isEnableDegrade(Class<?> retrofitInterface);

/**
* 加载指定Retrofit接口下的降级规则
* @param retrofitInterface Retrofit接口
*/
void loadDegradeRules(Class<?> retrofitInterface);

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

import java.io.IOException;

import com.github.lianjiatech.retrofit.spring.boot.degrade.RetrofitDegrade;

import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import okhttp3.Response;

/**
* @author 陈添明
* @since 2022/5/1 8:02 下午
*/
public class Release4jRetrofitDegrade implements RetrofitDegrade {

@Override
public boolean isEnableDegrade(Class<?> retrofitInterface) {
return false;
}

@Override
public void loadDegradeRules(Class<?> retrofitInterface) {
CircuitBreakerRegistry registry = new CircuitBreakerRegistry.Builder()
.build();
}

@Override
public Response intercept(Chain chain) throws IOException {
return null;
}
}

This file was deleted.

Loading

0 comments on commit 3803fe9

Please sign in to comment.