New file |
| | |
| | | package com.example.config.db; |
| | | |
| | | import com.alibaba.druid.pool.DruidDataSource; |
| | | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
| | | import com.example.config.ConfigValue; |
| | | import com.example.config.DruidProperties; |
| | | import com.example.enums.DataSourceType; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import java.sql.SQLException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | import javax.sql.DataSource; |
| | | |
| | | @Slf4j |
| | | @Configuration |
| | | public class DataSourceConfig { |
| | | |
| | | private static final String DEFAULT_DATA_SOURCE_KEY = "default"; // 主库的标识 |
| | | private final Map<String, DataSource> dataSourceCache = new HashMap<>(); // 数据源缓存 |
| | | |
| | | @Autowired |
| | | private ConfigValue configValue; |
| | | private String primaryUrl; |
| | | private String primaryPort; |
| | | private String primaryUsername; |
| | | private String primaryPassword; |
| | | private String dbUrl; |
| | | private String dbName; |
| | | @Autowired |
| | | private DruidProperties druidProperties; |
| | | |
| | | @Bean(name = "hospDynamicDataSources") |
| | | public DataSource hospDynamicDataSources() { |
| | | HospDynamicDataSource dynamicDataSource = new HospDynamicDataSource(); |
| | | Map<String, String> configMap = configValue.getConfigMap(); |
| | | dbUrl = configMap.get("ip"); |
| | | dbName = configMap.get("name"); |
| | | primaryPassword = configMap.get("password"); |
| | | primaryPort = configMap.get("prot"); |
| | | primaryUsername = configMap.get("username"); |
| | | String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName; |
| | | dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword)); |
| | | |
| | | DataSource masterDataSource = masterDataSource(druidProperties); |
| | | dynamicDataSource.addTargetDataSource(DataSourceType.MASTER.name(),masterDataSource); |
| | | dataSourceCache.put(DataSourceType.MASTER.name(), masterDataSource); |
| | | |
| | | dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 设置默认数据源 |
| | | return dynamicDataSource; |
| | | } |
| | | |
| | | // 动态创建数据源 |
| | | private DataSource createDataSource(String url, String username, String password) { |
| | | DruidDataSource dataSource = new DruidDataSource(); |
| | | dataSource.setUrl(url); |
| | | dataSource.setUsername(username); |
| | | dataSource.setPassword(password); |
| | | return dataSource; |
| | | } |
| | | |
| | | // 根据用户 ID 动态获取数据源 |
| | | public void addDataSource(String dbName) { |
| | | Map<String, String> configMap = configValue.getConfigMap(); |
| | | dbUrl = configMap.get("ip"); |
| | | primaryPassword = configMap.get("password"); |
| | | primaryPort = configMap.get("prot"); |
| | | primaryUsername = configMap.get("username"); |
| | | // 检查缓存中是否已经存在该数据源 |
| | | if (!dataSourceCache.containsKey(dbName)) { |
| | | synchronized (this) { |
| | | String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName; |
| | | DataSource dataSource = createDataSource(url, primaryUsername, primaryPassword); |
| | | dataSourceCache.put(dbName, dataSource); |
| | | HospDynamicDataSource dynamicDataSource = (HospDynamicDataSource) hospDynamicDataSources(); |
| | | dynamicDataSource.addTargetDataSource(dbName, dataSource); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public DataSource masterDataSource(DruidProperties druidProperties){ |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | Map<String, String> configMap = configValue.getConfigMap(); |
| | | try { |
| | | // 获取属性值并赋值 |
| | | Properties properties = new Properties(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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&allowPublicKeyRetrieval=true"); |
| | | 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); |
| | | } |
| | | 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+ |
| | | ";&characterEncoding=utf8"); |
| | | 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; |
| | | } |
| | | |
| | | } |