博客
关于我
shardingsphere实现分库
阅读量:328 次
发布时间:2019-03-04

本文共 2186 字,大约阅读时间需要 7 分钟。

实验环境

  • SpringBoot 版本:2.2.2.RELEASE
  • Shardingsphere 版本:4.0.0-RC3

实验说明

  • 数据库:demo_ds_0,demo_ds_1
  • 表结构:在 demo_ds_0 和 demo_ds_1 中分别创建 user 表,字段包括 id(主键),nameuidschoolage
CREATE TABLE `user` (
`id` bigint(255) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`uid` varchar(255) DEFAULT NULL,
`school` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)

实验步骤

第一步:引入 Shardingsphere 依赖

在项目依赖中添加以下坐标:

org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.0.0-RC3
org.apache.shardingsphere
sharding-jdbc-spring-namespace
4.0.0-RC3

第二步:配置 application.properties 文件

# 其他配置(如服务器绑定、上下文路径等)...
# 数据源配置
spring.shardingsphere.datasource.names=ds-0,ds-1
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_0
spring.shardingsphere.datasource.ds-0.username=spring
spring.shardingsphere.datasource.ds-0.password=spring
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_1
spring.shardingsphere.datasource.ds-1.username=spring
spring.shardingsphere.datasource.ds-1.password=spring
# 分片配置
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds-$-&{age % 2}
# 表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-$-{0..1}.users
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.user.key-generator.props.worker.id=123

第三步:验证

  • 插入数据:age=1,数据将保存在 demo_ds_1
  • 插入数据:age=2,数据将保存在 demo_ds_0
  • 插入数据:age=3,数据将保存在 demo_ds_0
  • 查询数据:可查询到三条记录,验证完毕
  • 注意事项

    • 确保数据库地址和账号信息配置正确
    • 部署前需检查所有配置参数的有效性
    • 验证过程中可根据实际需求调整分片策略

    转载地址:http://tcuh.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现图片erosion operation侵蚀操作算法(附完整源码)
    查看>>
    Objective-C实现图片的放大缩小(附完整源码)
    查看>>
    Objective-C实现图片腐蚀(附完整源码)
    查看>>
    Objective-C实现图片膨胀(附完整源码)
    查看>>
    Objective-C实现图的邻接矩阵(附完整源码)
    查看>>
    Objective-C实现圆球的表面积和体积(附完整源码)
    查看>>
    Objective-C实现在Regex的帮助下检查字谜算法(附完整源码)
    查看>>
    Objective-C实现在指定区间 [a, b] 中找到函数的实根,其中 f(a)*f(b) < 0算法(附完整源码)
    查看>>
    Objective-C实现均值滤波(附完整源码)
    查看>>
    Objective-C实现埃拉托斯特尼筛法算法(附完整源码)
    查看>>
    Objective-C实现域名解析(附完整源码)
    查看>>
    Objective-C实现域名转IP(附完整源码)
    查看>>
    Objective-C实现培根密码算法(附完整源码)
    查看>>
    Objective-C实现基于 LIFO的堆栈算法(附完整源码)
    查看>>
    Objective-C实现基于 LinkedList 的添加两个数字的解决方案算法(附完整源码)
    查看>>
    Objective-C实现基于opencv的抖动算法(附完整源码)
    查看>>
    Objective-C实现基于事件对象实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于信号实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于文件流拷贝文件(附完整源码)
    查看>>
    Objective-C实现基于模板的双向链表(附完整源码)
    查看>>