♻️ 重构 feign 导入支持单体版本通过SPI 导入

This commit is contained in:
冷冷 2024-04-04 21:32:23 +08:00
parent 1af0c068b0
commit d302fb71f1

View File

@ -34,6 +34,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
@ -48,6 +49,8 @@ import java.util.Map;
*/
public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, EnvironmentAware {
private final static String BASE_URL = "http://127.0.0.1:${server.port}${server.servlet.context-path}";
@Getter
private ClassLoader beanClassLoader;
@ -65,12 +68,11 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
}
private void registerFeignClients(BeanDefinitionRegistry registry) {
List<String> feignClients = new ArrayList<>(
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
List<String> feignClients = new ArrayList<>();
// 支持 springboot 2.7 + 最新版本的配置方式
ImportCandidates.load(FeignClient.class, getBeanClassLoader()).forEach(feignClients::add);
// 如果 spring.factories 里为空
if (feignClients.isEmpty()) {
return;
@ -83,16 +85,21 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
if (attributes == null) {
continue;
}
// 如果是单体项目自动注入 & url 为空
Boolean isMicro = environment.getProperty("spring.cloud.nacos.discovery.enabled", Boolean.class, true);
// 如果已经存在该 bean支持原生的 Feign
if (registry.containsBeanDefinition(className)) {
if (registry.containsBeanDefinition(className) && isMicro) {
continue;
}
registerClientConfiguration(registry, getClientName(attributes), attributes.get("configuration"));
registerClientConfiguration(registry, getClientName(attributes), className,
attributes.get("configuration"));
validate(attributes);
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientFactoryBean.class);
definition.addPropertyValue("url", getUrl(attributes));
definition.addPropertyValue("url", getUrl(registry, attributes));
definition.addPropertyValue("path", getPath(attributes));
String name = getName(attributes);
definition.addPropertyValue("name", name);
@ -103,14 +110,18 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
String contextId = getContextId(attributes);
aliasBuilder.append(contextId);
definition.addPropertyValue("contextId", contextId);
}
else {
} else {
aliasBuilder.append(name);
}
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", attributes.get("decode404"));
definition.addPropertyValue("fallback", attributes.get("fallback"));
definition.addPropertyValue("dismiss404",
Boolean.parseBoolean(String.valueOf(attributes.get("dismiss404"))));
Object fallbackFactory = attributes.get("fallbackFactory");
if (fallbackFactory != null) {
definition.addPropertyValue("fallbackFactory", fallbackFactory instanceof Class ? fallbackFactory
: ClassUtils.resolveClassName(fallbackFactory.toString(), null));
}
definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
@ -130,11 +141,10 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
new String[] { alias });
new String[]{alias});
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
catch (ClassNotFoundException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@ -143,6 +153,7 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
/**
* Return the class used by {@link SpringFactoriesLoader} to load configuration
* candidates.
*
* @return the factory class
*/
private Class<?> getSpringFactoriesLoaderFactoryClass() {
@ -185,8 +196,17 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
return value;
}
private String getUrl(Map<String, Object> attributes) {
String url = resolve((String) attributes.get("url"));
private String getUrl(BeanDefinitionRegistry registry, Map<String, Object> attributes) {
// 如果是单体项目自动注入 & url 为空
Boolean isMicro = environment.getProperty("spring.cloud.nacos.discovery.enabled", Boolean.class, true);
if (isMicro) {
return null;
}
String url = resolve(BASE_URL);
return FeignClientsRegistrar.getUrl(url);
}
@ -238,6 +258,16 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
builder.getBeanDefinition());
}
private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object className,
Object configuration) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FeignClientSpecification.class);
builder.addConstructorArgValue(name);
builder.addConstructorArgValue(className);
builder.addConstructorArgValue(configuration);
registry.registerBeanDefinition(name + "." + FeignClientSpecification.class.getSimpleName(),
builder.getBeanDefinition());
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;