| | |
| | | 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.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; |
| | |
| | | |
| | | @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 |
| | |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | |
| | | |
| | | @Bean |
| | | // @ConfigurationProperties("spring.datasource.druid.slavepacs") |
| | | // @ConditionalOnProperty(prefix = "spring.datasource.druid.slavepacs", name = "enabled", havingValue = "true") |
| | |
| | | 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 |