Sharding-JDBC实现水平拆分-单库分表
2024-09-02 03:20:07
参考资料:猿天地 https://mp.weixin.qq.com/s/901rNhc4WhLCQ023zujRVQ 作者:尹吉欢
当单表的数量急剧上升,超过了1千万以上,这个时候就要对表进行水平拆分。
表的水平拆分是什么?
就是将一个表拆分成N个表,就像一块大石头,搬不动,然后切割成10块,这样就能搬的动了。原理是一样的。 除了能够分担数量的压力,同时也能分散读写请求的压力,当然这个得看你的分片算法了,合理的算法才能够让数据分配均匀并提升性能。 今天我们主要讲单库中进行表的拆分,也就是不分库,只分表。
user表由原来的一个被拆分成了4个,数据会均匀的分布在这3个表中,也就是原来的user = user0 + user1 + user2 + user3。
技术选型:SpringBoot + Sharding-JDBC + MyBatis
1. 核心Jar包
同 垂直拆分
2. yml文件配置
# 数据源名称集合,对应下面数据源配置的名称
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: db1
# 主数据源
db1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_user?characterEncoding=utf-8
username: ****
password: ****
sharding:
tables:
user:
# 分表配置
actual-data-nodes.db1: user_${..}
# inline 表达式
table-strategy.inline.sharding-column: id
table-strategy.inline.algorithm-expression: user_${id.longValue()%}
props:
# 开启SQL显示,默认false
sql:
show: true
- actual-data-nodes 配置分表信息,这边用的inline表达式,翻译过来就是db1.user0,db1.user1,db1.user2,db1.user3
- inline.sharding-column 分表的字段,这边用id分表
- inline.algorithm-expression 分表算法行表达式,需符合groovy语法,上面的配置就是用id进行取模分片
如果我们有更复杂的分片需求,可以自定义分片算法来实现:
sharding:
tables:
user:
# 分表字段
table-strategy.standard.sharding-column: id
# 自定义分表算法类
table-strategy.standard.precise-algorithm-class-name: com.*.*.MyPreciseShardingAlgorithm
算法类:
public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
for (String tableName : availableTargetNames) {
if (tableName.endsWith(shardingValue.getValue() % 4 + "")) {
return tableName;
}
}
throw new IllegalArgumentException();
} }
在doSharding方法中你可以根据参数shardingValue做一些处理,最终返回这条数据需要分片的表名称即可。
除了单列字段分片,还支持多字段分片,大家可以自己去看文档操作一下。
需要分表的进行配置,不需要分表的无需配置,数据库操作代码一行都不用改变。
如果我们要在单库分表的基础上,再做读写分离,同样很简单,只要多配置一个从数据源就可以了,配置如下:
spring.shardingsphere.datasource.names=master,slave # 主数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456 # 从数据源
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456 # 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue()%4} # 读写分离配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave
最新文章
- python线程池实现
- 谈谈.Net技术面试
- C语言字符串拷贝
- <;转>;关闭 程序崩溃时 windows 正在检查该问题的解决方案
- kakfa的常用命令总结
- CSS备忘笔记
- 查看xcode的路径
- js实现简单计算器
- LintCode-字符串查找
- Java线程中断的本质深入理解(转)
- zookeeper 应用场景概述
- 开心的金明 NOIP 2006 普及组
- Anti-pattern/反模式
- go 定时器
- [Hive_add_2] Hive 数据类型
- MonkeyRunner_真机_运行脚本(二)
- windows kibana的连接使用
- jQuery 插件运行机制和 $冲突解决
- yii2中的rules 自定义验证规则详解
- 创建完美SDK的10个技巧
热门文章
- 使用@AutoConfigureBefore、After、Order调整Spring Boot自动配置顺序
- SQL批量插入数据【万级】
- flask 源码专题(三):请求上下文和应用上下文入栈与出栈
- web 部署专题(九):Nginx 前后端分离中csrf_token 认证的实现
- Python面试【315+道题】
- 数据可视化实例(十): 相关图(matplotlib,pandas)
- Python: 如何判断远程服务器上Excel文件是否被人打开
- hihoCoder 1041 国庆出游 最详细的解题报告
- 008.Nginx静态资源
- Java常用类-Object类