zjh
2025-02-07 59972ce8f9128858866c17655fc2d034d023b976
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
package com.ltkj.db;
 
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
 
//@Configuration
public class DataSourceConfig {
 
    private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 主库的标识
    private final Map<String, DataSource> dataSourceCache = new HashMap<>(); // 数据源缓存
 
    @Value("${config.properties}")
    private String url;
 
    @Value("${config.path}")
    private String path;
 
    // 从 application.yml 中读取主库的配置
//    @Value("${spring.datasource.url}")
    private String primaryUrl;
 
    private String primaryPort;
 
    //    @Value("${spring.datasource.username}")
    private String primaryUsername;
 
    //    @Value("${spring.datasource.password}")
    private String primaryPassword;
 
    //    @Value("${dbUrl}")
    private String dbUrl;
 
    private String dbName;
 
    @Bean(name = "hospDynamicDataSources")
    public DataSource hospDynamicDataSources() {
        HospDynamicDataSource dynamicDataSource = new HospDynamicDataSource();
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            fis = new FileInputStream(url);
            props.load(fis);
            fis.close();
            dbUrl = props.getProperty("ip");
            dbName = props.getProperty("name");
            primaryPassword = props.getProperty("password");
            primaryPort = props.getProperty("prot");
            primaryUsername = props.getProperty("username");
        } catch (IOException e) {
            throw new RuntimeException("读取配置文件失败", e);
        }
        String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName;
        // 初始化默认数据源为主库
        dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword));
 
        dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 设置默认数据源
        return dynamicDataSource;
    }
 
    // 动态创建数据源
    private DataSource createDataSource(String url, String username, String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
 
    // 根据用户 ID 动态获取数据源
    public void addDataSource(String dbName) {
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            fis = new FileInputStream(url);
            props.load(fis);
            fis.close();
            dbUrl = props.getProperty("ip");
            primaryPassword = props.getProperty("password");
            primaryPort = props.getProperty("prot");
            primaryUsername = props.getProperty("username");
        } catch (IOException e) {
            throw new RuntimeException("读取配置文件失败", e);
        }
 
        // 检查缓存中是否已经存在该数据源
        if (!dataSourceCache.containsKey(dbName)) {
            synchronized (this) {
                String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName;
                DataSource dataSource = createDataSource(url, primaryUsername, primaryPassword);
                dataSourceCache.put(dbName, dataSource);
                HospDynamicDataSource dynamicDataSource = (HospDynamicDataSource) hospDynamicDataSources();
                dynamicDataSource.addTargetDataSource(dbName, dataSource);
            }
        }
    }
}