Sharding JDBC分库分表怎么配置
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章主要介绍"Sharding JDBC分库分表怎么配置",在日常操作中,相信很多人在Sharding JDBC分库分表怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对
千家信息网最后更新 2025年02月02日Sharding JDBC分库分表怎么配置
这篇文章主要介绍"Sharding JDBC分库分表怎么配置",在日常操作中,相信很多人在Sharding JDBC分库分表怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Sharding JDBC分库分表怎么配置"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
分库分表配置
分库需要两个以上数据源,这里配置test0,test1两个数据库
spring.shardingsphere.datasource.names=test0,test1#test0spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0spring.shardingsphere.datasource.test0.username=spring.shardingsphere.datasource.test0.password=#test1spring.shardingsphere.datasource.test1.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.test1.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test1spring.shardingsphere.datasource.test1.username=spring.shardingsphere.datasource.test1.password=
配置分库策略 按照user_id % 2 进行分库
# 指定分片列名称的 shardingColumnspring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id# 指定分片算法行表达式的 algorithmExpressionspring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=test$->{user_id % 2}
配置绑定表和广播表
# 设置绑定表spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task# 设置广播表spring.shardingsphere.sharding.broadcast-tables[0]=health_level
设置分表策略,按照 record_id % 2 进行分表
# user 如果不加这个,数据会随机插入数据库中spring.shardingsphere.sharding.tables.user.actual-data-nodes=test$->{[0,1]}.user#路由到 test0 否则会随意添加到两个数据库中spring.shardingsphere.sharding.tables.other_table.actual-data-nodes=test$->{0}.other_table# health_recordspring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test$->{0..1}.health_record$->{0..1}spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_idspring.shardingsphere.sharding.tables.health_record.key-generator.column=record_idspring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE# health_taskspring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test$->{0..1}.health_task$->{0..1}spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id % 2}spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_idspring.shardingsphere.sharding.tables.health_task.key-generator.column=task_idspring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE
完整配置
server.port=8080#打印sqlspring.shardingsphere.props.sql.show=true#配置数据源spring.shardingsphere.datasource.names=test0,test1#test0spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0spring.shardingsphere.datasource.test0.username=devadminspring.shardingsphere.datasource.test0.password=#test1spring.shardingsphere.datasource.test1.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.test1.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test1spring.shardingsphere.datasource.test1.username=devadminspring.shardingsphere.datasource.test1.password=# 指定分片列名称的 shardingColumnspring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id# 指定分片算法行表达式的 algorithmExpressionspring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=test$->{user_id % 2}# 设置绑定表spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task# 设置广播表spring.shardingsphere.sharding.broadcast-tables[0]=health_level# user 如果不加这个,数据会随机插入数据库中spring.shardingsphere.sharding.tables.user.actual-data-nodes=test$->{[0,1]}.user#路由到 test0 否则会随意添加到两个数据库中spring.shardingsphere.sharding.tables.other_table.actual-data-nodes=test$->{0}.other_table# health_recordspring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test$->{0..1}.health_record$->{0..1}spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_idspring.shardingsphere.sharding.tables.health_record.key-generator.column=record_idspring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE# health_taskspring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test$->{0..1}.health_task$->{0..1}spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id % 2}spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_idspring.shardingsphere.sharding.tables.health_task.key-generator.column=task_idspring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE
数据库
test0 test1 两个数据库的结构如下:
执行测试方法之后,数据库的数据如下:
health_level 是广播表,所以test0、test1中的数据是一样的
user 表中的数据分布。test0中user_id 都为偶数,test1中user_id都为奇数。
testx_health_record0 、testx_health_task0 中 record_id 都为偶数,testx_health_record1、testx_health_task11中record_ir都为奇数。(我们只截取healt_record表,health_task表的数据是一样的)
test0 中 health_record0 和 health_record1
test1 中 health_record0 和 health_record1
到此,关于"Sharding JDBC分库分表怎么配置"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
数据
配置
分库
数据库
分表
两个
学习
广播
偶数
名称
奇数
数据源
方法
更多
策略
表达式
路由
法行
帮助
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
九天封神服务器
数据库中的行和列被称为
布马网络安全吗
服务器如何保证网络安全
数据库原理与应用课程论文
学校服务器上有独立显卡吗
沈阳服务器运维外包
vr虚拟软件开发核心问题
新宜农网络技术有限公司
炉石传说服务器故障2021
虚拟机安装网络安全基础
建立服务器套接字可能产生的异常
沧州昊宇网络技术合法吗
群力软件开发地址
学校网络安全保障工作排查
网络安全的界定
创造与魔法换服务器装备会没吗
手机版精灵宝可梦服务器下载安装
防火墙网络安全解决方案
四川轻化工数据库考试试卷
数据库系统软考报名
ff14服务器失去连接
城阳区app软件开发公司
网络安全法发展历程是否顺利
深圳网络安全评估厂子
协智网络技术有限公司
添加数据库的最快方法
亚洲网络安全大会参与国家
网易明日之后新手怎么做服务器
大亚湾嘉瑞科技软件开发