博客
关于我
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/

    你可能感兴趣的文章
    Multisim中555定时器使用技巧
    查看>>
    MySQL CRUD 数据表基础操作实战
    查看>>
    multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
    查看>>
    mysql csv import meets charset
    查看>>
    multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
    查看>>
    MySQL DBA 数据库优化策略
    查看>>
    multi_index_container
    查看>>
    MySQL DBA 进阶知识详解
    查看>>
    Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
    查看>>
    Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
    查看>>
    mysql deadlock found when trying to get lock暴力解决
    查看>>
    MuseTalk如何生成高质量视频(使用技巧)
    查看>>
    mutiplemap 总结
    查看>>
    MySQL DELETE 表别名问题
    查看>>
    MySQL Error Handling in Stored Procedures---转载
    查看>>
    MVC 区域功能
    查看>>
    MySQL FEDERATED 提示
    查看>>
    mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
    查看>>
    Mysql group by
    查看>>
    MySQL I 有福啦,窗口函数大大提高了取数的效率!
    查看>>