Skip to content

Commit

Permalink
lombok
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Apr 30, 2022
1 parent 9005d6b commit 3ac4650
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 534 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@
<okio.version>1.17.5</okio.version>
<sentinel.version>1.6.3</sentinel.version>
<jackson.version>2.12.6.1</jackson.version>
<lombok.version>1.18.24</lombok.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

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

import lombok.Data;

/**
* @author 陈添明
*/
@Data
public class DegradeProperty {


/**
* 启用熔断降级
* enable degrade
Expand All @@ -19,21 +21,4 @@ public class DegradeProperty {
* degrade type
*/
private DegradeType degradeType = DegradeType.SENTINEL;


public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

public DegradeType getDegradeType() {
return degradeType;
}

public void setDegradeType(DegradeType degradeType) {
this.degradeType = degradeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogLevel;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.LogStrategy;

import lombok.Data;

/**
* @author 陈添明
*/
@Data
public class LogProperty {

/**
Expand All @@ -16,7 +19,6 @@ public class LogProperty {
*/
private boolean enable = true;


/**
* 全局日志打印级别,支持的日志级别参见{@link LogLevel}
* Log printing level, see {@link LogLevel} for supported log levels
Expand All @@ -29,42 +31,9 @@ public class LogProperty {
*/
private LogStrategy globalLogStrategy = LogStrategy.BASIC;


/**
* 日志打印拦截器
* Log print Interceptor
*/
private Class<? extends BaseLoggingInterceptor> loggingInterceptor = DefaultLoggingInterceptor.class;

public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

public LogLevel getGlobalLogLevel() {
return globalLogLevel;
}

public void setGlobalLogLevel(LogLevel globalLogLevel) {
this.globalLogLevel = globalLogLevel;
}

public LogStrategy getGlobalLogStrategy() {
return globalLogStrategy;
}

public void setGlobalLogStrategy(LogStrategy globalLogStrategy) {
this.globalLogStrategy = globalLogStrategy;
}

public Class<? extends BaseLoggingInterceptor> getLoggingInterceptor() {
return loggingInterceptor;
}

public void setLoggingInterceptor(Class<? extends BaseLoggingInterceptor> loggingInterceptor) {
this.loggingInterceptor = loggingInterceptor;
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* 连接池参数配置
* Connection pool parameter configuration
*
* @author 陈添明
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PoolConfig {

private int maxIdleConnections;


private long keepAliveSecond;

public PoolConfig(int maxIdleConnections, long keepAliveSecond) {
this.maxIdleConnections = maxIdleConnections;
this.keepAliveSecond = keepAliveSecond;
}

public PoolConfig() {
}

public void setMaxIdleConnections(int maxIdleConnections) {
this.maxIdleConnections = maxIdleConnections;
}

public void setKeepAliveSecond(long keepAliveSecond) {
this.keepAliveSecond = keepAliveSecond;
}

public int getMaxIdleConnections() {
return maxIdleConnections;
}

public long getKeepAliveSecond() {
return keepAliveSecond;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.github.lianjiatech.retrofit.spring.boot.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
Expand Down Expand Up @@ -34,11 +33,13 @@
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.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 lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import retrofit2.CallAdapter;
import retrofit2.Converter;
Expand All @@ -50,10 +51,9 @@
@Configuration
@EnableConfigurationProperties(RetrofitProperties.class)
@AutoConfigureAfter({JacksonAutoConfiguration.class})
@Slf4j
public class RetrofitAutoConfiguration implements ApplicationContextAware {

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

private final RetrofitProperties retrofitProperties;

private ApplicationContext applicationContext;
Expand All @@ -71,30 +71,27 @@ public static PrototypeInterceptorBdfProcessor prototypeInterceptorBdfProcessor(
}
}

@Bean
public GlobalAndNetworkInterceptorFinder globalAndNetworkInterceptorFinder() {
return new GlobalAndNetworkInterceptorFinder();
}

@Bean
@ConditionalOnMissingBean
public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) ResourceNameParser resourceNameParser,
@Autowired(required = false) DegradeInterceptor degradeInterceptor)
@Autowired(required = false) DegradeInterceptor degradeInterceptor,
@Autowired(required = false) List<GlobalInterceptor> globalInterceptors,
@Autowired(required = false) List<NetworkInterceptor> networkInterceptors)
throws IllegalAccessException, InstantiationException {
RetrofitConfigBean retrofitConfigBean =
new RetrofitConfigBean(retrofitProperties, globalAndNetworkInterceptorFinder());
// Initialize the connection pool
Map<String, ConnectionPool> poolRegistry = new ConcurrentHashMap<>(4);
Map<String, PoolConfig> pool = retrofitProperties.getPool();
if (pool != null) {
pool.forEach((poolName, poolConfig) -> {
long keepAliveSecond = poolConfig.getKeepAliveSecond();
int maxIdleConnections = poolConfig.getMaxIdleConnections();
ConnectionPool connectionPool =
new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS);
poolRegistry.put(poolName, connectionPool);
});
}
RetrofitConfigBean retrofitConfigBean = new RetrofitConfigBean(retrofitProperties);
retrofitConfigBean.setGlobalInterceptors(globalInterceptors);
retrofitConfigBean.setNetworkInterceptors(networkInterceptors);
retrofitConfigBean.setResourceNameParser(resourceNameParser);
retrofitConfigBean.setDegradeInterceptor(degradeInterceptor);

Map<String, ConnectionPool> poolRegistry = new HashMap<>(4);
retrofitProperties.getPool().forEach((poolName, poolConfig) -> {
long keepAliveSecond = poolConfig.getKeepAliveSecond();
int maxIdleConnections = poolConfig.getMaxIdleConnections();
ConnectionPool connectionPool =
new ConnectionPool(maxIdleConnections, keepAliveSecond, TimeUnit.SECONDS);
poolRegistry.put(poolName, connectionPool);
});
retrofitConfigBean.setPoolRegistry(poolRegistry);

// callAdapterFactory
Expand All @@ -111,10 +108,7 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour
RetryProperty retry = retrofitProperties.getRetry();
Class<? extends BaseRetryInterceptor> retryInterceptor = retry.getRetryInterceptor();
BaseRetryInterceptor retryInterceptorInstance =
ApplicationContextUtils.getBeanOrNull(applicationContext, retryInterceptor);
if (retryInterceptorInstance == null) {
retryInterceptorInstance = retryInterceptor.newInstance();
}
ApplicationContextUtils.getBeanOrNew(applicationContext, retryInterceptor);
BeanUtils.copyProperties(retry, retryInterceptorInstance);
retrofitConfigBean.setRetryInterceptor(retryInterceptorInstance);

Expand All @@ -130,8 +124,7 @@ public RetrofitConfigBean retrofitConfigBean(@Autowired(required = false) Resour
new ServiceInstanceChooserInterceptor(serviceInstanceChooser);
retrofitConfigBean.setServiceInstanceChooserInterceptor(serviceInstanceChooserInterceptor);

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

return retrofitConfigBean;
}

Expand Down Expand Up @@ -176,7 +169,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
public static class RetrofitScannerRegistrarNotFoundConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() {
logger.debug("No {} found.", RetrofitFactoryBean.class.getName());
log.debug("No {} found.", RetrofitFactoryBean.class.getName());
}
}

Expand Down
Loading

0 comments on commit 3ac4650

Please sign in to comment.