feat: 代码结构优化,更清晰,稳定
feat: 升级1.0.6版本
This commit is contained in:
parent
088fe598a8
commit
5db7676f0c
@ -1,15 +1,21 @@
|
|||||||
package group.flyfish.rest.configuration;
|
package group.flyfish.rest.configuration;
|
||||||
|
|
||||||
|
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
|
||||||
|
import group.flyfish.rest.configuration.modifier.RestPropertiesModifier;
|
||||||
import group.flyfish.rest.core.auth.RestAuthProvider;
|
import group.flyfish.rest.core.auth.RestAuthProvider;
|
||||||
import group.flyfish.rest.core.factory.PropertiesConfigurator;
|
import group.flyfish.rest.registry.proxy.RestInvokers;
|
||||||
import group.flyfish.rest.utils.DataUtils;
|
import group.flyfish.rest.utils.DataUtils;
|
||||||
|
import lombok.AccessLevel;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,6 +53,20 @@ public class RestClientProperties implements InitializingBean {
|
|||||||
*/
|
*/
|
||||||
private RestAuthProvider authProvider;
|
private RestAuthProvider authProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改器们
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.NONE)
|
||||||
|
@Setter(AccessLevel.NONE)
|
||||||
|
private List<RestPropertiesModifier> modifiers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置感知项
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.NONE)
|
||||||
|
@Setter(AccessLevel.NONE)
|
||||||
|
private List<PropertiesConfigurable> configures;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典url
|
* 获取字典url
|
||||||
*
|
*
|
||||||
@ -65,8 +85,24 @@ public class RestClientProperties implements InitializingBean {
|
|||||||
this.authProvider = provider;
|
this.authProvider = provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
public void setModifiers(List<RestPropertiesModifier> modifiers) {
|
||||||
|
this.modifiers = modifiers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
public void setConfigures(List<PropertiesConfigurable> configures) {
|
||||||
|
this.configures = configures;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
PropertiesConfigurator.shared().configure(this);
|
if (DataUtils.isNotEmpty(modifiers)) {
|
||||||
|
modifiers.forEach(modifier -> modifier.modify(this));
|
||||||
|
}
|
||||||
|
if (DataUtils.isNotEmpty(configures)) {
|
||||||
|
configures.forEach(item -> item.configure(this));
|
||||||
|
}
|
||||||
|
RestInvokers.forEach(invoker -> invoker.configure(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
package group.flyfish.rest.configuration.configure;
|
||||||
|
|
||||||
|
import group.flyfish.rest.configuration.RestClientProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 属性感知
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
public interface PropertiesConfigurable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置属性,完成初始化
|
||||||
|
*
|
||||||
|
* @param properties 属性
|
||||||
|
*/
|
||||||
|
void configure(RestClientProperties properties);
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package group.flyfish.rest.configuration.modifier;
|
||||||
|
|
||||||
|
import group.flyfish.rest.configuration.RestClientProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rest属性修改器
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
public interface RestPropertiesModifier {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改属性,发生在afterPropertiesSet之前
|
||||||
|
*
|
||||||
|
* @param properties 属性
|
||||||
|
*/
|
||||||
|
void modify(RestClientProperties properties);
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package group.flyfish.rest.core.factory;
|
package group.flyfish.rest.core.factory;
|
||||||
|
|
||||||
import group.flyfish.rest.configuration.RestClientProperties;
|
import group.flyfish.rest.configuration.RestClientProperties;
|
||||||
|
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
@ -11,7 +12,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
|
|||||||
*
|
*
|
||||||
* @author wangyu
|
* @author wangyu
|
||||||
*/
|
*/
|
||||||
public class DefaultHttpClientProvider extends PropertiesConfigAdaptor implements HttpClientProvider, BeanFactoryAware {
|
public class DefaultHttpClientProvider implements HttpClientProvider, BeanFactoryAware, PropertiesConfigurable {
|
||||||
|
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package group.flyfish.rest.core.factory;
|
package group.flyfish.rest.core.factory;
|
||||||
|
|
||||||
import group.flyfish.rest.configuration.RestClientProperties;
|
import group.flyfish.rest.configuration.RestClientProperties;
|
||||||
|
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
|
||||||
import group.flyfish.rest.utils.DataUtils;
|
import group.flyfish.rest.utils.DataUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
@ -21,7 +22,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
* @author wangyu
|
* @author wangyu
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public final class HttpClientFactoryBean extends PropertiesConfigAdaptor implements FactoryBean<CloseableHttpClient> {
|
public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpClient>, PropertiesConfigurable {
|
||||||
|
|
||||||
// 使用非公平锁
|
// 使用非公平锁
|
||||||
private final ReentrantLock lock = new ReentrantLock();
|
private final ReentrantLock lock = new ReentrantLock();
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
package group.flyfish.rest.core.factory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 属性配置适配器
|
|
||||||
*
|
|
||||||
* @author wangyu
|
|
||||||
*/
|
|
||||||
public class PropertiesConfigAdaptor implements PropertiesConfigurable {
|
|
||||||
|
|
||||||
protected PropertiesConfigAdaptor() {
|
|
||||||
PropertiesConfigurator.prepare(this);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
package group.flyfish.rest.core.factory;
|
|
||||||
|
|
||||||
import group.flyfish.rest.configuration.RestClientProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 属性感知
|
|
||||||
*
|
|
||||||
* @author wangyu
|
|
||||||
*/
|
|
||||||
public interface PropertiesConfigurable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置属性,完成初始化
|
|
||||||
*
|
|
||||||
* @param properties 属性
|
|
||||||
*/
|
|
||||||
default void configure(RestClientProperties properties) {
|
|
||||||
// 默认什么也不做
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化完成后置动作
|
|
||||||
*/
|
|
||||||
default void afterConfigured(RestClientProperties properties) {
|
|
||||||
// 默认什么也不做
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
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.forEach(item -> item.afterConfigured(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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -88,8 +88,7 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
|
|||||||
* @return bean定义
|
* @return bean定义
|
||||||
*/
|
*/
|
||||||
private BeanDefinition generate(Class<?> clazz) {
|
private BeanDefinition generate(Class<?> clazz) {
|
||||||
return BeanDefinitionBuilder.genericBeanDefinition(clazz,
|
return BeanDefinitionBuilder.genericBeanDefinition(clazz, () -> RestProxyInvoker.produce(clazz, this))
|
||||||
() -> DataUtils.cast(RestProxyInvoker.produce(clazz, this)))
|
|
||||||
.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE).getRawBeanDefinition();
|
.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE).getRawBeanDefinition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package group.flyfish.rest.registry.proxy;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rest执行器实例集合
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
public class RestInvokers {
|
||||||
|
|
||||||
|
private static final List<RestProxyInvoker> invokers = new ArrayList<>();
|
||||||
|
|
||||||
|
public static void add(RestProxyInvoker invoker) {
|
||||||
|
invokers.add(invoker);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void forEach(Consumer<RestProxyInvoker> handler) {
|
||||||
|
invokers.forEach(handler);
|
||||||
|
}
|
||||||
|
}
|
@ -5,10 +5,10 @@ import group.flyfish.rest.annotation.AutoMapping;
|
|||||||
import group.flyfish.rest.annotation.RestApi;
|
import group.flyfish.rest.annotation.RestApi;
|
||||||
import group.flyfish.rest.annotation.RestService;
|
import group.flyfish.rest.annotation.RestService;
|
||||||
import group.flyfish.rest.configuration.RestClientProperties;
|
import group.flyfish.rest.configuration.RestClientProperties;
|
||||||
|
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
|
||||||
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.PropertiesConfigAdaptor;
|
|
||||||
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;
|
||||||
@ -32,7 +32,6 @@ import java.lang.reflect.Method;
|
|||||||
import java.lang.reflect.Proxy;
|
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.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,10 +40,10 @@ import java.util.stream.Stream;
|
|||||||
* @author wangyu
|
* @author wangyu
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements InvocationHandler {
|
public class RestProxyInvoker implements InvocationHandler, PropertiesConfigurable {
|
||||||
|
|
||||||
// 要代理的目标类
|
// 要代理的目标类
|
||||||
private final Class<T> targetType;
|
private final Class<?> targetType;
|
||||||
// 服务映射
|
// 服务映射
|
||||||
private final RestService restService;
|
private final RestService restService;
|
||||||
// 配置属性
|
// 配置属性
|
||||||
@ -66,7 +65,7 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
|
|||||||
* @param targetType 目标类型
|
* @param targetType 目标类型
|
||||||
* @param registry 注册器
|
* @param registry 注册器
|
||||||
*/
|
*/
|
||||||
private RestProxyInvoker(Class<T> targetType, RestApiRegistry registry) {
|
private RestProxyInvoker(Class<?> targetType, RestApiRegistry registry) {
|
||||||
this.targetType = targetType;
|
this.targetType = targetType;
|
||||||
this.registry = registry;
|
this.registry = registry;
|
||||||
// 注解的优先级高于全局基本路径
|
// 注解的优先级高于全局基本路径
|
||||||
@ -81,8 +80,9 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
|
|||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public static <T> T produce(Class<T> target, RestApiRegistry registry) {
|
public static <T> T produce(Class<?> target, RestApiRegistry registry) {
|
||||||
RestProxyInvoker<T> invoker = new RestProxyInvoker<>(target, registry);
|
RestProxyInvoker invoker = new RestProxyInvoker(target, registry);
|
||||||
|
RestInvokers.add(invoker);
|
||||||
return DataUtils.cast(Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, invoker));
|
return DataUtils.cast(Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, invoker));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
|
|||||||
* @param properties 属性
|
* @param properties 属性
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void afterConfigured(RestClientProperties properties) {
|
public void configure(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();
|
||||||
@ -243,13 +243,13 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
|
|||||||
url = restApi.url();
|
url = restApi.url();
|
||||||
} else {
|
} else {
|
||||||
// 构建基础url,优先级从小到大依次找。同时尝试取字典值
|
// 构建基础url,优先级从小到大依次找。同时尝试取字典值
|
||||||
Optional<String> baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl).filter(DataUtils::isNotBlank).findFirst().map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found);
|
url = Stream.of(restApi.baseUrl(), this.baseUrl)
|
||||||
// 判定和赋值
|
.filter(DataUtils::isNotBlank)
|
||||||
if (baseUrl.isPresent() && DataUtils.isNotBlank(restApi.uri())) {
|
.findFirst()
|
||||||
url = baseUrl.get() + restApi.uri();
|
// 判定和赋值
|
||||||
} else {
|
.map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found)
|
||||||
throw new IllegalArgumentException("【Rest调用】未指定url或baseurl,无法调用远端服务器!");
|
.map(base -> base + restApi.uri())
|
||||||
}
|
.orElseThrow(() -> new IllegalArgumentException("【Rest调用】未指定url或baseurl,无法调用远端服务器!"));
|
||||||
}
|
}
|
||||||
// 尝试解析路径参数
|
// 尝试解析路径参数
|
||||||
return context.hasPathParams() ? UrlCompiler.compile(url, context.getPathParams()) : url;
|
return context.hasPathParams() ? UrlCompiler.compile(url, context.getPathParams()) : url;
|
||||||
|
Loading…
Reference in New Issue
Block a user