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

    你可能感兴趣的文章
    Nginx反向代理和负载均衡部署指南
    查看>>
    Nginx反向代理是什么意思?如何配置Nginx反向代理?
    查看>>
    nginx反向代理解决跨域问题,使本地调试更方便
    查看>>
    nginx反向代理转发、正则、重写、负摘均衡配置案例
    查看>>
    Nginx反向代理配置
    查看>>
    Nginx启动SSL功能,并进行功能优化,你看这个就足够了
    查看>>
    nginx启动脚本
    查看>>
    Nginx和Tomcat的区别
    查看>>
    Nginx在Windows上和Linux上(Docker启动)分别配置基本身份认证示例
    查看>>
    Nginx在Windows下载安装启动与配置前后端请求代理
    查看>>
    Nginx在开发中常用的基础命令
    查看>>
    Nginx多域名,多证书,多服务配置,实用版
    查看>>
    nginx如何实现图片防盗链
    查看>>
    Nginx学习总结(11)——提高Nginx服务器的安全性,稳定性和性能的12种技巧
    查看>>
    Nginx学习总结(12)——Nginx各项配置总结
    查看>>
    Nginx学习总结(13)——Nginx 重要知识点回顾
    查看>>
    Nginx学习总结(14)——Nginx配置参数详细说明与整理
    查看>>
    Nginx学习总结(15)—— 提升 Web 应用性能的十个步骤
    查看>>
    Nginx学习总结(8)——Nginx服务器详解
    查看>>
    nginx学习笔记002---Nginx代理配置_案例1_实现了对前端代码的方向代理_并且配置了后端api接口的访问地址
    查看>>