1. 接入多源数据
This commit is contained in:
parent
cad8604cac
commit
615ecc2b5b
@ -95,6 +95,11 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot集成mybatis框架 -->
|
||||
<dependency>
|
||||
@ -164,6 +169,16 @@
|
||||
<version>${jwt.version}</version>
|
||||
</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-->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.ruoyi;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
@ -9,7 +10,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
|
||||
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class })
|
||||
public class RuoYiApplication
|
||||
{
|
||||
public static void main(String[] args)
|
||||
|
@ -32,51 +32,51 @@ import com.ruoyi.framework.datasource.DynamicDataSource;
|
||||
@Configuration
|
||||
public class DruidConfig
|
||||
{
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.master")
|
||||
public DataSource masterDataSource(DruidProperties druidProperties)
|
||||
{
|
||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
return druidProperties.dataSource(dataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.slave")
|
||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
||||
public DataSource slaveDataSource(DruidProperties druidProperties)
|
||||
{
|
||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
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.name(), "slaveDataSource");
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
// @Bean
|
||||
// @ConfigurationProperties("spring.datasource.druid.master")
|
||||
// public DataSource masterDataSource(DruidProperties druidProperties)
|
||||
// {
|
||||
// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
// return druidProperties.dataSource(dataSource);
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @ConfigurationProperties("spring.datasource.druid.slave")
|
||||
// @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
||||
// public DataSource slaveDataSource(DruidProperties druidProperties)
|
||||
// {
|
||||
// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
// 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.name(), "slaveDataSource");
|
||||
// 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)
|
||||
// {
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 去除监控页面底部的广告
|
||||
|
@ -1,21 +1,17 @@
|
||||
# 数据源配置
|
||||
spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driverClassName: com.mysql.cj.jdbc.Driver
|
||||
druid:
|
||||
# 主库数据源
|
||||
master:
|
||||
url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||
username: root
|
||||
password: password
|
||||
# 从库数据源
|
||||
slave:
|
||||
# 从数据源开关/默认关闭
|
||||
enabled: false
|
||||
url:
|
||||
username:
|
||||
password:
|
||||
statViewServlet:
|
||||
enabled: true
|
||||
# 设置白名单,不填则允许所有访问
|
||||
allow:
|
||||
url-pattern: /druid/*
|
||||
# 控制台管理用户名和密码
|
||||
login-username:
|
||||
login-password:
|
||||
dynamic:
|
||||
druid:
|
||||
# 初始连接数
|
||||
initialSize: 5
|
||||
# 最小连接池数量
|
||||
@ -37,14 +33,6 @@ spring:
|
||||
testOnReturn: false
|
||||
webStatFilter:
|
||||
enabled: true
|
||||
statViewServlet:
|
||||
enabled: true
|
||||
# 设置白名单,不填则允许所有访问
|
||||
allow:
|
||||
url-pattern: /druid/*
|
||||
# 控制台管理用户名和密码
|
||||
login-username:
|
||||
login-password:
|
||||
filter:
|
||||
stat:
|
||||
enabled: true
|
||||
@ -55,3 +43,23 @@ spring:
|
||||
wall:
|
||||
config:
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user