|
@@ -0,0 +1,80 @@
|
|
|
+package com.winhc.repal.config;
|
|
|
+
|
|
|
+import com.ctrip.framework.apollo.Config;
|
|
|
+import com.ctrip.framework.apollo.ConfigService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: FeignBuilderProcessor
|
|
|
+ * @Author: xda
|
|
|
+ * @Date: 2022/3/8 21:42
|
|
|
+ */
|
|
|
+@ConditionalOnProperty(value = "RUNTIME_ENV", havingValue = "K8S")
|
|
|
+@Component
|
|
|
+public class FeignBuilderProcessor implements BeanPostProcessor, ApplicationContextAware {
|
|
|
+
|
|
|
+ public static final Logger LOGGER = LoggerFactory.getLogger(FeignBuilderProcessor.class);
|
|
|
+
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private final AtomicInteger atomicInteger = new AtomicInteger();
|
|
|
+
|
|
|
+ public static final String SERVICE_ALIAS_NS = "service-alias";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if (atomicInteger.getAndIncrement() == 0) {
|
|
|
+ Config config = ConfigService.getConfig(SERVICE_ALIAS_NS);
|
|
|
+ Set<String> aliasSet = config.getPropertyNames();
|
|
|
+ String beanNameOfFeignClientFactoryBean = "org.springframework.cloud.openfeign.FeignClientFactoryBean";
|
|
|
+ Class<?> beanNameClz = null;
|
|
|
+ try {
|
|
|
+ beanNameClz = Class.forName(beanNameOfFeignClientFactoryBean);
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Class<?> finalBeanNameClz = beanNameClz;
|
|
|
+ applicationContext.getBeansOfType(beanNameClz).forEach((feignBeanName, beanOfFeignClientFactoryBean) -> {
|
|
|
+ try {
|
|
|
+ Field urlField = ReflectionUtils.findField(finalBeanNameClz, "url");
|
|
|
+ Field nameField = ReflectionUtils.findField(finalBeanNameClz, "name");
|
|
|
+ if (Objects.nonNull(urlField) && Objects.nonNull(nameField)) {
|
|
|
+ ReflectionUtils.makeAccessible(urlField);
|
|
|
+ ReflectionUtils.makeAccessible(nameField);
|
|
|
+ Object url = urlField.get(beanOfFeignClientFactoryBean);
|
|
|
+ Object name = nameField.get(beanOfFeignClientFactoryBean);
|
|
|
+ if (Objects.nonNull(url) && Objects.nonNull(name)) {
|
|
|
+ if (aliasSet.contains(name.toString().toLowerCase())) {
|
|
|
+ LOGGER.info("alias [{}] ===>>> [{}]", name.toString().toLowerCase(), config.getProperty(name.toString().toLowerCase(), null));
|
|
|
+ ReflectionUtils.setField(urlField, beanOfFeignClientFactoryBean, config.getProperty(name.toString().toLowerCase(), null));
|
|
|
+ } else {
|
|
|
+ ReflectionUtils.setField(urlField, beanOfFeignClientFactoryBean, name.toString().toLowerCase());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+}
|