Skip to content

Commit

Permalink
degrade重构
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Apr 30, 2022
1 parent a7c7a94 commit 9005d6b
Show file tree
Hide file tree
Showing 20 changed files with 508 additions and 1,364 deletions.
852 changes: 0 additions & 852 deletions README_EN.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser;
import com.github.lianjiatech.retrofit.spring.boot.degrade.DegradeType;

/**
Expand All @@ -17,17 +15,11 @@ public class DegradeProperty {
private boolean enable = false;

/**
* 熔断降级类型,暂时只支持SENTINEL
* degrade type, Only SENTINEL is currently supported
* 熔断降级类型
* degrade type
*/
private DegradeType degradeType = DegradeType.SENTINEL;

/**
* 资源名称解析器
* resource name parser
*/
private Class<? extends BaseResourceNameParser> resourceNameParser = DefaultResourceNameParser.class;


public boolean isEnable() {
return enable;
Expand All @@ -44,12 +36,4 @@ public DegradeType getDegradeType() {
public void setDegradeType(DegradeType degradeType) {
this.degradeType = degradeType;
}

public Class<? extends BaseResourceNameParser> getResourceNameParser() {
return resourceNameParser;
}

public void setResourceNameParser(Class<? extends BaseResourceNameParser> resourceNameParser) {
this.resourceNameParser = resourceNameParser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
Expand All @@ -28,7 +30,10 @@
import com.github.lianjiatech.retrofit.spring.boot.core.PrototypeInterceptorBdfProcessor;
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.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.interceptor.GlobalAndNetworkInterceptorFinder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
Expand All @@ -49,11 +54,14 @@ public class RetrofitAutoConfiguration implements ApplicationContextAware {

private static final Logger logger = LoggerFactory.getLogger(RetrofitAutoConfiguration.class);

@Autowired
private RetrofitProperties retrofitProperties;
private final RetrofitProperties retrofitProperties;

private ApplicationContext applicationContext;

public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) {
this.retrofitProperties = retrofitProperties;
}

@Configuration
public static class RetrofitProcessorAutoConfiguration {

Expand All @@ -70,7 +78,9 @@ public GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder() {

@Bean
@ConditionalOnMissingBean
public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, InstantiationException {
public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) ResourceNameParser resourceNameParser,
@Autowired(required = false) DegradeInterceptor degradeInterceptor)
throws IllegalAccessException, InstantiationException {
RetrofitConfigBean retrofitConfigBean =
new RetrofitConfigBean(retrofitProperties, globalAndNetworkInterceptorFinder());
// Initialize the connection pool
Expand All @@ -80,24 +90,28 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
pool.forEach((poolName, poolConfig) -> {
long keepAliveSecond = poolConfig.getKeepAliveSecond();
int maxIdleConnections = poolConfig.getMaxIdleConnections();
ConnectionPool connectionPool = new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS);
ConnectionPool connectionPool =
new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS);
poolRegistry.put(poolName, connectionPool);
});
}
retrofitConfigBean.setPoolRegistry(poolRegistry);

// callAdapterFactory
Class<? extends CallAdapter.Factory>[] globalCallAdapterFactories = retrofitProperties.getGlobalCallAdapterFactories();
Class<? extends CallAdapter.Factory>[] globalCallAdapterFactories =
retrofitProperties.getGlobalCallAdapterFactories();
retrofitConfigBean.setGlobalCallAdapterFactoryClasses(globalCallAdapterFactories);

// converterFactory
Class<? extends Converter.Factory>[] globalConverterFactories = retrofitProperties.getGlobalConverterFactories();
Class<? extends Converter.Factory>[] globalConverterFactories =
retrofitProperties.getGlobalConverterFactories();
retrofitConfigBean.setGlobalConverterFactoryClasses(globalConverterFactories);

// retryInterceptor
RetryProperty retry = retrofitProperties.getRetry();
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
BaseRetryInterceptor retryInterceptorInstance = ApplicationContextUtils.getBean(applicationContext, retryInterceptor);
BaseRetryInterceptor retryInterceptorInstance =
ApplicationContextUtils.getBeanOrNull(applicationContext, retryInterceptor);
if (retryInterceptorInstance == null) {
retryInterceptorInstance = retryInterceptor.newInstance();
}
Expand All @@ -112,17 +126,29 @@ public RetrofitConfigBean retrofitConfigBean() throws IllegalAccessException, In
serviceInstanceChooser = new NoValidServiceInstanceChooser();
}

ServiceInstanceChooserInterceptor serviceInstanceChooserInterceptor = new ServiceInstanceChooserInterceptor(serviceInstanceChooser);
ServiceInstanceChooserInterceptor serviceInstanceChooserInterceptor =
new ServiceInstanceChooserInterceptor(serviceInstanceChooser);
retrofitConfigBean.setServiceInstanceChooserInterceptor(serviceInstanceChooserInterceptor);

// resource name parser
DegradeProperty degrade = retrofitProperties.getDegrade();
Class<? extends BaseResourceNameParser> resourceNameParser = degrade.getResourceNameParser();
retrofitConfigBean.setResourceNameParser(resourceNameParser.newInstance());

retrofitConfigBean.setResourceNameParser(resourceNameParser);
retrofitConfigBean.setDegradeInterceptor(degradeInterceptor);
return retrofitConfigBean;
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "retrofit.degrade.enable", havingValue = "true")
public ResourceNameParser resourceNameParser() {
return new DefaultResourceNameParser();
}

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

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

import com.github.lianjiatech.retrofit.spring.boot.degrade.BaseResourceNameParser;
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.interceptor.GlobalAndNetworkInterceptorFinder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
Expand Down Expand Up @@ -35,7 +36,9 @@ public class RetrofitConfigBean {

private Class<? extends CallAdapter.Factory>[] globalCallAdapterFactoryClasses;

private BaseResourceNameParser resourceNameParser;
private ResourceNameParser resourceNameParser;

private DegradeInterceptor degradeInterceptor;

public RetrofitProperties getRetrofitProperties() {
return retrofitProperties;
Expand Down Expand Up @@ -98,11 +101,19 @@ public void setGlobalCallAdapterFactoryClasses(Class<? extends CallAdapter.Facto
this.globalCallAdapterFactoryClasses = globalCallAdapterFactoryClasses;
}

public BaseResourceNameParser getResourceNameParser() {
public ResourceNameParser getResourceNameParser() {
return resourceNameParser;
}

public void setResourceNameParser(BaseResourceNameParser resourceNameParser) {
public void setResourceNameParser(ResourceNameParser resourceNameParser) {
this.resourceNameParser = resourceNameParser;
}

public DegradeInterceptor getDegradeInterceptor() {
return degradeInterceptor;
}

public void setDegradeInterceptor(DegradeInterceptor degradeInterceptor) {
this.degradeInterceptor = degradeInterceptor;
}
}
Loading

0 comments on commit 9005d6b

Please sign in to comment.