优化中间库配置加载逻辑,新增启动时加载最近更新的配置功能,提升系统稳定性和灵活性

This commit is contained in:
geht
2026-06-17 10:58:34 +08:00
parent e28352f8ea
commit ba77a95554
5 changed files with 36 additions and 15 deletions

View File

@@ -1093,8 +1093,12 @@ jeecgboot-vue3/src/views/xslmes/mesXslEquipDowntimeRecord/MesXslEquipDowntimeRec
jeecgboot-vue3/src/views/xslmes/mesXslEquipDowntimeRecord/MesXslEquipDowntimeRecordList.vue
jeecgboot-vue3/src/views/xslmes/mesXslEquipDowntimeRecord/components/MesXslEquipDowntimeRecordModal.vue
-- author:GHT---date:20260616--for: 【MES上辅机】生产环境 SQL Server 中间库连接池校验 DUAL 报错修复 ---
原因application-prod.yml 全局 druid.validationQuery 为 SELECT 1 FROM DUAL动态创建的 sqlserver_mcs 数据源继承该配置后在 SQL Server 上报「对象名 DUAL 无效」。
修改prod 全局改为 SELECT 1McsDataSourceManager 创建/恢复 SQL Server 数据源时显式设置 validationQuery=SELECT 1。
jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-prod.yml
jeecg-boot/jeecg-boot-module/jeecg-module-xslmes/src/main/java/org/jeecg/modules/xslmes/mcs/datasource/McsDataSourceManager.java
-- author:GHT---date:20260616--for: 【MES上辅机】重启后中间库配置未自动加载租户ID硬编码为0---
原因McsDataSourceInitializer 启动时只查 tenant_id=0页面保存用当前租户如1002重启后查不到配置连接状态回退为 yml
修改:新增 loadStartupConfig 按最近更新时间加载任意租户配置isDbConfigActive 同时校验数据源是否已注册。
jeecg-boot/jeecg-boot-module/jeecg-module-xslmes/src/main/java/org/jeecg/modules/xslmes/mcs/service/IMesXslMcsDbConfigService.java
jeecg-boot/jeecg-boot-module/jeecg-module-xslmes/src/main/java/org/jeecg/modules/xslmes/mcs/service/impl/MesXslMcsDbConfigServiceImpl.java
jeecg-boot/jeecg-boot-module/jeecg-module-xslmes/src/main/java/org/jeecg/modules/xslmes/mcs/config/McsDataSourceInitializer.java
jeecg-boot/jeecg-boot-module/jeecg-module-xslmes/src/main/java/org/jeecg/modules/xslmes/mcs/datasource/McsDataSourceManager.java

View File

@@ -28,20 +28,16 @@ public class McsDataSourceInitializer implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
try {
MesXslMcsDbConfig config = mcsDbConfigService.getOne(
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<MesXslMcsDbConfig>()
.eq(MesXslMcsDbConfig::getTenantId, 0)
.orderByDesc(MesXslMcsDbConfig::getUpdateTime)
.last("LIMIT 1"),
false);
MesXslMcsDbConfig config = mcsDbConfigService.loadStartupConfig();
if (config != null) {
mcsDataSourceManager.applyConfig(config);
log.info("[MCS中间库] 已加载数据库中的中间库配置(含读写开关)");
log.info("[MCS中间库] 已加载数据库中的中间库配置 tenantId={}, status={}, host={}:{}",
config.getTenantId(), config.getStatus(), config.getServerHost(), config.getServerPort());
} else {
log.info("[MCS中间库] 未找到已启用的数据库配置,继续使用 application.yml 中的 sqlserver_mcs");
log.info("[MCS中间库] 数据库中无中间库配置,继续使用 application.yml 中的 sqlserver_mcs");
}
} catch (Exception e) {
log.warn("[MCS中间库] 启动加载配置失败,将使用 yml 默认连接: {}", e.getMessage());
log.error("[MCS中间库] 启动加载配置失败,将使用 yml 默认连接", e);
}
}
//update-end---author:GHT ---date:20260616 for【MES上辅机】启动时加载中间库可视化配置-----------

View File

@@ -59,7 +59,11 @@ public class McsDataSourceManager {
public boolean isDbConfigActive() {
MesXslMcsDbConfig cfg = cachedConfig.get();
return cfg != null && Integer.valueOf(1).equals(cfg.getStatus());
if (cfg == null || !Integer.valueOf(1).equals(cfg.getStatus())) {
return false;
}
DynamicRoutingDataSource routing = (DynamicRoutingDataSource) dataSource;
return routing.getDataSources().containsKey(DS_KEY);
}
public boolean isReadEnabled() {
@@ -83,11 +87,11 @@ public class McsDataSourceManager {
* 将配置应用到动态数据源(启用时注册/更新,禁用时移除)
*/
public void applyConfig(MesXslMcsDbConfig config) {
cachedConfig.set(config);
DynamicRoutingDataSource routing = (DynamicRoutingDataSource) dataSource;
closeAndRemoveDataSource(routing);
DataSourceCachePool.removeCache(DS_KEY);
if (config == null) {
cachedConfig.set(null);
restoreYmlDataSource(routing);
log.info("[MCS中间库] 已清除页面配置");
return;
@@ -98,6 +102,7 @@ public class McsDataSourceManager {
DataSourceProperty property = buildDataSourceProperty(config, plainPassword);
DataSource mcsDs = dataSourceCreator.createDataSource(property);
routing.addDataSource(DS_KEY, mcsDs);
cachedConfig.set(config);
log.info("[MCS中间库] 动态数据源 {} 已热刷新(无需重启): {}:{}/{}", DS_KEY,
config.getServerHost(), config.getServerPort(), config.getDbName());
} catch (Exception e) {
@@ -107,6 +112,7 @@ public class McsDataSourceManager {
}
return;
}
cachedConfig.set(config);
restoreYmlDataSource(routing);
log.info("[MCS中间库] 连接未启用,读写开关仍按页面配置生效");
}

View File

@@ -28,4 +28,9 @@ public interface IMesXslMcsDbConfigService extends IService<MesXslMcsDbConfig> {
* 删除配置并移除动态数据源
*/
Result<String> deleteConfig(String id);
/**
* 启动时加载中间库配置(不限定租户,取最近更新的一条)
*/
MesXslMcsDbConfig loadStartupConfig();
}

View File

@@ -44,6 +44,16 @@ public class MesXslMcsDbConfigServiceImpl extends ServiceImpl<MesXslMcsDbConfigM
return config;
}
//update-begin---author:GHT ---date:20260616 for【MES上辅机】启动时加载中间库配置不限定租户-----------
@Override
public MesXslMcsDbConfig loadStartupConfig() {
LambdaQueryWrapper<MesXslMcsDbConfig> qw = new LambdaQueryWrapper<>();
qw.orderByDesc(MesXslMcsDbConfig::getUpdateTime);
qw.last("LIMIT 1");
return getOne(qw, false);
}
//update-end---author:GHT ---date:20260616 for【MES上辅机】启动时加载中间库配置不限定租户-----------
//update-begin---author:GHT ---date:20260616 for【MES上辅机】SQL Server中间库可视化配置-----------
@Override
@Transactional(rollbackFor = Exception.class)