Skip to content

Commit

Permalink
many refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 1, 2022
1 parent 9ca3901 commit 9ce0ba5
Show file tree
Hide file tree
Showing 45 changed files with 306 additions and 357 deletions.
4 changes: 2 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.1.1.RELEASE</spring-boot.version>
<spring-boot.version>2.0.0.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>2.1.6.RELEASE</version>
<version>${spring-boot.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.BeansException;
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;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand All @@ -24,8 +20,12 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.core.AutoConfiguredRetrofitScannerRegistrar;
import com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory;
import com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory;
import com.github.lianjiatech.retrofit.spring.boot.core.DefaultErrorDecoder;
import com.github.lianjiatech.retrofit.spring.boot.core.NoValidServiceInstanceChooser;
import com.github.lianjiatech.retrofit.spring.boot.core.PathMatchInterceptorBdfProcessor;
import com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory;
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;
Expand All @@ -50,12 +50,10 @@
@EnableConfigurationProperties(RetrofitProperties.class)
@AutoConfigureAfter({JacksonAutoConfiguration.class})
@Slf4j
public class RetrofitAutoConfiguration implements ApplicationContextAware {
public class RetrofitAutoConfiguration {

private final RetrofitProperties retrofitProperties;

private ApplicationContext applicationContext;

public RetrofitAutoConfiguration(RetrofitProperties retrofitProperties) {
this.retrofitProperties = retrofitProperties;
}
Expand Down Expand Up @@ -102,6 +100,26 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour
return retrofitConfigBean;
}

@Bean
public BodyCallAdapterFactory bodyCallAdapterFactory() {
return new BodyCallAdapterFactory();
}

@Bean
public ResponseCallAdapterFactory responseCallAdapterFactory() {
return new ResponseCallAdapterFactory();
}

@Bean
public BasicTypeConverterFactory basicTypeConverterFactory() {
return new BasicTypeConverterFactory();
}

@Bean
public DefaultErrorDecoder defaultErrorDecoder() {
return new DefaultErrorDecoder();
}

@Bean
@ConditionalOnMissingBean
public ErrorDecoderInterceptor errorDecoderInterceptor() {
Expand Down Expand Up @@ -149,32 +167,16 @@ public DegradeInterceptor degradeInterceptor(ResourceNameParser resourceNamePars

@Bean
@ConditionalOnMissingBean
public ObjectMapper jacksonObjectMapper() {
return new ObjectMapper()
public JacksonConverterFactory jacksonConverterFactory() {
ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

@Bean
@ConditionalOnMissingBean
@Autowired
public JacksonConverterFactory jacksonConverterFactory(ObjectMapper objectMapper) {
return JacksonConverterFactory.create(objectMapper);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

@Configuration
@Import({AutoConfiguredRetrofitScannerRegistrar.class})
@ConditionalOnMissingBean(RetrofitFactoryBean.class)
public static class RetrofitScannerRegistrarNotFoundConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() {
log.debug("No {} found.", RetrofitFactoryBean.class.getName());
}
}
public static class RetrofitScannerRegistrarNotFoundConfiguration {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public class RetrofitProperties {
@NestedConfigurationProperty
private LogProperty log = new LogProperty();

/**
* Disable Void return type
*/
private boolean disableVoidReturnType = false;

/**
* 全局连接超时时间
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.lianjiatech.retrofit.spring.boot.core;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeansException;
Expand All @@ -17,7 +18,7 @@

/**
* This will just scan the same base package as Spring Boot does. If you want more power, you can explicitly use
* {@link com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitScan} but this will get typed mappers working correctly, out-of-the-box,
* {@link RetrofitScan} but this will get typed mappers working correctly, out-of-the-box,
* similar to using Spring Data JPA repositories.
*
* @author 陈添明
Expand All @@ -40,24 +41,19 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!AutoConfigurationPackages.has(this.beanFactory)) {
log.debug("Could not determine auto-configuration package, automatic retrofit scanning disabled.");
log.warn("Could not determine auto-configuration package, automatic retrofit scanning disabled.");
return;
}

log.debug("Searching for retrofits annotated with @RetrofitClient");

List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (log.isDebugEnabled()) {
packages.forEach(pkg -> log.debug("Using auto-configuration base package '{}'", pkg));
}

// Scan the @RetrofitClient annotated interface under the specified path and register it to the
// BeanDefinitionRegistry
ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader);
if (resourceLoader != null) {
scanner.setResourceLoader(resourceLoader);
}
String[] packageArr = packages.toArray(new String[0]);
log.info("Scan the @RetrofitClient annotated interface using the auto-configuration package. packages={}",
Arrays.toString(packageArr));
scanner.registerFilters();
// Scan and register to BeanDefinition
scanner.doScan(packageArr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import retrofit2.Retrofit;

/**
* 同步调用,如果返回的http状态码是是成功,返回responseBody 反序列化之后的对象。否则,抛出异常!异常信息中包含请求和响应相关信息。
* 同步调用,如果返回的http状态码是是成功,返回responseBody 反序列化之后的对象。否则,抛出异常!异常信息中包含请求和响应相关信息。<br></>
* Synchronous call, if the returned http status code is successful, return the responseBody object after deserialization.
* Otherwise, throw an exception! The exception information includes request and response related information.
*
Expand All @@ -48,16 +48,16 @@ public final class BodyCallAdapterFactory extends CallAdapter.Factory {
if (Response.class.isAssignableFrom(getRawType(returnType))) {
return null;
}
return new BodyCallAdapter(returnType, annotations, retrofit);
return new BodyCallAdapter<>(returnType, annotations, retrofit);
}

final class BodyCallAdapter<R> implements CallAdapter<R, R> {
static final class BodyCallAdapter<R> implements CallAdapter<R, R> {

private Type returnType;
private final Type returnType;

private Retrofit retrofit;
private final Retrofit retrofit;

private Annotation[] annotations;
private final Annotation[] annotations;

BodyCallAdapter(Type returnType, Annotation[] annotations, Retrofit retrofit) {
this.returnType = returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;

import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;

import lombok.extern.slf4j.Slf4j;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
*
* @author 陈添明
*/
public class DefaultErrorDecoder implements ErrorDecoder {

}
public class DefaultErrorDecoder implements ErrorDecoder {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;
package com.github.lianjiatech.retrofit.spring.boot.core;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import retrofit2.Retrofit;

/**
* <p>
* 同步调用执行,直接返回 #{@link Response} 对象
* 同步调用执行,直接返回 #{@link Response} 对象。<br/>
* Synchronous call execution, directly return #{@link Response} object
* </p>
*
* @author 陈添明
*/
Expand All @@ -41,7 +39,7 @@ public final class ResponseCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
if (Response.class.isAssignableFrom(getRawType(returnType))) {
return new ResponseCallAdapter(returnType);
return new ResponseCallAdapter<>(returnType);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.github.lianjiatech.retrofit.spring.boot.annotation;
package com.github.lianjiatech.retrofit.spring.boot.core;

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 retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
Expand Down Expand Up @@ -66,7 +63,6 @@
*/
Class<?> fallback() default void.class;


/**
* Define a fallback factory for the specified Feign client interface. The fallback
* factory must produce instances of fallback classes that implement the interface
Expand All @@ -75,19 +71,9 @@
*/
Class<?> fallbackFactory() default void.class;


/**
* When calling {@link Retrofit#create(Class)} on the resulting {@link Retrofit} instance, eagerly validate the
* configuration of all methods in the supplied interface.
*
* @return validateEagerly
*/
boolean validateEagerly() default false;

/**
* 当前接口采用的错误解码器,当请求发生异常或者收到无效响应结果的时候,将HTTP相关信息解码到异常中,无效响应由业务自己判断。
* 一般情况下,每个服务对应的无效响应各不相同,可以自定义对应的{@link ErrorDecoder},然后配置在这里。
* <p>
* 一般情况下,每个服务对应的无效响应各不相同,可以自定义对应的{@link ErrorDecoder},然后配置在这里。<br></>
* The error decoder used in the current interface will decode HTTP related information into the exception when an exception occurs in the request or an invalid response result is received.
* The invalid response is determined by the business itself.
* In general, the invalid response corresponding to each service is different, you can customize the corresponding {@link ErrorDecoder}, and then configure it here.
Expand All @@ -103,6 +89,14 @@
*/
String poolName() default "default";

/**
* When calling {@link Retrofit#create(Class)} on the resulting {@link Retrofit} instance, eagerly validate the
* configuration of all methods in the supplied interface.
*
* @return validateEagerly
*/
boolean validateEagerly() default false;

/**
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
Expand Down Expand Up @@ -130,7 +124,6 @@
*/
int writeTimeoutMs() default -1;


/**
* Sets the default timeout for complete calls. A value of 0 means no timeout,
* otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
Expand All @@ -148,7 +141,6 @@
*/
int pingIntervalMs() default 0;


/**
* Configure this client to allow protocol redirects from HTTPS to HTTP and from HTTP to HTTPS.
* Redirects are still first restricted by followRedirects. Defaults to true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;

import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitScan;
import lombok.extern.slf4j.Slf4j;

/**
* @author 陈添明
*/
@Slf4j
public class RetrofitClientScannerRegistrar
implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware {

Expand All @@ -42,6 +43,8 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR
}
// Specify the base package for scanning
String[] basePackages = getPackagesToScan(attributes);
log.info("Scan the @RetrofitClient annotated interface using the @RetrofitScan configuration. packages={}",
Arrays.toString(basePackages));
scanner.registerFilters();
// Scan and register to BeanDefinition
scanner.doScan(basePackages);
Expand Down
Loading

0 comments on commit 9ce0ba5

Please sign in to comment.