1. 接入多源数据

This commit is contained in:
purple
2020-05-19 19:43:21 +08:00
parent cad8604cac
commit 615ecc2b5b
4 changed files with 523 additions and 499 deletions
ruoyi

@ -95,6 +95,11 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- SpringBoot集成mybatis框架 --> <!-- SpringBoot集成mybatis框架 -->
<dependency> <dependency>
@ -164,6 +169,16 @@
<version>${jwt.version}</version> <version>${jwt.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- swagger2--> <!-- swagger2-->
<dependency> <dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>

@ -1,5 +1,6 @@
package com.ruoyi; package com.ruoyi;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -9,7 +10,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* *
* @author ruoyi * @author ruoyi
*/ */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class })
public class RuoYiApplication public class RuoYiApplication
{ {
public static void main(String[] args) public static void main(String[] args)

@ -32,51 +32,51 @@ import com.ruoyi.framework.datasource.DynamicDataSource;
@Configuration @Configuration
public class DruidConfig public class DruidConfig
{ {
@Bean // @Bean
@ConfigurationProperties("spring.datasource.druid.master") // @ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource(DruidProperties druidProperties) // public DataSource masterDataSource(DruidProperties druidProperties)
{ // {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); // DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource); // return druidProperties.dataSource(dataSource);
} // }
//
@Bean // @Bean
@ConfigurationProperties("spring.datasource.druid.slave") // @ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") // @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource(DruidProperties druidProperties) // public DataSource slaveDataSource(DruidProperties druidProperties)
{ // {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); // DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource); // return druidProperties.dataSource(dataSource);
} // }
//
@Bean(name = "dynamicDataSource") // @Bean(name = "dynamicDataSource")
@Primary // @Primary
public DynamicDataSource dataSource(DataSource masterDataSource) // public DynamicDataSource dataSource(DataSource masterDataSource)
{ // {
Map<Object, Object> targetDataSources = new HashMap<>(); // Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); // targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); // setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources); // return new DynamicDataSource(masterDataSource, targetDataSources);
} // }
//
/** // /**
* 设置数据源 // * 设置数据源
* // *
* @param targetDataSources 备选数据源集合 // * @param targetDataSources 备选数据源集合
* @param sourceName 数据源名称 // * @param sourceName 数据源名称
* @param beanName bean名称 // * @param beanName bean名称
*/ // */
public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) // public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
{ // {
try // try
{ // {
DataSource dataSource = SpringUtils.getBean(beanName); // DataSource dataSource = SpringUtils.getBean(beanName);
targetDataSources.put(sourceName, dataSource); // targetDataSources.put(sourceName, dataSource);
} // }
catch (Exception e) // catch (Exception e)
{ // {
} // }
} // }
/** /**
* 去除监控页面底部的广告 * 去除监控页面底部的广告

@ -1,21 +1,17 @@
# 数据源配置 # 数据源配置
spring: spring:
datasource: datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid: druid:
# 主库数据源 statViewServlet:
master: enabled: true
url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 # 设置白名单,不填则允许所有访问
username: root allow:
password: password url-pattern: /druid/*
# 从库数据源 # 控制台管理用户名和密码
slave: login-username:
# 从数据源开关/默认关闭 login-password:
enabled: false dynamic:
url: druid:
username:
password:
# 初始连接数 # 初始连接数
initialSize: 5 initialSize: 5
# 最小连接池数量 # 最小连接池数量
@ -37,14 +33,6 @@ spring:
testOnReturn: false testOnReturn: false
webStatFilter: webStatFilter:
enabled: true enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter: filter:
stat: stat:
enabled: true enabled: true
@ -55,3 +43,23 @@ spring:
wall: wall:
config: config:
multi-statement-allow: true multi-statement-allow: true
datasource:
# 主库数据源
master:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:6060/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: LOLm2dI2UQF#RxOf
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
teemlink:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://172.16.30.233:1433;DatabaseName=obpm_LianCheng_Data
username: sa
password: Lcdatacenter_888