shardingJdbc 的 actual-data-nodes节点扩展
配置类package com.shardingjdbc.shardingjdbcstu.config;import lombok.AllArgsConstructor;import org.apache.shardingsphere.core.yaml.config.sharding.YamlTableRuleConfiguration;import org.apache.shardingsphe
·
自己测试用的
配置类
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的配置来的
更多推荐
已为社区贡献2条内容
所有评论(0)