Browse Source
2.增加url后置处理器EpmetFeignClientsBeanPostProcessor.java 3.新增:问卷提交时候,相epmet-governance服务推送结果数据的逻辑dev
9 changed files with 298 additions and 4 deletions
@ -0,0 +1,146 @@ |
|||||
|
package com.tduck.cloud.api.config; |
||||
|
|
||||
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
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.context.EnvironmentAware; |
||||
|
import org.springframework.core.env.Environment; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.springframework.util.ReflectionUtils; |
||||
|
|
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.FileNotFoundException; |
||||
|
import java.io.IOException; |
||||
|
import java.lang.reflect.Field; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.Paths; |
||||
|
import java.util.Objects; |
||||
|
import java.util.Properties; |
||||
|
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
|
||||
|
/** |
||||
|
* @Description FeignClient修饰BeanPostProcessor |
||||
|
* 1.处理url:在spring.profiles.active=local的情况下,读取~/epmet_files/epmet_hosts.properties文件中的行,在生成OpenFeignClient的时候 |
||||
|
* 将文件中读取到的地址替换进去,而不通过注册中心。如果文件中没设置,那使用注册中心,通过负载均衡器选择实例。 |
||||
|
* properties实例: |
||||
|
* epmet-user-server=http://localhost:8087
|
||||
|
* gov-org-server=http://localhost:8092
|
||||
|
* # epmet-actual-base=http://localhost:8118
|
||||
|
* @Author wxz |
||||
|
* @Date 2023/7/28 13:18 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@ConditionalOnProperty(matchIfMissing = false, prefix = "spring.profiles", name = "active", havingValue = "local") |
||||
|
public class EpmetFeignClientsBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, EnvironmentAware { |
||||
|
|
||||
|
public static final String FEIGN_CLIENT_FACTORY_BEAN_CLASS_NAME = "org.springframework.cloud.openfeign.FeignClientFactoryBean"; |
||||
|
public static final String URL_FIELD_NAME = "url"; |
||||
|
|
||||
|
private ApplicationContext applicationContext; |
||||
|
|
||||
|
private Environment environment; |
||||
|
|
||||
|
private AtomicInteger atomicInteger = new AtomicInteger(); |
||||
|
|
||||
|
/** |
||||
|
* epmet-user-server=localhost:8087 |
||||
|
* gov-org-server=localhost:8092 |
||||
|
* ... |
||||
|
*/ |
||||
|
private Properties localServerAndUrlProps = new Properties(); |
||||
|
|
||||
|
public EpmetFeignClientsBeanPostProcessor() { |
||||
|
// 获取home目录下。unix系统:~/ windows系统:C:\Users\xxx
|
||||
|
Path epmetHostsConfigPath = Paths.get(System.getProperty("user.home")).resolve("epmet_files").resolve("epmet_hosts.properties"); |
||||
|
if (Files.notExists(epmetHostsConfigPath)) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
try (FileInputStream is = new FileInputStream(epmetHostsConfigPath.toFile())) { |
||||
|
// 读取本地配置文件,加载到缓存
|
||||
|
localServerAndUrlProps.load(is); |
||||
|
} catch ( |
||||
|
FileNotFoundException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} catch (IOException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
||||
|
|
||||
|
if (atomicInteger.getAndIncrement() == 0) { |
||||
|
|
||||
|
Class beanNameClz = null; |
||||
|
try { |
||||
|
beanNameClz = Class.forName(FEIGN_CLIENT_FACTORY_BEAN_CLASS_NAME); |
||||
|
} catch ( |
||||
|
ClassNotFoundException e) { |
||||
|
log.info("没有{}相关实例", FEIGN_CLIENT_FACTORY_BEAN_CLASS_NAME); |
||||
|
} |
||||
|
|
||||
|
final Class fbeanNameClz = beanNameClz; |
||||
|
applicationContext.getBeansOfType(fbeanNameClz).forEach((feignBeanName, beanOfFeignClientFactoryBean) -> { |
||||
|
try { |
||||
|
setUrlField(fbeanNameClz, beanOfFeignClientFactoryBean); |
||||
|
} catch ( |
||||
|
Exception e) { |
||||
|
String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); |
||||
|
log.error("bean\"{}\"设置url失败,错误信息:{}", feignBeanName, errorStackTrace); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param clazz: 丢想的class文件 |
||||
|
* @param obj: 对象 |
||||
|
* @return |
||||
|
* @description: 为指定bean设置指定属性 |
||||
|
* @author: WangXianZhang |
||||
|
* @date: 2023/7/28 13:23 |
||||
|
*/ |
||||
|
private void setUrlField(Class clazz, Object obj) throws Exception { |
||||
|
// 1.先获取对端服务名
|
||||
|
Field serverNameField = ReflectionUtils.findField(clazz, "name"); |
||||
|
serverNameField.setAccessible(true); |
||||
|
String serverNames = (String) serverNameField.get(obj); |
||||
|
|
||||
|
if (StringUtils.isBlank(serverNames)) { |
||||
|
log.info("服务\"{}\"的\"{}\"字段为空,不设置url", clazz.getName(), URL_FIELD_NAME); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Field urlField = ReflectionUtils.findField(clazz, URL_FIELD_NAME); |
||||
|
|
||||
|
String currentServiceUrl; |
||||
|
|
||||
|
if (Objects.nonNull(urlField) |
||||
|
&& StringUtils.isNotBlank(currentServiceUrl = (String) localServerAndUrlProps.get(serverNames))) { |
||||
|
|
||||
|
ReflectionUtils.makeAccessible(urlField); |
||||
|
ReflectionUtils.setField(urlField, obj, currentServiceUrl); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setEnvironment(Environment environment) { |
||||
|
this.environment = environment; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
|
this.applicationContext = applicationContext; |
||||
|
} |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project |
||||
|
xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion> |
||||
|
4.0.0 |
||||
|
</modelVersion> |
||||
|
<parent> |
||||
|
<groupId> |
||||
|
com.tduck |
||||
|
</groupId> |
||||
|
<artifactId> |
||||
|
tduck-platform |
||||
|
</artifactId> |
||||
|
<version> |
||||
|
0.0.1-SNAPSHOT |
||||
|
</version> |
||||
|
</parent> |
||||
|
|
||||
|
<groupId> |
||||
|
org.example |
||||
|
</groupId> |
||||
|
<artifactId> |
||||
|
tduck-clients |
||||
|
</artifactId> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source> |
||||
|
17 |
||||
|
</maven.compiler.source> |
||||
|
<maven.compiler.target> |
||||
|
17 |
||||
|
</maven.compiler.target> |
||||
|
<project.build.sourceEncoding> |
||||
|
UTF-8 |
||||
|
</project.build.sourceEncoding> |
||||
|
</properties> |
||||
|
|
||||
|
</project> |
Loading…
Reference in new issue