博客
关于我
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工作笔记005---nginx配置负载均衡_在微服务中实现网关集群_实现TCP传输层协议__http协议的负载均衡
    查看>>
    nginx常用命令及简单配置
    查看>>
    Nginx常用屏蔽规则,让网站更安全
    查看>>
    nginx开机启动脚本
    查看>>
    nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    查看>>
    nginx总结及使用Docker创建nginx教程
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
    查看>>
    nginx日志分割并定期删除
    查看>>
    Nginx日志分析系统---ElasticStack(ELK)工作笔记001
    查看>>
    Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
    查看>>
    Nginx映射本地静态资源时,浏览器提示跨域问题解决
    查看>>
    nginx最最最详细教程来了
    查看>>
    Nginx服务器---正向代理
    查看>>
    Nginx服务器上安装SSL证书
    查看>>
    Nginx服务器基本配置
    查看>>
    Nginx服务器的安装
    查看>>
    Nginx标准配置文件(包括反向代理、大文件上传、Https证书配置、文件预览等)
    查看>>
    Nginx模块 ngx_http_limit_conn_module 限制连接数
    查看>>
    Nginx模块 ngx_http_limit_req_module 限制请求速率
    查看>>