feat: 代码结构优化,更清晰,稳定

feat: 升级1.0.6版本
This commit is contained in:
wangyu 2023-01-31 14:30:23 +08:00
parent 088fe598a8
commit 5db7676f0c
11 changed files with 117 additions and 116 deletions

View File

@ -1,15 +1,21 @@
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.factory.PropertiesConfigurator;
import group.flyfish.rest.registry.proxy.RestInvokers;
import group.flyfish.rest.utils.DataUtils;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -47,6 +53,20 @@ public class RestClientProperties implements InitializingBean {
*/
private RestAuthProvider authProvider;
/**
* 修改器们
*/
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private List<RestPropertiesModifier> modifiers;
/**
* 配置感知项
*/
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private List<PropertiesConfigurable> configures;
/**
* 获取字典url
*
@ -65,8 +85,24 @@ public class RestClientProperties implements InitializingBean {
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
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));
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
package group.flyfish.rest.core.factory;
import group.flyfish.rest.configuration.RestClientProperties;
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
@ -11,7 +12,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
*
* @author wangyu
*/
public class DefaultHttpClientProvider extends PropertiesConfigAdaptor implements HttpClientProvider, BeanFactoryAware {
public class DefaultHttpClientProvider implements HttpClientProvider, BeanFactoryAware, PropertiesConfigurable {
private CloseableHttpClient client;

View File

@ -1,6 +1,7 @@
package group.flyfish.rest.core.factory;
import group.flyfish.rest.configuration.RestClientProperties;
import group.flyfish.rest.configuration.configure.PropertiesConfigurable;
import group.flyfish.rest.utils.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@ -21,7 +22,7 @@ import java.util.concurrent.locks.ReentrantLock;
* @author wangyu
*/
@Slf4j
public final class HttpClientFactoryBean extends PropertiesConfigAdaptor implements FactoryBean<CloseableHttpClient> {
public final class HttpClientFactoryBean implements FactoryBean<CloseableHttpClient>, PropertiesConfigurable {
// 使用非公平锁
private final ReentrantLock lock = new ReentrantLock();

View File

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

View File

@ -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) {
// 默认什么也不做
}
}

View File

@ -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;
}
}

View File

@ -88,8 +88,7 @@ public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, Bea
* @return bean定义
*/
private BeanDefinition generate(Class<?> clazz) {
return BeanDefinitionBuilder.genericBeanDefinition(clazz,
() -> DataUtils.cast(RestProxyInvoker.produce(clazz, this)))
return BeanDefinitionBuilder.genericBeanDefinition(clazz, () -> RestProxyInvoker.produce(clazz, this))
.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE).getRawBeanDefinition();
}

View File

@ -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);
}
}

View File

@ -5,10 +5,10 @@ import group.flyfish.rest.annotation.AutoMapping;
import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestService;
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.client.RestClient;
import group.flyfish.rest.core.client.RestClientBuilder;
import group.flyfish.rest.core.factory.PropertiesConfigAdaptor;
import group.flyfish.rest.mapping.RestResultMapping;
import group.flyfish.rest.registry.RestApiRegistry;
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.Type;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
/**
@ -41,10 +40,10 @@ import java.util.stream.Stream;
* @author wangyu
*/
@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;
// 配置属性
@ -66,7 +65,7 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
* @param targetType 目标类型
* @param registry 注册器
*/
private RestProxyInvoker(Class<T> targetType, RestApiRegistry registry) {
private RestProxyInvoker(Class<?> targetType, RestApiRegistry registry) {
this.targetType = targetType;
this.registry = registry;
// 注解的优先级高于全局基本路径
@ -81,8 +80,9 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
* @param <T> 泛型
* @return 结果
*/
public static <T> T produce(Class<T> target, RestApiRegistry registry) {
RestProxyInvoker<T> invoker = new RestProxyInvoker<>(target, registry);
public static <T> T produce(Class<?> target, RestApiRegistry registry) {
RestProxyInvoker invoker = new RestProxyInvoker(target, registry);
RestInvokers.add(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 属性
*/
@Override
public void afterConfigured(RestClientProperties properties) {
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();
@ -243,13 +243,13 @@ public class RestProxyInvoker<T> extends PropertiesConfigAdaptor implements Invo
url = restApi.url();
} else {
// 构建基础url优先级从小到大依次找同时尝试取字典值
Optional<String> baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl).filter(DataUtils::isNotBlank).findFirst().map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found);
// 判定和赋值
if (baseUrl.isPresent() && DataUtils.isNotBlank(restApi.uri())) {
url = baseUrl.get() + restApi.uri();
} else {
throw new IllegalArgumentException("【Rest调用】未指定url或baseurl无法调用远端服务器");
}
url = Stream.of(restApi.baseUrl(), this.baseUrl)
.filter(DataUtils::isNotBlank)
.findFirst()
// 判定和赋值
.map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found)
.map(base -> base + restApi.uri())
.orElseThrow(() -> new IllegalArgumentException("【Rest调用】未指定url或baseurl无法调用远端服务器"));
}
// 尝试解析路径参数
return context.hasPathParams() ? UrlCompiler.compile(url, context.getPathParams()) : url;