From 9449b5f71bd0c20ed9aefd025ee87c69bcdfbd40 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期三, 18 十二月 2024 14:51:12 +0800 Subject: [PATCH] 20241218 --- src/main/java/com/example/config/DruidConfig.java | 166 ++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 108 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/example/config/DruidConfig.java b/src/main/java/com/example/config/DruidConfig.java index dba1ede..a444748 100644 --- a/src/main/java/com/example/config/DruidConfig.java +++ b/src/main/java/com/example/config/DruidConfig.java @@ -4,7 +4,11 @@ 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.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.web.servlet.FilterRegistrationBean; @@ -28,75 +32,95 @@ @Slf4j @Configuration public class DruidConfig { - - @Value("${config.path}") - private String CONFIG_PATH; - @Value("${config.dir}") - private String CONFIG_DIR; + @Autowired + private ConfigValue configValue; private DruidDataSource dataSource; @Bean public DataSource masterDataSource(DruidProperties druidProperties){ dataSource = DruidDataSourceBuilder.create().build(); - Properties props = new Properties(); + Map<String, String> configMap = configValue.getConfigMap(); try { - // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 - FileInputStream fis = null; - try { - fis = new FileInputStream(CONFIG_PATH); - } catch (FileNotFoundException e) { - log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌 绯荤粺姝e湪鍒涘缓锛�"); - File f = new File(CONFIG_DIR); - if(!f.exists()){ - f.mkdirs(); - } - File file = new File(CONFIG_PATH); - try { - FileWriter fileWriter = new FileWriter(file); - fileWriter.write("ip = 浣犵殑涓绘暟鎹簱杩炴帴ip鍦板潃\n"); - fileWriter.write("prot = 浣犵殑涓绘暟鎹簱杩炴帴绔彛\n"); - fileWriter.write("name = 浣犵殑涓绘暟鎹簱杩炴帴鍚嶇О\n"); - fileWriter.write("username = 浣犵殑涓绘暟鎹簱杩炴帴鐢ㄦ埛鍚峔n"); - fileWriter.write("password = 浣犵殑涓绘暟鎹簱杩炴帴瀵嗙爜\n"); - fileWriter.write("\n"); - fileWriter.write("hisenabled = 鏄惁寮�鍚� his 浠庡簱鏁版嵁搴撹繛鎺� (浠庢暟鎹簮寮�鍏�/榛樿鍏抽棴 fales)\n"); - fileWriter.write("hisip = 浣犵殑 his 鏁版嵁搴撹繛鎺p鍦板潃\n"); - fileWriter.write("hisprot = 浣犵殑 his 鏁版嵁搴撹繛鎺ョ鍙n"); - fileWriter.write("hisname = 浣犵殑 his 鏁版嵁搴撹繛鎺ュ悕绉癨n"); - fileWriter.write("hisusername = 浣犵殑 his 鏁版嵁搴撹繛鎺ョ敤鎴峰悕\n"); - fileWriter.write("hispassword = 浣犵殑 his 鏁版嵁搴撹繛鎺ュ瘑鐮乗n"); - fileWriter.write("\n"); - fileWriter.write("pacsenabled = 鏄惁寮�鍚痯acs浠庡簱鏁版嵁搴撹繛鎺� (浠庢暟鎹簮寮�鍏�/榛樿鍏抽棴 fales)\n"); - fileWriter.write("pacsip = 浣犵殑pacs鏁版嵁搴撹繛鎺p鍦板潃\n"); - fileWriter.write("pacsprot = 浣犵殑pacs鏁版嵁搴撹繛鎺ョ鍙n"); - fileWriter.write("pacsname = 浣犵殑pacs鏁版嵁搴撹繛鎺ュ悕绉癨n"); - fileWriter.write("pacsusername = 浣犵殑pacs鏁版嵁搴撹繛鎺ョ敤鎴峰悕\n"); - fileWriter.write("pacspassword = 浣犵殑pacs鏁版嵁搴撹繛鎺ュ瘑鐮乗n"); - fileWriter.write("\n"); - fileWriter.write("lisenabled = 鏄惁寮�鍚� lis 浠庡簱鏁版嵁搴撹繛鎺� (浠庢暟鎹簮寮�鍏�/榛樿鍏抽棴 fales)\n"); - fileWriter.write("lisip = 浣犵殑 lis 鏁版嵁搴撹繛鎺p鍦板潃\n"); - fileWriter.write("lisprot = 浣犵殑 lis 鏁版嵁搴撹繛鎺ョ鍙n"); - fileWriter.write("lisname = 浣犵殑 lis 鏁版嵁搴撹繛鎺ュ悕绉癨n"); - fileWriter.write("lisusername = 浣犵殑 lis 鏁版嵁搴撹繛鎺ョ敤鎴峰悕\n"); - fileWriter.write("lispassword = 浣犵殑 lis 鏁版嵁搴撹繛鎺ュ瘑鐮乗n"); - fileWriter.close(); - log.info("鏁版嵁搴撹繛鎺ユ枃浠跺垱寤烘垚鍔燂紒"); - } catch (IOException ioException) { - log.info("鏁版嵁搴撹繛鎺ユ枃浠跺垱寤哄け璐� 璇疯仈绯荤鐞嗗憳鎵嬪姩鍒涘缓锛�"); - ioException.printStackTrace(); - } - e.printStackTrace(); - } - props.load(fis); - fis.close(); // 鑾峰彇灞炴�у�煎苟璧嬪�� Properties properties = new Properties(); // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� - properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("ip")+":"+props.getProperty("prot")+"/"+props.getProperty("name")+"" + + properties.setProperty("druid.url","jdbc:mysql://"+configMap.get("ip")+":"+configMap.get("prot")+"/"+configMap.get("name")+"" + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); - properties.setProperty("druid.username",props.getProperty("username")); - properties.setProperty("druid.password",props.getProperty("password")); + properties.setProperty("druid.username",configMap.get("username")); + properties.setProperty("druid.password",configMap.get("password")); + dataSource.restart(properties); + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.error("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + e.printStackTrace(); + } + 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(); + Map<String, String> configMap = configValue.getConfigMap(); + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",configMap.get("hisenabled")); + properties.setProperty("druid.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver"); + properties.setProperty("druid.url","jdbc:sqlserver://"+configMap.get("hisip")+":"+configMap.get("hisprot")+";DatabaseName="+configMap.get("hisname")+ + ";&characterEncoding=utf8"); + properties.setProperty("druid.username",configMap.get("hisusername")); + properties.setProperty("druid.password",configMap.get("hispassword")); + try { + dataSource.restart(properties); + log.info("his鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (SQLException e) { + log.error("his鏁版嵁搴撹繛鎺ュけ璐�!!!"); + } + 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(); + Map<String, String> configMap = configValue.getConfigMap(); + try { + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",configMap.get("lisenabled")); + properties.setProperty("druid.url","jdbc:mysql://"+configMap.get("lisip")+":"+configMap.get("lisprot")+"/"+configMap.get("lisname")+"" + + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); + properties.setProperty("druid.username",configMap.get("lisusername")); + properties.setProperty("druid.password",configMap.get("lispassword")); + dataSource.restart(properties); + log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + } catch (Exception e) { + log.error("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); + 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(); + Map<String, String> configMap = configValue.getConfigMap(); + try { + // 鑾峰彇灞炴�у�煎苟璧嬪�� + Properties properties = new Properties(); + // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� + properties.setProperty("druid.enabled",configMap.get("pacsenabled")); + properties.setProperty("druid.driverClassName","oracle.jdbc.OracleDriver"); + properties.setProperty("druid.url","jdbc:oracle:thin:@//"+configMap.get("pacsip")+"/"+configMap.get("pacsname")); + properties.setProperty("druid.username",configMap.get("pacsusername")); + properties.setProperty("druid.password",configMap.get("pacspassword")); dataSource.restart(properties); log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); } catch (Exception e) { @@ -106,6 +130,32 @@ 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