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

本文共 2159 字,大约阅读时间需要 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-1spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_0spring.shardingsphere.datasource.ds-0.username=springspring.shardingsphere.datasource.ds-0.password=springspring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_1spring.shardingsphere.datasource.ds-1.username=springspring.shardingsphere.datasource.ds-1.password=spring# 分片配置spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=agespring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds-$-&{age % 2}# 表配置spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-$-{0..1}.usersspring.shardingsphere.sharding.tables.user.key-generator.column=idspring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKEspring.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/

    你可能感兴趣的文章
    Mysql 表分区
    查看>>
    mysql 表的操作
    查看>>
    mysql 视图,视图更新删除
    查看>>
    MySQL 触发器
    查看>>
    mysql 让所有IP访问数据库
    查看>>
    mysql 记录的增删改查
    查看>>
    MySQL 设置数据库的隔离级别
    查看>>
    MySQL 证明为什么用limit时,offset很大会影响性能
    查看>>
    Mysql 语句操作索引SQL语句
    查看>>
    MySQL 误操作后数据恢复(update,delete忘加where条件)
    查看>>
    MySQL 调优/优化的 101 个建议!
    查看>>
    mysql 转义字符用法_MySql 转义字符的使用说明
    查看>>
    mysql 输入密码秒退
    查看>>
    mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
    查看>>
    mysql 通过查看mysql 配置参数、状态来优化你的mysql
    查看>>
    mysql 里对root及普通用户赋权及更改密码的一些命令
    查看>>
    Mysql 重置自增列的开始序号
    查看>>
    mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
    查看>>
    MySQL 错误
    查看>>
    mysql 随机数 rand使用
    查看>>