shardingJdbc的actual-data-nodes节点扩展方式
更新时间:2026年06月14日 10:08:58 作者:destiny@
这段描述主要讨论了使用yml配置文件进行测试时遇到的问题的解决过程,强调了配置文件优先执行的重要性,并提供了一些建用和经验分享,适合对yml和yml配置感兴趣的读者
自己测试用的
配置类
package com.shardingjdbc.shardingjdbcstu.config;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlTableRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;
/**
* @author qb
* @version 1.0
* @since 2022/3/11 16:48
*/
@AllArgsConstructor
@Component
public class TestConfig {
private final SpringBootShardingRuleConfigurationProperties shardingRuleConfigurationProperties;
@PostConstruct
public void init(){
Map<String, YamlTableRuleConfiguration> tables =
shardingRuleConfigurationProperties.getTables();
YamlTableRuleConfiguration order = tables.get("order");
String actualDataNodesTo = order.getActualDataNodes();
//TODO: 可以查询数据库,根据数据关联表
StringBuilder stringBuilder = new StringBuilder("ds0.order$->{[");
stringBuilder.append("202101,202102");
stringBuilder.append("]}");
order.setActualDataNodes(stringBuilder.toString());
String actualDataNodes = order.getActualDataNodes();
System.out.println(actualDataNodesTo);
System.out.println(actualDataNodes);
System.out.println(tables);
}
}
yml
spring:
shardingsphere:
# 参数配置,显示sql
props:
sql:
show: true
# 配置数据源
datasource:
# 给每个数据源取别名,下面的ds1,ds1任意取名字
names: ds0
# 给master-ds1每个数据源配置数据库连接信息
ds0:
#配置druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ksd_order_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
# 配置ds1-slave
# ds1:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://192.168.100.110:3306/ksd_order_db?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
# username: root
# password: 123456
# maxPoolSize: 100
# minPoolSize: 5
# 配置默认数据源ds0
sharding:
# 默认数据源,主要用于写,注意一定要配置读写分离 ,注意:如果不配置,
#那么就会把三个节点都当做从slave节点,新增,修改和删除会出错。
default-data-source-name: ds0
# 配置分表的规则
tables:
# ksd_order 逻辑表名
order:
# 数据节点:数据源$->{0..N}.逻辑表名$->{0..N}
actual-data-nodes: ds0.order$->{0..1} #只分表不分库
# 拆分库策略,也就是什么样子的数据放入放到哪个数据库中。
# database-strategy:
# standard:
# shardingColumn: tenantId # 分片字段(分片键)
# preciseAlgorithmClassName: com.shardingjdbc.shardingjdbcstu.algorithm.TenantShardingAlgorithm
# # 拆分库策略,也就是什么样子的数据放入放到哪个数据库中。
# database-strategy:
# inline: #inline 行表达时分片策略(核心,必须要掌握)
# sharding-column: age # 分片字段(分片键)
# algorithm-expression: ds$->{age % 2} # 分片算法表达式
# 拆分表策略,也就是什么样子的数据放入放到哪个数据表中。
table-strategy:
standard:
shardingColumn: userid #tenantId # 分片字段(分片键)
preciseAlgorithmClassName: com.shardingjdbc.shardingjdbcstu.algorithm.TenantShardingAlgorithm
#需求:用户变1000W的数据,对用户的数据进行分表和分库的操作,根据年龄单数储存在user1 偶数储存在user0
#同时age单数分表节点配置的事0-1

test
@Select("select * from order where userid = #{userid} limit #{pageNo},#{pageSize}")
List<Order> findOrders(
@Param("userid") Long userid,
@Param("pageNo") Integer pageNo,
@Param("pageSize") Integer pageSize
);
@Select("select * from order limit #{pageNo},#{pageSize}")
List<Order> findOrdersTo(
@Param("pageNo") Integer pageNo,
@Param("pageSize") Integer pageSize
);

执行结果

最后一次查询应该是order0与order1,但是并没有这样执行,而是根据自己config的配置来的
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot项目整合达梦数据库详解(MYSQL转换达梦数据库)
这篇文章主要为大家详细介绍了MYSQL转换达梦数据库以及SpringBoot项目整合达梦数据库的相关教程,文中的示例代码讲解详细,需要的可以参考下2025-03-03
SpringSecurity实现前后端分离登录token认证详解
目前市面上比较流行的权限框架主要实Shiro和Spring Security,这两个框架各自侧重点不同,各有各的优劣,本文将给大家详细介绍SpringSecurity如何实现前后端分离登录token认证2023-06-06
SpringBoot如何读取xml配置bean(@ImportResource)
这篇文章主要介绍了SpringBoot如何读取xml配置bean(@ImportResource),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01


最新评论