From 965a994c0fe4c59a72638a6a4f1bb28dfd66f655 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期二, 17 十二月 2024 20:06:25 +0800 Subject: [PATCH] 20241217 --- src/main/java/com/example/config/DruidConfig.java | 129 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 129 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/example/config/DruidConfig.java b/src/main/java/com/example/config/DruidConfig.java index dba1ede..9671b24 100644 --- a/src/main/java/com/example/config/DruidConfig.java +++ b/src/main/java/com/example/config/DruidConfig.java @@ -4,6 +4,9 @@ import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; import com.alibaba.druid.util.Utils; +import com.example.datasource.DynamicDataSource; +import com.example.enums.DataSourceType; +import com.example.utils.SpringUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -106,6 +109,132 @@ return druidProperties.dataSource(dataSource); } + @Bean +// @ConfigurationProperties("spring.datasource.druid.slavehis") +// @ConditionalOnProperty(prefix = "spring.datasource.druid.slavehis", name = "enabled", havingValue = "true") + public DataSource slaveHisDataSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(CONFIG_PATH); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌锛�"); + } + props.load(fis); + fis.close(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",props.getProperty("hisenabled")); + properties.setProperty("druid.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver"); + properties.setProperty("druid.url","jdbc:sqlserver://"+props.getProperty("hisip")+":"+props.getProperty("hisprot")+";DatabaseName="+props.getProperty("hisname")+ + ";&characterEncoding=utf8"); + properties.setProperty("druid.username",props.getProperty("hisusername")); + properties.setProperty("druid.password",props.getProperty("hispassword")); + dataSource.restart(properties); + log.info("his鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + @Bean +// @ConfigurationProperties("spring.datasource.druid.slavelis") +// @ConditionalOnProperty(prefix = "spring.datasource.druid.slavelis", name = "enabled", havingValue = "true") + public DataSource slaveDataLisSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(CONFIG_PATH); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌锛�"); + } + props.load(fis); + fis.close(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",props.getProperty("lisenabled")); + properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("lisip")+":"+props.getProperty("lisprot")+"/"+props.getProperty("lisname")+"" + + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); + properties.setProperty("druid.username",props.getProperty("lisusername")); + properties.setProperty("druid.password",props.getProperty("lispassword")); + dataSource.restart(properties); + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + @Bean +// @ConfigurationProperties("spring.datasource.druid.slavepacs") +// @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true") + public DataSource slaveDataPacsSource(DruidProperties druidProperties) { + DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); + Properties props = new Properties(); + try { + // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 + FileInputStream fis = null; + try { + fis = new FileInputStream(CONFIG_PATH); + } catch (FileNotFoundException e) { + log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌"); + } + props.load(fis); + fis.close(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",props.getProperty("pacsenabled")); + properties.setProperty("druid.driverClassName","oracle.jdbc.OracleDriver"); + properties.setProperty("druid.url","jdbc:oracle:thin:@//"+props.getProperty("pacsip")+"/"+props.getProperty("pacsname")); + properties.setProperty("druid.username",props.getProperty("pacsusername")); + properties.setProperty("druid.password",props.getProperty("pacspassword")); + dataSource.restart(properties); + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.info("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + return druidProperties.dataSource(dataSource); + } + + @Bean(name = "dynamicDataSource") + @Primary + public DynamicDataSource dataSource(DataSource masterDataSource) { + Map<Object, Object> targetDataSources = new HashMap<>(); + targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); + setDataSource(targetDataSources, DataSourceType.SLAVE_HIS.name(), "slaveHisDataSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_LIS.name(), "slaveDataLisSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_PACS.name(), "slaveDataPacsSource"); + return new DynamicDataSource(masterDataSource, targetDataSources); + } + + /** + * 璁剧疆鏁版嵁婧� + * + * @param targetDataSources 澶囬�夋暟鎹簮闆嗗悎 + * @param sourceName 鏁版嵁婧愬悕绉� + * @param beanName bean鍚嶇О + */ + public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) { + try { + DataSource dataSource = SpringUtils.getBean(beanName); + targetDataSources.put(sourceName, dataSource); + } catch (Exception e) { + } + } + /** * 鍘婚櫎鐩戞帶椤甸潰搴曢儴鐨勫箍鍛� */ -- Gitblit v1.8.0