From b73fedc4026231b31cbc99f9fdad54c8e73a1a34 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期五, 04 七月 2025 17:39:24 +0800 Subject: [PATCH] 2025-0704 --- ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java | 344 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 315 insertions(+), 29 deletions(-) diff --git a/ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java b/ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java index 9d24863..cfdf4b5 100644 --- a/ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java +++ b/ltkj-hosp/src/main/java/com/ltkj/db/DataSourceConfig.java @@ -1,50 +1,92 @@ package com.ltkj.db; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import com.ltkj.common.enums.DataSourceType; +import lombok.extern.slf4j.Slf4j; +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.*; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; +import java.util.Properties; -//@Configuration +@Slf4j +@Configuration public class DataSourceConfig { - - // 浣跨敤浠g爜 -// try { -// long id = Long.parseLong(request.getAttribute("id").toString()); -// DataSourceContextHolder.setDataSourceKey(String.valueOf(id)); -// dataSourceConfig.addDataSource(String.valueOf(id)); // 纭繚鏁版嵁婧愬凡娉ㄥ唽 -// if (log.getSize()>50) log.setSize(50); -// if (log.getLang().equals("zh-cn")) log.setLang("zh"); -// return serverLogService.getLog(log,id); -// }finally { -// DataSourceContextHolder.setDataSourceKey("default"); // 鍒囨崲鍥炰富搴� -// } 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}") +// @Value("${spring.datasource.url}") private String primaryUrl; - @Value("${spring.datasource.username}") + private String primaryPort; + + // @Value("${spring.datasource.username}") private String primaryUsername; - @Value("${spring.datasource.password}") + // @Value("${spring.datasource.password}") private String primaryPassword; - @Value("${dbUrl}") + // @Value("${dbUrl}") private String dbUrl; - @Bean - public DataSource dynamicDataSource() { - DynamicDataSource dynamicDataSource = new DynamicDataSource(); + 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(primaryUrl, primaryUsername, primaryPassword)); + dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword)); + + DruidProperties properties = new DruidProperties(); + DataSource masterDataSource = masterDataSource(properties); + dynamicDataSource.addTargetDataSource(DataSourceType.MASTER.name(),masterDataSource); + dataSourceCache.put(DataSourceType.MASTER.name(), masterDataSource); + + DataSource hisDataSource = slaveHisDataSource(properties); + dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_HIS.name(),hisDataSource); + dataSourceCache.put(DataSourceType.SLAVE_HIS.name(), hisDataSource); + + DataSource slaveDataLisSource = slaveDataLisSource(properties); + dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_LIS.name(),slaveDataLisSource); + dataSourceCache.put(DataSourceType.SLAVE_LIS.name(), slaveDataLisSource); + + DataSource dataPacsSource = slaveDataPacsSource(properties); + dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_PACS.name(),dataPacsSource); + dataSourceCache.put(DataSourceType.SLAVE_PACS.name(), dataPacsSource); + + DataSource wsSource = slaveDataWsSource(properties); + dynamicDataSource.addTargetDataSource(DataSourceType.SLAVE_WS.name(),wsSource); + dataSourceCache.put(DataSourceType.SLAVE_WS.name(), wsSource); dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 璁剧疆榛樿鏁版嵁婧� return dynamicDataSource; @@ -60,15 +102,259 @@ } // 鏍规嵁鐢ㄦ埛 ID 鍔ㄦ�佽幏鍙栨暟鎹簮 - public void addDataSource(String userId) { - String dbName = "scum_admin_user_" + userId; + 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(userId)) { - String url = "jdbc:mysql://"+dbUrl+":3306/"+dbName; - DataSource dataSource = createDataSource(url, primaryUsername, primaryPassword); - dataSourceCache.put(userId, dataSource); - DynamicDataSource dynamicDataSource = (DynamicDataSource) dynamicDataSource(); - dynamicDataSource.addTargetDataSource(userId, dataSource); + 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); + } } } + + private DataSource masterDataSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(url); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌锛�"); + } + props.load(fis); + fis.close(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + String hisenabled = props.getProperty("hisenabled"); + if (hisenabled.equals("false")) + return null; + dataSource = creatMysql(hisenabled, props.getProperty("ip"),props.getProperty("prot"),props.getProperty("name"),props.getProperty("username"),props.getProperty("password")); + log.info("his鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + private DataSource slaveHisDataSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(url); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌锛�"); + } + props.load(fis); + fis.close(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + String hisenabled = props.getProperty("hisenabled"); + if (hisenabled.equals("false")) + return null; + String hisdbtype = props.getProperty("hisdbtype"); + switch (hisdbtype){ + case "sqlserver": + dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); + break; + case "mysql": + dataSource = creatMysql(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); + break; + case "oracle": + dataSource = creatOracle(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); + break; + default: + dataSource = creatSqlServer(hisenabled, props.getProperty("hisip"),props.getProperty("hisprot"),props.getProperty("hisname"),props.getProperty("hisusername"),props.getProperty("hispassword")); + break; + } + log.info("his鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + private DataSource slaveDataLisSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(url); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌锛�"); + } + props.load(fis); + fis.close(); + String lisenabled = props.getProperty("lisenabled"); + if (lisenabled.equals("false")) + return null; + String lisdbtype = props.getProperty("lisdbtype"); + switch (lisdbtype){ + case "sqlserver": + dataSource = creatSqlServer(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); + break; + case "mysql": + dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); + break; + case "oracle": + dataSource = creatOracle(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); + break; + default: + dataSource = creatMysql(lisenabled, props.getProperty("lisip"),props.getProperty("lisprot"),props.getProperty("lisname"),props.getProperty("lisusername"),props.getProperty("lispassword")); + break; + } + log.info("Lis鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + private DataSource slaveDataPacsSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(url); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌"); + } + props.load(fis); + fis.close(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + String pacsenabled = props.getProperty("pacsenabled"); + if (pacsenabled.equals("false")) + return null; + String pacsdbtype = props.getProperty("pacsdbtype"); + switch (pacsdbtype){ + case "sqlserver": + dataSource = creatSqlServer(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); + break; + case "mysql": + dataSource = creatMysql(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); + break; + case "oracle": + dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); + break; + default: + dataSource = creatOracle(pacsenabled, props.getProperty("pacsip"),props.getProperty("pacsprot"),props.getProperty("pacsname"),props.getProperty("pacsusername"),props.getProperty("pacspassword")); + break; + } + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + private DataSource slaveDataWsSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(url); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌"); + } + props.load(fis); + fis.close(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + String pacsenabled = props.getProperty("wsenabled"); + if (pacsenabled.equals("false")) + return null; + String pacsdbtype = props.getProperty("wsdbtype"); + switch (pacsdbtype){ + case "sqlserver": + dataSource = creatSqlServer(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"), + props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword")); + break; + case "mysql": + dataSource = creatMysql(pacsenabled, props.getProperty("wsip"), + props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword")); + break; + case "oracle": + dataSource = creatOracle(pacsenabled, props.getProperty("wsip"), + props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername"),props.getProperty("wspassword")); + break; + default: + dataSource = creatOracle(pacsenabled, props.getProperty("wsip"),props.getProperty("wsprot"),props.getProperty("wsname"),props.getProperty("wsusername") + ,props.getProperty("wspassword")); + break; + } + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + + + private DruidDataSource creatSqlServer(String enabled, String ip,String port,String db,String user,String password) throws SQLException { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties properties = new Properties(); + properties.setProperty("druid.enabled", enabled); + properties.setProperty("druid.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver"); + properties.setProperty("druid.url","jdbc:sqlserver://"+ ip+":"+ port+";DatabaseName="+ db+ + ";&characterEncoding=utf8"); + properties.setProperty("druid.username", user); + properties.setProperty("druid.password", password); + dataSource.restart(properties); + return dataSource; + } + + private DruidDataSource creatMysql(String enabled, String ip,String port,String db,String user,String password) throws SQLException { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties properties = new Properties(); + properties.setProperty("druid.enabled",enabled); + properties.setProperty("druid.url","jdbc:mysql://"+ip+":"+port+"/"+db+"" + + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); + properties.setProperty("druid.username",user); + properties.setProperty("druid.password",password); + dataSource.restart(properties); + return dataSource; + } + + private DruidDataSource creatOracle(String enabled, String ip,String port,String db,String user,String password) throws SQLException { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties properties = new Properties(); + properties.setProperty("druid.enabled",enabled); + properties.setProperty("druid.driverClassName","oracle.jdbc.OracleDriver"); + properties.setProperty("druid.url","jdbc:oracle:thin:@//"+ip+"/"+db); + properties.setProperty("druid.username",user); + properties.setProperty("druid.password",password); + dataSource.restart(properties); + return dataSource; + } + } -- Gitblit v1.8.0