因snakeyaml原因,暂时放弃对分表分库的支持
This commit is contained in:
parent
3fe91e7155
commit
11371dcf43
@ -65,6 +65,7 @@ public class SysLoginController
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "获取用户信息")
|
@Operation(summary = "获取用户信息")
|
||||||
@GetMapping("getInfo")
|
@GetMapping("getInfo")
|
||||||
|
// @DataSource(DataSourceType.SLAVE)
|
||||||
public AjaxResult getInfo()
|
public AjaxResult getInfo()
|
||||||
{
|
{
|
||||||
SysUser user = SecurityUtils.getLoginUser().getUser();
|
SysUser user = SecurityUtils.getLoginUser().getUser();
|
||||||
|
@ -12,23 +12,23 @@ spring:
|
|||||||
# 从库数据源
|
# 从库数据源
|
||||||
slave:
|
slave:
|
||||||
# 从数据源开关/默认关闭
|
# 从数据源开关/默认关闭
|
||||||
enabled: false
|
enabled: true
|
||||||
url:
|
url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
username:
|
username: root
|
||||||
password:
|
password: 123456
|
||||||
sharding:
|
sharding:
|
||||||
# 分库分表开关/默认关闭
|
# 分库分表开关/默认关闭
|
||||||
enabled: false
|
enabled: true
|
||||||
order1:
|
ruoyi1:
|
||||||
enabled: false
|
enabled: true
|
||||||
url:
|
url: jdbc:mysql://127.0.0.1/ruoyi1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
username:
|
username: root
|
||||||
password:
|
password: 123456
|
||||||
order2:
|
ruoyi2:
|
||||||
enabled: false
|
enabled: true
|
||||||
url:
|
url: jdbc:mysql://127.0.0.1/ruoyi2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
username:
|
username: root
|
||||||
password:
|
password: 123456
|
||||||
|
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initialSize: 5
|
initialSize: 5
|
||||||
|
@ -49,7 +49,7 @@ user:
|
|||||||
spring:
|
spring:
|
||||||
cache:
|
cache:
|
||||||
# 指定缓存类型 jcache 本地缓存 redis 缓存
|
# 指定缓存类型 jcache 本地缓存 redis 缓存
|
||||||
type: jcache
|
type: redis
|
||||||
redis:
|
redis:
|
||||||
# 指定存活时间(ms)
|
# 指定存活时间(ms)
|
||||||
time-to-live: 86400000
|
time-to-live: 86400000
|
||||||
|
@ -36,17 +36,17 @@ public class ShardingDataSourceConfig {
|
|||||||
Logger logger = LoggerFactory.getLogger(ShardingDataSourceConfig.class);
|
Logger logger = LoggerFactory.getLogger(ShardingDataSourceConfig.class);
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties("spring.datasource.druid.sharding.order1")
|
@ConfigurationProperties("spring.datasource.druid.sharding.ruoyi1")
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.order1", name = "enabled", havingValue = "true")
|
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.ruoyi1", name = "enabled", havingValue = "true")
|
||||||
public DataSource order1DataSource(DruidProperties druidProperties) {
|
public DataSource ruoyi1DataSource(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.sharding.order2")
|
@ConfigurationProperties("spring.datasource.druid.sharding.ruoyi2")
|
||||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.order2", name = "enabled", havingValue = "true")
|
@ConditionalOnProperty(prefix = "spring.datasource.druid.sharding.ruoyi2", name = "enabled", havingValue = "true")
|
||||||
public DataSource order2DataSource(DruidProperties druidProperties) {
|
public DataSource ruoyi2DataSource(DruidProperties druidProperties) {
|
||||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||||
return druidProperties.dataSource(dataSource);
|
return druidProperties.dataSource(dataSource);
|
||||||
}
|
}
|
||||||
@ -72,25 +72,25 @@ public class ShardingDataSourceConfig {
|
|||||||
Map<String, DataSource> dataSourceMap = new HashMap<>();
|
Map<String, DataSource> dataSourceMap = new HashMap<>();
|
||||||
|
|
||||||
// 添加数据源
|
// 添加数据源
|
||||||
setDataSource(dataSourceMap, "order1", "order1DataSource");
|
setDataSource(dataSourceMap, "ruoyi1", "ruoyi1DataSource");
|
||||||
setDataSource(dataSourceMap, "order2", "order2DataSource");
|
setDataSource(dataSourceMap, "ruoyi2", "ruoyi2DataSource");
|
||||||
|
|
||||||
// 配置分片规则
|
// 配置分片规则
|
||||||
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
|
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
|
||||||
// 表规则配置 示例:
|
// 表规则配置 示例:
|
||||||
// 添加order1.sys_order_0,order2.sys_order_0,order1.sys_order_1,order2.sys_order_1
|
// 添加order1.sys_order_0,order2.sys_order_0,order1.sys_order_1,order2.sys_order_1
|
||||||
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(
|
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(
|
||||||
"sys_user", "order$->{1..2}.sys_order_$->{0..1}");
|
"sys_user", "ruoyi$->{1..2}.sys_user_$->{0..1}");
|
||||||
// 配置分库策略
|
// 配置分库策略
|
||||||
// 示例: 根据user_id分库,user_id为单数去order1库,偶数去order2库
|
// 示例: 根据user_id分库,user_id为单数去order1库,偶数去order2库
|
||||||
orderTableRuleConfig.setDatabaseShardingStrategyConfig(
|
orderTableRuleConfig.setDatabaseShardingStrategyConfig(
|
||||||
new InlineShardingStrategyConfiguration("user_id", "order$->{user_id % 2 + 1}"));
|
new InlineShardingStrategyConfiguration("user_id", "ruoyi$->{user_id % 2 + 1}"));
|
||||||
// 配置分表策略
|
// 配置分表策略
|
||||||
// 示例: 根据order_id分表,order_id为偶数分到sys_order_0表,奇数分到sys_order_1表
|
// 示例: 根据sex分表,sex为偶数分到sys_order_0表,奇数分到sys_order_1表
|
||||||
orderTableRuleConfig.setTableShardingStrategyConfig(
|
orderTableRuleConfig.setTableShardingStrategyConfig(
|
||||||
new InlineShardingStrategyConfiguration("order_id", "sys_order_$->{order_id % 2}"));
|
new InlineShardingStrategyConfiguration("sex", "sys_user_$->{sex % 2}"));
|
||||||
// 分布式主键
|
// 分布式主键
|
||||||
orderTableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id"));
|
orderTableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "user_id"));
|
||||||
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
|
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
|
||||||
|
|
||||||
// 获取数据源对象
|
// 获取数据源对象
|
||||||
|
@ -4,13 +4,16 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import com.ruoyi.common.annotation.DataSource;
|
||||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||||
|
import com.ruoyi.common.enums.DataSourceType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户表 数据层
|
* 用户表 数据层
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
|
@DataSource(DataSourceType.SHARDING)
|
||||||
public interface SysUserMapper
|
public interface SysUserMapper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user