From 59972ce8f9128858866c17655fc2d034d023b976 Mon Sep 17 00:00:00 2001 From: zjh <1084500556@qq.com> Date: 星期五, 07 二月 2025 17:33:15 +0800 Subject: [PATCH] zjh20250207 --- ltkj-framework/src/main/java/com/ltkj/framework/config/DruidConfig.java | 92 +++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 86 insertions(+), 6 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 a3924ad..df79300 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 @@ -5,7 +5,6 @@ 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; @@ -14,14 +13,12 @@ import javax.servlet.ServletResponse; import javax.sql.DataSource; +import com.ltkj.db.HospDynamicDataSource; import com.ltkj.framework.config.properties.DruidProperties; import com.ltkj.framework.datasource.DynamicDataSource; -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; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -48,6 +45,28 @@ @Value ("${config.path}") private String path; + + + private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 涓诲簱鐨勬爣璇� + private final Map<String, DataSource> dataSourceCache = new HashMap<>(); // 鏁版嵁婧愮紦瀛� + + + // 浠� 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 @@ -259,8 +278,6 @@ return druidProperties.dataSource(dataSource); } - - @Bean // @ConfigurationProperties("spring.datasource.druid.slavepacs") // @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true") @@ -346,6 +363,69 @@ return dataSource; } +// @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; +// } + + // 鏍规嵁鐢ㄦ埛 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); + + DynamicDataSource dynamicDataSource = dataSource(dataSource); + dynamicDataSource.addTargetDataSource(dbName, dataSource); + } + } + } + + // 鍔ㄦ�佸垱寤烘暟鎹簮 + private DataSource createDataSource(String url, String username, String password) { + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + return dataSource; + } + @Bean(name = "dynamicDataSource") @Primary -- Gitblit v1.8.0