新增:数据源预热;加密配置解密优化
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.sdm.common.config;//package com.sdm.project.config.mybatis;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class DataSourcePreWarmer {
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner secondDataSourcePreWarmer(
|
||||
@Qualifier("secondDataSource") DataSource secondDataSource) {
|
||||
return args -> {
|
||||
try (Connection conn = secondDataSource.getConnection()) {
|
||||
log.info("✅ secondDataSource 预热成功,连接已建立: {}", conn);
|
||||
} catch (Exception e) {
|
||||
log.error("❌ secondDataSource 预热失败", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner mainDataSourcePreWarmer(
|
||||
@Qualifier("masterDataSource") DataSource master,
|
||||
@Qualifier("slaveDataSource") DataSource slave) {
|
||||
return args -> {
|
||||
try {
|
||||
try (Connection c1 = master.getConnection()) {
|
||||
log.info("✅ masterDataSource 预热成功: {}", c1);
|
||||
}
|
||||
try (Connection c2 = slave.getConnection()) {
|
||||
log.info("✅ slaveDataSource 预热成功: {}", c2);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 主从数据源预热失败", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public class HpcHandler implements ExecutionHandler<Map<String, Object>,HPCExecu
|
||||
if(!submitResp.isSuccess()|| StringUtils.isBlank(submitResp.getData())){
|
||||
// 推送失败消息
|
||||
sendMsg(ThreadLocalContext.getTenantId(),ThreadLocalContext.getUserId(),submitHpcTaskRemoteReq.getJobName(),"失败");
|
||||
log.error("HpcHandler submit failed,jobName:{}",params);
|
||||
log.error("HpcHandler submit failed:{}",JSONObject.toJSONString(params));
|
||||
throw new RuntimeException("HpcHandler submit failed,"+submitResp.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package com.sdm.outbridge.config; //// common模块:com.xxx.common.config.CommonConfig
|
||||
|
||||
import com.sdm.common.utils.AESUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.env.YamlPropertySourceLoader;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.io.support.DefaultPropertySourceFactory;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 加载common模块的自定义配置文件
|
||||
@@ -25,7 +31,48 @@ public class CommonConfig {
|
||||
}
|
||||
List<org.springframework.core.env.PropertySource<?>> sources = new YamlPropertySourceLoader()
|
||||
.load(resource.getResource().getFilename(), resource.getResource());
|
||||
return sources.get(0);
|
||||
org.springframework.core.env.PropertySource<?> originalSource = sources.get(0);
|
||||
org.springframework.core.env.PropertySource<?> decryptedSource =
|
||||
decryptPropertySource(originalSource);
|
||||
return decryptedSource;
|
||||
}
|
||||
}
|
||||
|
||||
private static org.springframework.core.env.PropertySource<?> decryptPropertySource(org.springframework.core.env.PropertySource<?> source) {
|
||||
// 只处理可枚举的属性源(yml加载后的源都是EnumerablePropertySource)
|
||||
if (!(source instanceof EnumerablePropertySource)) {
|
||||
return source;
|
||||
}
|
||||
|
||||
EnumerablePropertySource<?> enumerableSource = (EnumerablePropertySource<?>) source;
|
||||
Map<String, Object> decryptedProperties = new HashMap<>();
|
||||
// 遍历所有配置项
|
||||
for (String propertyName : enumerableSource.getPropertyNames()) {
|
||||
Object value = enumerableSource.getProperty(propertyName);
|
||||
if (value != null && value instanceof String) {
|
||||
String strValue = (String) value;
|
||||
if (strValue.startsWith("ENC(") && strValue.endsWith(")")) {
|
||||
// 解密并替换值
|
||||
String spdmEnkey = StringUtils.isBlank(System.getProperty("spdm.enkey"))?
|
||||
System.getenv("spdm.enkey"):System.getProperty("spdm.enkey");
|
||||
String encryptedValue = strValue.substring(4, strValue.length() - 1);
|
||||
try {
|
||||
decryptedProperties.put(propertyName, AESUtil.decodeNew(encryptedValue, spdmEnkey));
|
||||
} catch (Exception e) {
|
||||
System.out.println("利元亨现场配置解密异常:"+e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
// 非加密值,直接保留
|
||||
decryptedProperties.put(propertyName, strValue);
|
||||
}
|
||||
} else {
|
||||
// 非字符串类型,直接保留
|
||||
decryptedProperties.put(propertyName, value);
|
||||
}
|
||||
}
|
||||
// 生成新的属性源(名称和原始源一致,确保覆盖)
|
||||
return new MapPropertySource(source.getName(), decryptedProperties);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user