优化中间库配置加载逻辑,新增启动时加载最近更新的配置功能,提升系统稳定性和灵活性
This commit is contained in:
@@ -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 1;McsDataSourceManager 创建/恢复 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
|
||||
|
||||
@@ -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上辅机】启动时加载中间库可视化配置-----------
|
||||
|
||||
@@ -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中间库] 连接未启用,读写开关仍按页面配置生效");
|
||||
}
|
||||
|
||||
@@ -28,4 +28,9 @@ public interface IMesXslMcsDbConfigService extends IService<MesXslMcsDbConfig> {
|
||||
* 删除配置并移除动态数据源
|
||||
*/
|
||||
Result<String> deleteConfig(String id);
|
||||
|
||||
/**
|
||||
* 启动时加载中间库配置(不限定租户,取最近更新的一条)
|
||||
*/
|
||||
MesXslMcsDbConfig loadStartupConfig();
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user