大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍“Sharding JDBC分库分表怎么配置”,在日常操作中,相信很多人在Sharding JDBC分库分表怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Sharding JDBC分库分表怎么配置”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
10年积累的成都做网站、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有石鼓免费网站建设让你可以放心的选择与我们合作。
分库需要两个以上数据源,这里配置test0,test1两个数据库
spring.shardingsphere.datasource.names=test0,test1 #test0 spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.test0.driver-class-name=com.MySQL.cj.jdbc.Driver spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0 spring.shardingsphere.datasource.test0.username= spring.shardingsphere.datasource.test0.password= #test1 spring.shardingsphere.datasource.test1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.test1.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test1 spring.shardingsphere.datasource.test1.username= spring.shardingsphere.datasource.test1.password=
配置分库策略 按照user_id % 2 进行分库
# 指定分片列名称的 shardingColumn spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id # 指定分片算法行表达式的 algorithmExpression spring.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_record spring.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_id spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE # health_task spring.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_id spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE
完整配置
server.port=8080 #打印sql spring.shardingsphere.props.sql.show=true #配置数据源 spring.shardingsphere.datasource.names=test0,test1 #test0 spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0 spring.shardingsphere.datasource.test0.username=devadmin spring.shardingsphere.datasource.test0.password= #test1 spring.shardingsphere.datasource.test1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.test1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.test1.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test1 spring.shardingsphere.datasource.test1.username=devadmin spring.shardingsphere.datasource.test1.password= # 指定分片列名称的 shardingColumn spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id # 指定分片算法行表达式的 algorithmExpression spring.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_record spring.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_id spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE # health_task spring.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_id spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id spring.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分库分表怎么配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!