feat: 1.0.2版本升级,更新核心代码,解耦
This commit is contained in:
parent
9766258f35
commit
fc8a6293e8
4
pom.xml
4
pom.xml
@ -13,7 +13,7 @@
|
||||
|
||||
<groupId>group.flyfish</groupId>
|
||||
<artifactId>rest-proxy</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
@ -22,7 +22,7 @@
|
||||
<java.version>1.8</java.version>
|
||||
<commons-collection.version>4.4</commons-collection.version>
|
||||
<commons.lang.version>2.6</commons.lang.version>
|
||||
<sdk.version>1.0.1</sdk.version>
|
||||
<sdk.version>1.0.2</sdk.version>
|
||||
</properties>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>group.flyfish</groupId>
|
||||
<artifactId>rest-proxy</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>group.flyfish</groupId>
|
||||
<artifactId>rest-proxy</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package group.flyfish.rest.configuration;
|
||||
|
||||
import group.flyfish.rest.core.factory.DefaultHttpClientProvider;
|
||||
import group.flyfish.rest.core.factory.HttpClientFactoryBean;
|
||||
import group.flyfish.rest.core.factory.HttpClientProvider;
|
||||
import group.flyfish.rest.mapping.RestResultMapping;
|
||||
@ -14,9 +15,7 @@ import group.flyfish.rest.utils.DataUtils;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -42,13 +41,12 @@ public class RestClientConfiguration {
|
||||
/**
|
||||
* http client工厂bean
|
||||
*
|
||||
* @param properties 属性
|
||||
* @return 结果
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CloseableHttpClient.class)
|
||||
public HttpClientFactoryBean httpClientFactoryBean(RestClientProperties properties) {
|
||||
return new HttpClientFactoryBean(properties);
|
||||
public HttpClientFactoryBean httpClientFactoryBean() {
|
||||
return new HttpClientFactoryBean();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,8 +56,8 @@ public class RestClientConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public HttpClientProvider httpClientProvider(CloseableHttpClient client) {
|
||||
return () -> client;
|
||||
public HttpClientProvider httpClientProvider() {
|
||||
return new DefaultHttpClientProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,15 +66,14 @@ public class RestClientConfiguration {
|
||||
* @return 结果
|
||||
*/
|
||||
@Bean
|
||||
@Lazy
|
||||
public RestApiRegistry restApiRegistry(RestArgumentResolverComposite composite, HttpClientProvider provider,
|
||||
List<RestResultMapping> mappings, RestClientProperties properties) {
|
||||
List<RestResultMapping> mappings) {
|
||||
// 先注册映射们
|
||||
if (DataUtils.isNotEmpty(mappings)) {
|
||||
mappings.forEach(mapping -> RestResultMapping.MAPPINGS.put(mapping.getClass(), mapping));
|
||||
}
|
||||
// 最后实例化
|
||||
return new RestApiRegistry(composite, properties, provider);
|
||||
return new RestApiRegistry(composite, provider);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,8 +1,10 @@
|
||||
package group.flyfish.rest.configuration;
|
||||
|
||||
import group.flyfish.rest.core.factory.PropertiesConfigurator;
|
||||
import group.flyfish.rest.utils.DataUtils;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
@ -16,7 +18,7 @@ import java.util.Map;
|
||||
*/
|
||||
@Data
|
||||
@Slf4j
|
||||
public class RestClientProperties {
|
||||
public class RestClientProperties implements InitializingBean {
|
||||
|
||||
/**
|
||||
* 超时时间,默认30s
|
||||
@ -50,4 +52,9 @@ public class RestClientProperties {
|
||||
}
|
||||
return urls.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
PropertiesConfigurator.shared().configure(this);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
package group.flyfish.rest.core.factory;
|
||||
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
|
||||
/**
|
||||
* 默认的http客户端提供者
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public class DefaultHttpClientProvider implements HttpClientProvider, BeanFactoryAware, PropertiesConfigurable {
|
||||
|
||||
private CloseableHttpClient client;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
public DefaultHttpClientProvider() {
|
||||
PropertiesConfigurator.prepare(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取client,可以自由替换
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public CloseableHttpClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置属性,完成初始化
|
||||
*
|
||||
* @param properties 属性
|
||||
*/
|
||||
@Override
|
||||
public void configure(RestClientProperties properties) {
|
||||
this.client = beanFactory.getBean(CloseableHttpClient.class);
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package group.flyfish.rest.core.factory;
|
||||
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
import group.flyfish.rest.utils.DataUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
@ -22,15 +21,18 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
* @author wangyu
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpClient> {
|
||||
public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpClient>, PropertiesConfigurable {
|
||||
|
||||
// 使用非公平锁
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
// 客户端实例,单例
|
||||
private volatile CloseableHttpClient client;
|
||||
// 配置,配置没进来就不初始化
|
||||
private final RestClientProperties properties;
|
||||
private RestClientProperties properties;
|
||||
|
||||
public HttpClientFactoryBean() {
|
||||
PropertiesConfigurator.prepare(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建单例的httpClient
|
||||
@ -78,4 +80,14 @@ public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpCli
|
||||
public Class<?> getObjectType() {
|
||||
return CloseableHttpClient.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置属性,完成初始化
|
||||
*
|
||||
* @param properties 属性
|
||||
*/
|
||||
@Override
|
||||
public void configure(RestClientProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package group.flyfish.rest.core.factory;
|
||||
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
|
||||
/**
|
||||
* 属性感知
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public interface PropertiesConfigurable {
|
||||
|
||||
/**
|
||||
* 配置属性,完成初始化
|
||||
* @param properties 属性
|
||||
*/
|
||||
void configure(RestClientProperties properties);
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package group.flyfish.rest.core.factory;
|
||||
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 属性配置器
|
||||
* 解耦bean依赖,解决属性注入失效的问题
|
||||
*
|
||||
* @author wangyu
|
||||
*/
|
||||
public class PropertiesConfigurator implements PropertiesConfigurable {
|
||||
|
||||
private static final PropertiesConfigurator INSTANCE = new PropertiesConfigurator();
|
||||
|
||||
private final List<PropertiesConfigurable> list = new ArrayList<>();
|
||||
|
||||
private RestClientProperties properties;
|
||||
|
||||
/**
|
||||
* 配置属性,完成初始化
|
||||
*
|
||||
* @param properties 属性
|
||||
*/
|
||||
@Override
|
||||
public void configure(RestClientProperties properties) {
|
||||
this.properties = properties;
|
||||
list.forEach(item -> item.configure(properties));
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public void configure(PropertiesConfigurable configurable) {
|
||||
if (null != properties) {
|
||||
configurable.configure(properties);
|
||||
} else {
|
||||
list.add(configurable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预配置
|
||||
*
|
||||
* @param configurable 可配置实例
|
||||
*/
|
||||
public static void prepare(PropertiesConfigurable configurable) {
|
||||
INSTANCE.configure(configurable);
|
||||
}
|
||||
|
||||
public static PropertiesConfigurator shared() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package group.flyfish.rest.registry;
|
||||
|
||||
import group.flyfish.rest.annotation.EnableRestApiProxy;
|
||||
import group.flyfish.rest.annotation.RestService;
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
import group.flyfish.rest.core.factory.HttpClientProvider;
|
||||
import group.flyfish.rest.registry.proxy.RestProxyInvoker;
|
||||
import group.flyfish.rest.registry.proxy.support.RestArgumentResolverComposite;
|
||||
@ -19,6 +18,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.*;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -36,9 +36,6 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
||||
@Getter
|
||||
private final RestArgumentResolverComposite composite;
|
||||
|
||||
@Getter
|
||||
private final RestClientProperties properties;
|
||||
|
||||
@Getter
|
||||
private final HttpClientProvider provider;
|
||||
|
||||
@ -72,6 +69,8 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
||||
Reflections reflections = new Reflections(packageNames.toArray());
|
||||
// 得到Resource注解的类
|
||||
Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(RestService.class);
|
||||
// 不存在,不要浪费性能
|
||||
if (CollectionUtils.isEmpty(classSet)) return;
|
||||
// 代理并生成子类,并注册到ioc容器
|
||||
classSet.forEach(clazz -> registry.registerBeanDefinition(clazz.getName(), generate(clazz)));
|
||||
} catch (IllegalStateException e) {
|
||||
@ -100,10 +99,6 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public void setBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, "当前bean factory不被支持!");
|
||||
|
@ -7,6 +7,8 @@ import group.flyfish.rest.annotation.RestService;
|
||||
import group.flyfish.rest.configuration.RestClientProperties;
|
||||
import group.flyfish.rest.core.client.RestClient;
|
||||
import group.flyfish.rest.core.client.RestClientBuilder;
|
||||
import group.flyfish.rest.core.factory.PropertiesConfigurable;
|
||||
import group.flyfish.rest.core.factory.PropertiesConfigurator;
|
||||
import group.flyfish.rest.mapping.RestResultMapping;
|
||||
import group.flyfish.rest.registry.RestApiRegistry;
|
||||
import group.flyfish.rest.registry.proxy.support.ArgumentResolveContext;
|
||||
@ -30,6 +32,7 @@ import java.lang.reflect.Proxy;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@ -38,16 +41,18 @@ import java.util.stream.Stream;
|
||||
* @author wangyu
|
||||
*/
|
||||
@Slf4j
|
||||
public class RestProxyInvoker<T> implements InvocationHandler {
|
||||
public class RestProxyInvoker<T> implements InvocationHandler, PropertiesConfigurable {
|
||||
|
||||
// 要代理的目标类
|
||||
private final Class<T> targetType;
|
||||
// 服务映射
|
||||
private final RestService restService;
|
||||
// 配置属性
|
||||
private RestClientProperties properties;
|
||||
// 初始的基本路径
|
||||
private final String baseUrl;
|
||||
private String baseUrl;
|
||||
// 超时时间
|
||||
private final RequestConfig config;
|
||||
private RequestConfig config;
|
||||
// 注册器,包含基础信息
|
||||
private final RestApiRegistry registry;
|
||||
// 结果映射
|
||||
@ -65,12 +70,7 @@ public class RestProxyInvoker<T> implements InvocationHandler {
|
||||
// 注解的优先级高于全局基本路径
|
||||
this.restService = AnnotationUtils.findAnnotation(targetType, RestService.class);
|
||||
Assert.notNull(restService, "当前类尚未添加@RestService注解!");
|
||||
RestClientProperties properties = registry.getProperties();
|
||||
this.config = RequestConfig.custom()
|
||||
.setConnectTimeout((int) properties.getConnectionTimeout().toMillis())
|
||||
.setSocketTimeout(restService.timeout())
|
||||
.build();
|
||||
this.baseUrl = this.findBaseUrl();
|
||||
PropertiesConfigurator.prepare(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,6 +85,21 @@ public class RestProxyInvoker<T> implements InvocationHandler {
|
||||
return DataUtils.cast(Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, invoker));
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成配置
|
||||
*
|
||||
* @param properties 属性
|
||||
*/
|
||||
@Override
|
||||
public void configure(RestClientProperties properties) {
|
||||
this.properties = properties;
|
||||
this.config = RequestConfig.custom()
|
||||
.setConnectTimeout((int) properties.getConnectionTimeout().toMillis())
|
||||
.setSocketTimeout(restService.timeout())
|
||||
.build();
|
||||
this.baseUrl = this.findBaseUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行rest请求的地方,这里很简单易懂
|
||||
*
|
||||
@ -180,7 +195,6 @@ public class RestProxyInvoker<T> implements InvocationHandler {
|
||||
* 找到配置固化的基本url
|
||||
*/
|
||||
private String findBaseUrl() {
|
||||
RestClientProperties properties = registry.getProperties();
|
||||
// 当且仅当存在时进入
|
||||
if (null != restService) {
|
||||
// 注解的路径解析
|
||||
@ -220,7 +234,6 @@ public class RestProxyInvoker<T> implements InvocationHandler {
|
||||
if (DataUtils.isNotBlank(restApi.url())) {
|
||||
url = restApi.url();
|
||||
} else {
|
||||
RestClientProperties properties = registry.getProperties();
|
||||
// 构建基础url,优先级从小到大依次找。同时尝试取字典值
|
||||
Optional<String> baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl)
|
||||
.filter(DataUtils::isNotBlank)
|
||||
|
Loading…
Reference in New Issue
Block a user