1. 处理多个环境的配置

This commit is contained in:
YunaiV
2021-04-18 21:44:34 +08:00
parent ef7089c1b9
commit 4f70810c35
13 changed files with 104 additions and 263 deletions

View File

@ -0,0 +1,22 @@
package cn.iocoder.dashboard.framework.datasource.core.enums;
/**
* 对应于多数据源中不同数据源配置
*
* 通过在方法上,使用 {@link com.baomidou.dynamic.datasource.annotation.DS} 注解,设置使用的数据源。
* 注意,默认是 {@link #MASTER} 数据源
*
* 对应官方文档为 http://dynamic-datasource.com/guide/customize/Annotation.html
*/
public interface DataSourceEnum {
/**
* 主库,推荐使用 {@link com.baomidou.dynamic.datasource.annotation.Master} 注解
*/
String MASTER = "master";
/**
* 从库,推荐使用 {@link com.baomidou.dynamic.datasource.annotation.Slave} 注解
*/
String SLAVE = "slave";
}

View File

@ -1,14 +0,0 @@
package cn.iocoder.dashboard.framework.datasource.core.enums;
/**
* 对应于多数据源中不同数据源配置
*
* 在方法上使用注解{@code @DS(DatasourceMark.slave)}可以指定slave数据源默认是master数据源
*/
public interface DatasourceMark {
String MASTER = "master";
String SLAVE = "slave";
}

View File

@ -18,11 +18,7 @@ public interface ToolSchemaTableMapper extends BaseMapperX<ToolSchemaTableDO> {
.likeIfPresent("table_comment", tableComment));
}
default List<ToolSchemaTableDO> selectListByTableSchema(String tableSchema) {
return selectList(new QueryWrapper<ToolSchemaTableDO>().eq("table_schema", tableSchema));
}
default ToolSchemaTableDO selectByTableName1(String tableSchema, String tableName) {
default ToolSchemaTableDO selectByTableSchemaAndTableName(String tableSchema, String tableName) {
return selectOne(new QueryWrapper<ToolSchemaTableDO>().eq("table_schema",tableSchema)
.eq("table_name", tableName));
}

View File

@ -101,10 +101,10 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
@Override
public Long createCodegen(String tableName) {
//获取当前schema
// 获取当前schema
String tableSchema = codegenProperties.getDbSchemas().iterator().next();
// 从数据库中,获得数据库表结构
ToolSchemaTableDO schemaTable = schemaTableMapper.selectByTableName1(tableSchema, tableName);
ToolSchemaTableDO schemaTable = schemaTableMapper.selectByTableSchemaAndTableName(tableSchema, tableName);
List<ToolSchemaColumnDO> schemaColumns = schemaColumnMapper.selectListByTableName(tableSchema, tableName);
// 导入
return this.createCodegen0(ToolCodegenImportTypeEnum.DB, schemaTable, schemaColumns);

View File

@ -4,39 +4,42 @@ server:
--- #################### 数据库相关配置 ####################
spring:
# 数据源配置项 TODO 监控配置
# 排除默认的自动配置使用dynamic-datasource-spring-boot-starter配置多数据源整合druid
# 数据源配置项
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
exclude:
- com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源
datasource:
druid:
druid: # Druid 【监控】相关的全局配置
web-stat-filter:
enabled: true
stat-view-servlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
allow: # 设置白名单,不填则允许所有访问
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-username: # 控制台管理用户名和密码
login-password:
filter:
stat:
enabled: true
# 慢 SQL 记录
log-slow-sql: true
log-slow-sql: true # 慢 SQL 记录
slow-sql-millis: 100
merge-sql: true
wall:
config:
multi-statement-allow: true
dynamic:
# druid全局配置 https://dynamic-datasource.com/en/guide/integration/Druid.html#configurate-parameters
# druid: #The following are the supported global parameters
# initial-size:
# max-active:
# min-idle:
# max-wait:
dynamic: # 多数据源配置
druid: # Druid 【连接池】相关的全局配置
initial-size: 5 # 初始连接数
min-idle: 10 # 最小连接池数量
max-active: 20 # 最大连接池数量
max-wait: 600000 # 配置获取连接等待超时的时间,单位:毫秒
time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位:毫秒
min-evictable-idle-time-millis: 300000 # 配置一个连接在池中最小生存的时间,单位:毫秒
max-evictable-idle-time-millis: 900000 # 配置一个连接在池中最大生存的时间,单位:毫秒
validation-query: SELECT 1 FROM DUAL # 配置检测连接是否有效
test-while-idle: true
test-on-borrow: false
test-on-return: false
primary: master
datasource:
master:
@ -45,19 +48,12 @@ spring:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 3WLiVUBEwTbvAfsh
# druid 局部配置
# druid: # The following are independent parameters that can be reset for each db
# validation-query: select 1 FROM DUAL #such as oracle need this
# slave:
# name: ruoyi-vue-pro-slave
# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
# driver-class-name: com.mysql.jdbc.Driver
# username: root
# password: 123456
slave: # 模拟从库,可根据自己需要修改 # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro
url: jdbc:mysql://400-infra.server.iocoder.cn:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 3WLiVUBEwTbvAfsh
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
redis:
@ -103,9 +99,9 @@ apollo:
eagerLoad:
enabled: true # 设置 Apollo 在日志初始化前生效,可以实现日志的动态级别配置
jdbc: # 自定义的 JDBC 配置项,用于数据库的地址
url: ${spring.datasource.url}
username: ${spring.datasource.username}
password: ${spring.datasource.password}
url: ${spring.datasource.dynamic.datasource.master.url}
username: ${spring.datasource.dynamic.datasource.master.username}
password: ${spring.datasource.dynamic.datasource.master.password}
--- #################### 服务保障相关配置 ####################
@ -181,7 +177,7 @@ yudao:
base-path: http://127.0.0.1:${server.port}${yudao.web.api-prefix}/system/file/get/
codegen:
base-package: ${yudao.info.base-package}
db-schemas: ${spring.datasource.name}
db-schemas: ${spring.datasource.dynamic.datasource.master.name}
xss:
enable: false
exclude-urls: # 如下两个 url仅仅是为了演示去掉配置也没关系

View File

@ -4,7 +4,7 @@ server:
--- #################### 数据库相关配置 ####################
spring:
# 数据源配置项 TODO 监控配置
# 数据源配置项
autoconfigure:
exclude:
- com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源
@ -48,8 +48,8 @@ spring:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
slave:
name: ruoyi-vue-slave
slave: # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
@ -177,18 +177,10 @@ yudao:
base-path: http://127.0.0.1:${server.port}${yudao.web.api-prefix}/system/file/get/
codegen:
base-package: ${yudao.info.base-package}
db-schemas: ${spring.datasource.name}
db-schemas: ${spring.datasource.dynamic.datasource.master.name}
xss:
enable: false
exclude-urls: # 如下两个 url仅仅是为了演示去掉配置也没关系
- ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求
- ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求
demo: false # 关闭演示模式

View File

@ -12,7 +12,6 @@ spring:
max-file-size: 16MB # 单个文件大小
max-request-size: 32MB # 设置总上传的文件大小
# Jackson 配置项
jackson:
serialization:
write-dates-as-timestamps: true # 设置 Date 的格式,使用时间戳
@ -32,3 +31,6 @@ mybatis-plus:
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject
--- #################### 芋道相关配置 ####################

View File

@ -4,6 +4,8 @@ import cn.iocoder.dashboard.framework.datasource.config.DataSourceConfiguration;
import cn.iocoder.dashboard.framework.mybatis.config.MybatisConfiguration;
import cn.iocoder.dashboard.framework.redis.config.RedisConfig;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidDynamicDataSourceConfiguration;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
@ -26,6 +28,9 @@ public class BaseDbAndRedisIntegrationTest {
// MyBatis 配置类
MybatisConfiguration.class, // 自己的 MyBatis 配置类
MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类
// Dynamic Datasource 配置后类
DynamicDataSourceAutoConfiguration.class, //
DruidDynamicDataSourceConfiguration.class, //
// Redis 配置类
RedisAutoConfiguration.class, // Spring Redis 自动配置类
RedisConfig.class, // 自己的 Redis 配置类

View File

@ -1,24 +1,12 @@
package cn.iocoder.dashboard.modules.tool.dal.mysql.codegen;
import cn.iocoder.dashboard.BaseDbUnitTest;
import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO;
import org.junit.jupiter.api.Test;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ToolInformationSchemaTableMapperTest extends BaseDbUnitTest {
@Resource
private ToolSchemaTableMapper toolInformationSchemaTableMapper;
@Test
public void tstSelectListByTableSchema() {
List<ToolSchemaTableDO> tables = toolInformationSchemaTableMapper
.selectListByTableSchema("ruoyi-vue-pro");
assertTrue(tables.size() > 0);
}
}

View File

@ -7,12 +7,37 @@ spring:
spring:
# 数据源配置项
autoconfigure:
exclude:
- com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 排除 Druid 的自动配置,使用 dynamic-datasource-spring-boot-starter 配置多数据源
datasource:
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
dynamic: # 多数据源配置
druid: # Druid 【连接池】相关的全局配置
initial-size: 5 # 初始连接数
min-idle: 10 # 最小连接池数量
max-active: 20 # 最大连接池数量
max-wait: 600000 # 配置获取连接等待超时的时间,单位:毫秒
time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位:毫秒
min-evictable-idle-time-millis: 300000 # 配置一个连接在池中最小生存的时间,单位:毫秒
max-evictable-idle-time-millis: 900000 # 配置一个连接在池中最大生存的时间,单位:毫秒
validation-query: SELECT 1 FROM DUAL # 配置检测连接是否有效
test-while-idle: true
test-on-borrow: false
test-on-return: false
primary: master
datasource:
master:
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
slave: # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
redis:
@ -70,7 +95,7 @@ yudao:
base-path: http://127.0.0.1:${server.port}/${yudao.web.api-prefix}/file/get/
codegen:
base-package: ${yudao.info.base-package}.modules
db-schemas: ${spring.datasource.name}
db-schemas: ${spring.datasource.dynamic.datasource.master.name}
xss:
enable: false
exclude-urls: # 如下两个 url仅仅是为了演示去掉配置也没关系