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