From 2b39b654975aa9cb40254f20e545b9f389a82de7 Mon Sep 17 00:00:00 2001 From: zjh <1084500556@qq.com> Date: 星期五, 24 一月 2025 18:24:33 +0800 Subject: [PATCH] zjh20250124 --- ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java | 197 ++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 162 insertions(+), 35 deletions(-) diff --git a/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java b/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java index 21a21ab..796a281 100644 --- a/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java +++ b/ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java @@ -1,12 +1,14 @@ package com.ltkj.framework.config; import java.io.*; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.annotation.Resource; import javax.servlet.Filter; import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -17,6 +19,7 @@ import com.ltkj.system.service.ISysConfigService; 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.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; @@ -39,6 +42,13 @@ @Slf4j public class DruidConfig { + @Value ("${config.properties}") + private String url; + + @Value ("${config.path}") + private String path; + + @Bean // @ConfigurationProperties("spring.datasource.druid.master") public DataSource masterDataSource(DruidProperties druidProperties) { @@ -48,15 +58,15 @@ // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 FileInputStream fis = null; try { - fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + fis = new FileInputStream(url); } catch (FileNotFoundException e) { log.info("鏁版嵁搴撹繛鎺ユ枃浠舵壘涓嶅埌 绯荤粺姝e湪鍒涘缓锛�"); - File f = new File("D:\\ltkjprojectconf"); + File f = new File(path); if(!f.exists()){ f.mkdirs(); } - File file = new File("D:\\ltkjprojectconf\\config.properties"); + File file = new File(url); try { FileWriter fileWriter = new FileWriter(file); fileWriter.write("ip = 浣犵殑涓绘暟鎹簱杩炴帴ip鍦板潃\n"); @@ -108,7 +118,7 @@ Properties properties = new Properties(); // 杩欓噷鏄祴璇曞啓娉曪紝鍏蜂綋鐨剉alue鍙互閫氳繃璇锋眰鍙傛暟浼犻�掕繃鏉� properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("ip")+":"+props.getProperty("prot")+"/"+props.getProperty("name")+"" + - "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"); properties.setProperty("druid.username",props.getProperty("username")); properties.setProperty("druid.password",props.getProperty("password")); dataSource.restart(properties); @@ -130,22 +140,32 @@ // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 FileInputStream fis = null; try { - fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + fis = new FileInputStream(url); } 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")); - properties.setProperty("druid.username",props.getProperty("hisusername")); - properties.setProperty("druid.password",props.getProperty("hispassword")); - dataSource.restart(properties); - log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + 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(); @@ -163,22 +183,31 @@ // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 FileInputStream fis = null; try { - fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + fis = new FileInputStream(url); } 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.driverClassName","com.microsoft.sqlserver.jdbc.SQLServerDriver"); - properties.setProperty("druid.url","jdbc:sqlserver://"+props.getProperty("lisip")+":"+props.getProperty("lisprot")+";DatabaseName="+props.getProperty("lisname")); - properties.setProperty("druid.username",props.getProperty("lisusername")); - properties.setProperty("druid.password",props.getProperty("lispassword")); - dataSource.restart(properties); - log.info("鏁版嵁搴撹繛鎺ユ垚鍔�!!!"); + 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(); @@ -196,21 +225,31 @@ // 浠庢枃浠朵腑璇诲彇閰嶇疆淇℃伅 FileInputStream fis = null; try { - fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); + fis = new FileInputStream(url); } 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","com.microsoft.sqlserver.jdbc.SQLServerDriver"); - properties.setProperty("druid.url","jdbc:sqlserver://"+props.getProperty("pacsip")+":"+props.getProperty("pacsprot")+";DatabaseName="+props.getProperty("pacsname")); - properties.setProperty("druid.username",props.getProperty("pacsusername")); - properties.setProperty("druid.password",props.getProperty("pacspassword")); - dataSource.restart(properties); + 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("鏁版嵁搴撹繛鎺ュけ璐� 璇疯仈绯荤鐞嗗憳锛�"); @@ -218,6 +257,93 @@ } return druidProperties.dataSource(dataSource); } + + + @Bean +// @ConfigurationProperties("spring.datasource.druid.slavepacs") +// @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true") + public 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("pacsenabled"); + 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+ + ";encrypt=true;trustServerCertificate=true;sslProtocol=TLSv1.2;"); + 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; + } + @Bean(name = "dynamicDataSource") @Primary @@ -227,6 +353,7 @@ setDataSource(targetDataSources, DataSourceType.SLAVE_HIS.name(), "slaveHisDataSource"); setDataSource(targetDataSources, DataSourceType.SLAVE_LIS.name(), "slaveDataLisSource"); setDataSource(targetDataSources, DataSourceType.SLAVE_PACS.name(), "slaveDataPacsSource"); + setDataSource(targetDataSources, DataSourceType.SLAVE_WS.name(), "slaveDataWsSource"); return new DynamicDataSource(masterDataSource, targetDataSources); } @@ -261,7 +388,7 @@ // 鍒涘缓filter杩涜杩囨护 Filter filter = new Filter() { @Override - public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { + public void init(FilterConfig filterConfig) throws ServletException { } @Override -- Gitblit v1.8.0