feat: 优化共享配置类

This commit is contained in:
wangyu 2023-01-31 12:10:05 +08:00
parent 9df84805ec
commit 088fe598a8
6 changed files with 30 additions and 16 deletions

View File

@ -11,16 +11,12 @@ import org.springframework.beans.factory.BeanFactoryAware;
* *
* @author wangyu * @author wangyu
*/ */
public class DefaultHttpClientProvider implements HttpClientProvider, BeanFactoryAware, PropertiesConfigurable { public class DefaultHttpClientProvider extends PropertiesConfigAdaptor implements HttpClientProvider, BeanFactoryAware {
private CloseableHttpClient client; private CloseableHttpClient client;
private BeanFactory beanFactory; private BeanFactory beanFactory;
public DefaultHttpClientProvider() {
PropertiesConfigurator.prepare(this);
}
/** /**
* 获取client可以自由替换 * 获取client可以自由替换
* *

View File

@ -21,7 +21,7 @@ import java.util.concurrent.locks.ReentrantLock;
* @author wangyu * @author wangyu
*/ */
@Slf4j @Slf4j
public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpClient>, PropertiesConfigurable { public final class HttpClientFactoryBean extends PropertiesConfigAdaptor implements FactoryBean<CloseableHttpClient> {
// 使用非公平锁 // 使用非公平锁
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
@ -30,10 +30,6 @@ public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpCli
// 配置配置没进来就不初始化 // 配置配置没进来就不初始化
private RestClientProperties properties; private RestClientProperties properties;
public HttpClientFactoryBean() {
PropertiesConfigurator.prepare(this);
}
/** /**
* 构建单例的httpClient * 构建单例的httpClient
* *

View File

@ -0,0 +1,13 @@
package group.flyfish.rest.core.factory;
/**
* 属性配置适配器
*
* @author wangyu
*/
public class PropertiesConfigAdaptor implements PropertiesConfigurable {
protected PropertiesConfigAdaptor() {
PropertiesConfigurator.prepare(this);
}
}

View File

@ -11,7 +11,17 @@ public interface PropertiesConfigurable {
/** /**
* 配置属性完成初始化 * 配置属性完成初始化
*
* @param properties 属性 * @param properties 属性
*/ */
void configure(RestClientProperties properties); default void configure(RestClientProperties properties) {
// 默认什么也不做
}
/**
* 初始化完成后置动作
*/
default void afterConfigured(RestClientProperties properties) {
// 默认什么也不做
}
} }

View File

@ -28,6 +28,7 @@ public class PropertiesConfigurator implements PropertiesConfigurable {
public void configure(RestClientProperties properties) { public void configure(RestClientProperties properties) {
this.properties = properties; this.properties = properties;
list.forEach(item -> item.configure(properties)); list.forEach(item -> item.configure(properties));
list.forEach(item -> item.afterConfigured(properties));
list.clear(); list.clear();
} }

View File

@ -8,8 +8,7 @@ import group.flyfish.rest.configuration.RestClientProperties;
import group.flyfish.rest.core.auth.RestAuthProvider; import group.flyfish.rest.core.auth.RestAuthProvider;
import group.flyfish.rest.core.client.RestClient; import group.flyfish.rest.core.client.RestClient;
import group.flyfish.rest.core.client.RestClientBuilder; import group.flyfish.rest.core.client.RestClientBuilder;
import group.flyfish.rest.core.factory.PropertiesConfigurable; import group.flyfish.rest.core.factory.PropertiesConfigAdaptor;
import group.flyfish.rest.core.factory.PropertiesConfigurator;
import group.flyfish.rest.mapping.RestResultMapping; import group.flyfish.rest.mapping.RestResultMapping;
import group.flyfish.rest.registry.RestApiRegistry; import group.flyfish.rest.registry.RestApiRegistry;
import group.flyfish.rest.registry.proxy.support.ArgumentResolveContext; import group.flyfish.rest.registry.proxy.support.ArgumentResolveContext;
@ -42,7 +41,7 @@ import java.util.stream.Stream;
* @author wangyu * @author wangyu
*/ */
@Slf4j @Slf4j
public class RestProxyInvoker<T> implements InvocationHandler, PropertiesConfigurable { public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements InvocationHandler {
// 要代理的目标类 // 要代理的目标类
private final Class<T> targetType; private final Class<T> targetType;
@ -73,7 +72,6 @@ public class RestProxyInvoker<T> implements InvocationHandler, PropertiesConfigu
// 注解的优先级高于全局基本路径 // 注解的优先级高于全局基本路径
this.restService = AnnotationUtils.findAnnotation(targetType, RestService.class); this.restService = AnnotationUtils.findAnnotation(targetType, RestService.class);
Assert.notNull(restService, "当前类尚未添加@RestService注解"); Assert.notNull(restService, "当前类尚未添加@RestService注解");
PropertiesConfigurator.prepare(this);
} }
/** /**
@ -94,7 +92,7 @@ public class RestProxyInvoker<T> implements InvocationHandler, PropertiesConfigu
* @param properties 属性 * @param properties 属性
*/ */
@Override @Override
public void configure(RestClientProperties properties) { public void afterConfigured(RestClientProperties properties) {
this.properties = properties; this.properties = properties;
this.config = RequestConfig.custom().setConnectTimeout((int) properties.getConnectionTimeout().toMillis()).setSocketTimeout(restService.timeout()).build(); this.config = RequestConfig.custom().setConnectTimeout((int) properties.getConnectionTimeout().toMillis()).setSocketTimeout(restService.timeout()).build();
this.baseUrl = this.findBaseUrl(); this.baseUrl = this.findBaseUrl();