quling
2026-08-22 9f19520e742ec66226466fd72116a77b81d5eb32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.tcm.training.admin.bootstrap;
 
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Component;
 
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
 
/**
 * 启动时校验海诊通动态数据源配置,避免 @DS 未命中时静默回退主库。
 */
@Component
public class HaizhentongDataSourceStartupValidator implements ApplicationRunner {
 
    private static final Logger log = LoggerFactory.getLogger(HaizhentongDataSourceStartupValidator.class);
    private static final String HAIZHENTONG_DATASOURCE_NAME = "haizhentong";
    private static final String MISSING_DATASOURCE_MESSAGE =
            "海诊通数据源 haizhentong 未配置,请检查当前激活环境的配置文件";
 
    private final DynamicDataSourceProperties dynamicDataSourceProperties;
    private final ObjectProvider<DynamicRoutingDataSource> dynamicRoutingDataSourceProvider;
 
    /**
     * 注入启动校验所需的配置环境与主数据源代理。
     */
    public HaizhentongDataSourceStartupValidator(
            DynamicDataSourceProperties dynamicDataSourceProperties,
            ObjectProvider<DynamicRoutingDataSource> dynamicRoutingDataSourceProvider
    ) {
        this.dynamicDataSourceProperties = dynamicDataSourceProperties;
        this.dynamicRoutingDataSourceProvider = dynamicRoutingDataSourceProvider;
    }
 
    /**
     * 在应用启动阶段校验海诊通数据源是否已配置且已注册到动态路由中。
     */
    @Override
    public void run(ApplicationArguments args) {
        Set<String> configuredDataSourceNames = getConfiguredDataSourceNames();
        validateConfiguredDataSourceNames(configuredDataSourceNames);
 
        DynamicRoutingDataSource dynamicRoutingDataSource = dynamicRoutingDataSourceProvider.getIfAvailable();
        if (dynamicRoutingDataSource == null) {
            log.warn("未获取到 DynamicRoutingDataSource Bean,跳过海诊通数据源运行期注册校验,configured={}",
                    configuredDataSourceNames);
            return;
        }
 
        Set<String> registeredDataSourceNames = getRegisteredDataSourceNames(dynamicRoutingDataSource);
        validateRegisteredDataSourceNames(registeredDataSourceNames);
 
        log.info("海诊通动态数据源校验通过,configured={}, registered={}",
                configuredDataSourceNames, registeredDataSourceNames);
    }
 
    /**
     * 读取配置文件中声明的动态数据源名称列表。
     */
    private Set<String> getConfiguredDataSourceNames() {
        Map<String, ?> datasourceMap = dynamicDataSourceProperties.getDatasource();
        if (datasourceMap == null) {
            return Collections.emptySet();
        }
        return new LinkedHashSet<String>(datasourceMap.keySet());
    }
 
    /**
     * 校验配置文件中已显式声明海诊通数据源。
     */
    private void validateConfiguredDataSourceNames(Set<String> configuredDataSourceNames) {
        if (configuredDataSourceNames.contains(HAIZHENTONG_DATASOURCE_NAME)) {
            return;
        }
        throw new IllegalStateException(MISSING_DATASOURCE_MESSAGE);
    }
 
    /**
     * 通过动态数据源路由器反射读取运行期已注册的数据源名称。
     */
    private Set<String> getRegisteredDataSourceNames(DynamicRoutingDataSource dynamicRoutingDataSource) {
        Map<String, ?> datasourceMap = dynamicRoutingDataSource.getDataSources();
        if (datasourceMap == null) {
            throw new IllegalStateException("动态数据源路由未返回有效的数据源注册表");
        }
        Set<String> datasourceNames = new LinkedHashSet<String>();
        for (Object key : datasourceMap.keySet()) {
            datasourceNames.add(String.valueOf(key));
        }
        return datasourceNames;
    }
 
    /**
     * 校验海诊通数据源已被动态数据源组件成功注册。
     */
    private void validateRegisteredDataSourceNames(Set<String> registeredDataSourceNames) {
        if (registeredDataSourceNames.contains(HAIZHENTONG_DATASOURCE_NAME)) {
            return;
        }
        throw new IllegalStateException("海诊通数据源 haizhentong 未注册成功,请检查多数据源配置");
    }
}