Skip to content

Commit

Permalink
same Interceptor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Apr 30, 2022
1 parent 3ac4650 commit 9ca3901
Show file tree
Hide file tree
Showing 68 changed files with 514 additions and 800 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ retrofit:
- response_status_not_2xx
- occur_io_exception
# 重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor

# 熔断降级配置
degrade:
Expand Down Expand Up @@ -487,7 +487,7 @@ retrofit:
global-retry-rules:
- response_status_not_2xx
# 重试拦截器
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor
retry-interceptor: com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor
```

**重试规则支持三种配置**:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;

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

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;

import java.lang.annotation.*;

/**
* 自动将注解上的参数值赋值到handleInterceptor实例上
* Automatically assign the parameter value on the annotation to the handleInterceptor instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;

import java.lang.annotation.*;
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)
@Documented
public @interface OkHttpClientBuilder {
}
public @interface OkHttpClientBuilder {}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;

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;

import com.github.lianjiatech.retrofit.spring.boot.core.DefaultErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.core.ErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy;
import org.springframework.stereotype.Service;

import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;

import java.lang.annotation.*;

/**
* @author 陈添明
*/
Expand Down Expand Up @@ -74,33 +76,6 @@
Class<?> fallbackFactory() default void.class;


/**
* 针对当前接口是否启用日志打印
* Whether to enable log printing for the current interface
*
* @return enableLog
*/
boolean enableLog() default true;

/**
* 日志打印级别,支持的日志级别参见{@link LogLevel}
* 如果为NULL,则取全局日志打印级别
* Log printing level, see {@link LogLevel} for supported log levels
*
* @return logLevel
*/
LogLevel logLevel() default LogLevel.NULL;

/**
* 日志打印策略,支持的日志打印策略参见{@link LogStrategy}
* 如果为NULL,则取全局日志打印策略
* Log printing strategy, see {@link LogStrategy} for supported log printing strategies
*
* @return logStrategy
*/
LogStrategy logStrategy() default LogStrategy.NULL;


/**
* When calling {@link Retrofit#create(Class)} on the resulting {@link Retrofit} instance, eagerly validate the
* configuration of all methods in the supplied interface.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClientScannerRegistrar;
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;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClientScannerRegistrar;

/**
* @author 陈添明
Expand All @@ -22,15 +27,13 @@
*/
String[] value() default {};


/**
* Scan package path
*
* @return basePackages
*/
String[] basePackages() default {};


/**
* Scan package classes
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseLoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.DefaultLoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy;
import com.github.lianjiatech.retrofit.spring.boot.log.LogLevel;
import com.github.lianjiatech.retrofit.spring.boot.log.LogStrategy;

import lombok.Data;

Expand All @@ -14,10 +12,10 @@
public class LogProperty {

/**
* 启用日志打印
* 是否启用全局日志打印
* Enable log printing
*/
private boolean enable = true;
private boolean enableGlobalLog = true;

/**
* 全局日志打印级别,支持的日志级别参见{@link LogLevel}
Expand All @@ -30,10 +28,4 @@ public class LogProperty {
* Log printing strategy, see {@link LogStrategy} for supported log printing strategies
*/
private LogStrategy globalLogStrategy = LogStrategy.BASIC;

/**
* 日志打印拦截器
* Log print Interceptor
*/
private Class<? extends BaseLoggingInterceptor> loggingInterceptor = DefaultLoggingInterceptor.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,23 +25,22 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.core.AutoConfiguredRetrofitScannerRegistrar;
import com.github.lianjiatech.retrofit.spring.boot.core.NoValidServiceInstanceChooser;
import com.github.lianjiatech.retrofit.spring.boot.core.PrototypeInterceptorBdfProcessor;
import com.github.lianjiatech.retrofit.spring.boot.core.PathMatchInterceptorBdfProcessor;
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.interceptor.ErrorDecoderInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.util.ApplicationContextUtils;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceChooseInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor;

import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
Expand All @@ -66,8 +64,8 @@ public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) {
public static class RetrofitProcessorAutoConfiguration {

@Bean
public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor() {
return new PrototypeInterceptorBdfProcessor();
public static PathMatchInterceptorBdfProcessor prototypeInterceptorBdfProcessor() {
return new PathMatchInterceptorBdfProcessor();
}
}

Expand All @@ -76,13 +74,19 @@ public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor(
public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) ResourceNameParser resourceNameParser,
@Autowired(required = false) DegradeInterceptor degradeInterceptor,
@Autowired(required = false) List<GlobalInterceptor> globalInterceptors,
@Autowired(required = false) List<NetworkInterceptor> networkInterceptors)
throws IllegalAccessException, InstantiationException {
@Autowired(required = false) List<NetworkInterceptor> networkInterceptors,
ServiceChooseInterceptor serviceChooseInterceptor, RetryInterceptor retryInterceptor,
LoggingInterceptor loggingInterceptor, ErrorDecoderInterceptor errorDecoderInterceptor) {

RetrofitConfigBean retrofitConfigBean = new RetrofitConfigBean(retrofitProperties);
retrofitConfigBean.setGlobalInterceptors(globalInterceptors);
retrofitConfigBean.setNetworkInterceptors(networkInterceptors);
retrofitConfigBean.setResourceNameParser(resourceNameParser);
retrofitConfigBean.setDegradeInterceptor(degradeInterceptor);
retrofitConfigBean.setServiceChooseInterceptor(serviceChooseInterceptor);
retrofitConfigBean.setRetryInterceptor(retryInterceptor);
retrofitConfigBean.setLoggingInterceptor(loggingInterceptor);
retrofitConfigBean.setErrorDecoderInterceptor(errorDecoderInterceptor);

Map<String, ConnectionPool> poolRegistry = new HashMap<>(4);
retrofitProperties.getPool().forEach((poolName, poolConfig) -> {
Expand All @@ -93,39 +97,39 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour
poolRegistry.put(poolName, connectionPool);
});
retrofitConfigBean.setPoolRegistry(poolRegistry);
retrofitConfigBean.setGlobalCallAdapterFactoryClasses(retrofitProperties.getGlobalCallAdapterFactories());
retrofitConfigBean.setGlobalConverterFactoryClasses(retrofitProperties.getGlobalConverterFactories());
return retrofitConfigBean;
}

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

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

// retryInterceptor
RetryProperty retry = retrofitProperties.getRetry();
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
BaseRetryInterceptor retryInterceptorInstance =
ApplicationContextUtils.getBeanOrNew(applicationContext, retryInterceptor);
BeanUtils.copyProperties(retry, retryInterceptorInstance);
retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance);

// add ServiceInstanceChooserInterceptor
ServiceInstanceChooser serviceInstanceChooser;
try {
serviceInstanceChooser = applicationContext.getBean(ServiceInstanceChooser.class);
} catch (BeansException e) {
serviceInstanceChooser = new NoValidServiceInstanceChooser();
}
@Bean
@ConditionalOnMissingBean
public ErrorDecoderInterceptor errorDecoderInterceptor() {
return new ErrorDecoderInterceptor();
}

@Bean
@ConditionalOnMissingBean
public RetryInterceptor retryInterceptor() {
return new RetryInterceptor(retrofitProperties.getRetry());
}

ServiceInstanceChooserInterceptor serviceInstanceChooserInterceptor =
new ServiceInstanceChooserInterceptor(serviceInstanceChooser);
retrofitConfigBean.setServiceInstanceChooserInterceptor(serviceInstanceChooserInterceptor);
@Bean
@ConditionalOnMissingBean
public LoggingInterceptor logInterceptor() {
return new LoggingInterceptor(retrofitProperties.getLog());
}

@Bean
@ConditionalOnMissingBean
public ServiceInstanceChooser serviceInstanceChooser() {
return new NoValidServiceInstanceChooser();
}

return retrofitConfigBean;
@Bean
@ConditionalOnMissingBean
ServiceChooseInterceptor serviceChooseInterceptor(@Autowired ServiceInstanceChooser serviceInstanceChooser) {
return new ServiceChooseInterceptor(serviceInstanceChooser);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

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.ErrorDecoderInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.GlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceInstanceChooserInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.ServiceChooseInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryInterceptor;

import lombok.Data;
import okhttp3.ConnectionPool;
Expand All @@ -30,9 +32,9 @@ public class RetrofitConfigBean {

private List<NetworkInterceptor> networkInterceptors;

private BaseRetryInterceptor retryInterceptor;
private RetryInterceptor retryInterceptor;

private ServiceInstanceChooserInterceptor serviceInstanceChooserInterceptor;
private ServiceChooseInterceptor serviceChooseInterceptor;

private Class<? extends Converter.Factory>[] globalConverterFactoryClasses;

Expand All @@ -42,6 +44,10 @@ public class RetrofitConfigBean {

private DegradeInterceptor degradeInterceptor;

private LoggingInterceptor loggingInterceptor;

private ErrorDecoderInterceptor errorDecoderInterceptor;

public RetrofitConfigBean(RetrofitProperties retrofitProperties) {
this.retrofitProperties = retrofitProperties;
}
Expand Down
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.retry.BaseRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.DefaultRetryInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.retry.RetryRule;

import lombok.Data;
Expand All @@ -20,8 +18,8 @@ public class RetryProperty {
private boolean enableGlobalRetry = false;

/**
* 全局最大重试次数,最大可设置为100
* The maximum number of retries, the maximum can be set to 100
* 全局最大重试次数
* The maximum number of retries
*/
private int globalMaxRetries = 2;

Expand All @@ -37,9 +35,4 @@ public class RetryProperty {
*/

private RetryRule[] globalRetryRules = {RetryRule.RESPONSE_STATUS_NOT_2XX, RetryRule.OCCUR_IO_EXCEPTION};

/**
* retry interceptor
*/
private Class<? extends BaseRetryInterceptor> retryInterceptor = DefaultRetryInterceptor.class;
}
Loading

0 comments on commit 9ca3901

Please sign in to comment.