| | |
| | | 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; |
| | |
| | | @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("数据库连接文件找不到 系统正在创建!"); |
| | | 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 数据库连接ip地址\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 = 是否开启pacs从库数据库连接 (从数据源开关/默认关闭 fales)\n"); |
| | | fileWriter.write("pacsip = 你的pacs数据库连接ip地址\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 数据库连接ip地址\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(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | 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) { |
| | |
| | | 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) { |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 去除监控页面底部的广告 |
| | | */ |