1 基本概念

1.1 ShardingSphere概述

官网:https://shardingsphere.apache.org/index_zh.html

1.2 分库分表概述

分库分表是为了解决由于数据量过大而导致数据库性能降低的问题,将原来独立的数据库拆分成若干数据库组成 ,将数据大表拆分成若干数据表组成,使得单一数据库、单一数据表的数据量变小,从而达到提升数据库性能的目的。

● 水平分表

将一个表的数据按一定规则拆分到多个表结构相同的表中。

● 垂直分表

将一个表按照字段拆分成多个表,每个表存储其中一部分字段。

● 水平分库

同一个表的数据按一定规则拆到不同的数据库中,每个库可以放在不同的服务器上。

● 垂直分库

按照业务将表分布到不同的数据库上,达到专库专用。

2 简单使用

第一步:创建数据库,执行脚本;

第二步:创建springboot工程,引入依赖;

    <!-- sharding-jdbc依赖 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>

第三步:编写测试相关代码;

第四步:编写配置文件,测试。

2.1 水平分表

策略:cid为偶数数据添加到course_db的course_0表,cid为奇数数据添加到course_db的course_1表。

配置:

# sharding-jdbc 水平分表

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true # 配置数据源,给数据源起名
spring.shardingsphere.datasource.names=ds # 配置数据
spring.shardingsphere.datasource.ds.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=1234 # 指定course表分布情况,配置表在哪个数据库里面,表名称都是什么 ds.course_0 ds.course_1
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds.course_$->{0..1} # 指定表分片策略
# cid为偶数数据添加到ds(course_db)的course_0表,cid为奇数数据添加到ds(course_db)的course_1表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2} # 指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.2 垂直分表

可直接手动拆分表实现。

2.3 水平分库

策略:sid是偶数数据添加到course_db_0的course表,sid是奇数数据添加到course_db_1的course表。

配置:

# sharding-jdbc 水平分库

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true # 配置数据源,给数据源起名
spring.shardingsphere.datasource.names=ds0,ds1 # 配置第一个数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/course_db_0?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=1234 # 配置第二个数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/course_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=1234 # 指定course表分布情况,配置表在哪个数据库里面,表名称都是什么 ds0.course d1.course
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds$->{0..1}.course # 指定数据库分片策略
# sid是偶数数据添加到ds0(course_db_0)的course表,sid是奇数数据添加到ds1(course_db_1)的course表
spring.shardingsphere.sharding.tables.course.database-strategy.inline..sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=ds$->{cid % 2} #spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=cid
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{cid % 2} # 指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.4 垂直分库

策略:操作course表调用course_db,操作student表调用student_db。

配置:

# sharding-jdbc 垂直分库

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true # 配置数据源,给数据源起名
spring.shardingsphere.datasource.names=m0,m1 # 配置第一个数据源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=1234 # 配置第二个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/student_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=1234 # 操作course表调用m0(course_db),操作student表调用m1(student_db)
# 配置student表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m0.course # 指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 配置student表分布情况
spring.shardingsphere.sharding.tables.student.actual-data-nodes=m1.student # 指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.student.key-generator.column=sid
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE # 打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.5 广播表

策略:添加、删除dictionary表数据时,同时操作course_db中dictionary表与student_db中dictionary表。

配置:

# sharding-jdbc 操作广播表

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true # 配置数据源,给数据源起名
spring.shardingsphere.datasource.names=m0,m1 # 配置第一个数据源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=1234 # 配置第二个数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/student_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=1234 # 配置course表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m0.course # 指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 配置student表分布情况
spring.shardingsphere.sharding.tables.student.actual-data-nodes=m1.student # 指定student表里面主键sid生成策略
spring.shardingsphere.sharding.tables.student.key-generator.column=sid
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE # 配置广播表
# 添加、删除dictionary表数据时,同时操作m0(course_db)中dictionary表与m1(student_db)中dictionary表
spring.shardingsphere.sharding.broadcast-tables=dictionary
spring.shardingsphere.sharding.tables.dictionary.key-generator.column=id
spring.shardingsphere.sharding.tables.dictionary.key-generator.type=SNOWFLAKE # 打开sql输出日志
spring.shardingsphere.props.sql.show=true

2.6 读写分离

主从复制通过MySQL自身配置实现,Sharding-JDBC通过对SQL语义的分析,将写操作与读操作分别路由至主库与从库实现读写分离。

策略:写操作course_db(127.0.0.1)从库中course表,读操作course_db(192.168.1.107)主库中course表。

配置:

# sharding-jdbc 读写分离

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true # 配置数据源,给数据源起名
spring.shardingsphere.datasource.names=s0,s1 # 配置主库数据源
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=1234 # 配置从库数据源
spring.shardingsphere.datasource.s1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s1.url=jdbc:mysql://192.168.1.107:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s1.username=root
spring.shardingsphere.datasource.s1.password=root # 主库从库逻辑数据源定义
# 写操作s0(course_db)主库中course表,读操作s1(course_db)从库中course表
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=s0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s1 # 配置course表分布情况
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds0.course # 指定course表里面主键cid生成策略
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 打开sql输出日志
spring.shardingsphere.props.sql.show=true

ShardingSphere学习的更多相关文章

  1. 【ShardingSphere】ShardingSphere学习(三)-数据分片-分片

    分片键 分片算法 分片策略 SQL Hint 分片键 用于分片的数据库字段,是将数据库(表)水平拆分的关键字段.例:将订单表中的订单主键的尾数取模分片,则订单主键为分片字段. SQL中如果无分片字段, ...

  2. 【ShardingSphere】ShardingSphere学习(二)-核心概念-SQL

    逻辑表 水平拆分的数据库(表)的相同逻辑和数据结构表的总称. 例:订单数据根据主键尾数拆分为10张表,分别是t_order_0到t_order_9,他们的逻辑表名为t_order. 真实表 在分片的数 ...

  3. 【ShardingSphere】ShardingSphere学习(一)

    参考官方文档:http://shardingsphere.apache.org/ ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC.Sha ...

  4. 分库分表框架ShardingSphere入门学习1

    背景 传统的将数据集中存储至单一数据节点的解决方案,在性能.可用性和运维成本这三方面已经难于满足互联网的海量数据场景. 从性能方面来说,由于关系型数据库大多采用 B+ 树类型的索引,在数据量超过阈值的 ...

  5. 分库分表利器——sharding-sphere

    背景 得不到的东西让你彻夜难眠,没有尝试过的技术让我跃跃欲试. 本着杀鸡焉用牛刀的准则,我们倡导够用就行,不跟风,不盲从. 所以,结果就是我们一直没有真正使用分库分表.曾经好几次,感觉没有分库分表(起 ...

  6. springboot + sharding-jdbc 学习

    官网地址:http://shardingsphere.io/document/current/cn/overview/ sharding-jdbc事务:https://blog.csdn.net/ya ...

  7. Sharding-JDBC 学习资料

    学习资料 网站 官网 https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/ 基于 Docker 的 M ...

  8. sharding-JDBC学习笔记

    sharding-JDBC学习笔记 ShardingSphere ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC.Sharding-Pr ...

  9. JAVA全栈工程师学习线路(建议收藏)

    互联网技术,更新迭代迅速,用日新月异来说也不为过,所以,面对这这种大环境,对于码农尤其是那些对于初入职场的新手来说,该如何自我学习升级,往方向发展,这一点是大家都经常困惑的. 大部分人,刚开始学习的J ...

随机推荐

  1. ArcEngine+C# 森林资源仿真系统 核心代码

    目录 第一章 基础功能的核心代码 实现滚轮缩放事件 创建或获取地理数据(导入前询问用户是否覆盖) 创建要素类(Shape) 点列数据创建要素类 Shape文件创建要素类 GDB中取出要素类 创建栅格数 ...

  2. centos7安装privoxy

    本文分为三部分,第一部分是在阿里云的ECS上安装Privoxy,第二部分是在AWS的EC2上安装Privoxy,第三部分是Privoxy的配置. 第一部分:阿里云ECS安装Privoxy 配置yum源 ...

  3. Qt 程序发布以及打包成exe安装包

    一.简述 Qt 项目开发完成之后,需要打包发布程序,而因为用户电脑上没有 Qt 配置环境,所以需要将 release 生成的 exe 文件和所依赖的 dll 文件复制到一个文件夹中,然后再用 Inno ...

  4. Flask - 解决 app.run() 添加 host、port、debug 参数后运行不生效的问题

    问题背景 app.run() 添加了 host.port.debug 参数,运行后发现没有生效,咋肥事! 解决方案 要打开 debug 模式的话,勾选 FLASK_DEBUG 就好啦 再次运行,发现已 ...

  5. GoLang设计模式05 - 原型模式

    原型模式也是一种创建型模式,它可以帮助我们优雅地创建对象的拷贝.在这种设计模式里面,将克隆某个对象的职责交给了要被克隆的这个对象.被克隆的对象需要提供一个clone()方法.通过这个方法可以返回该对象 ...

  6. Stream流思想和常用方法

    一.IO流用于读写:Stream流用于处理数组和集合数据: 1.传统集合遍历: 2.使用Stream流的方式过滤: 其中,链式编程(返回值就是对象自己)中,filter使用的是Predicate函数式 ...

  7. 将rgb表示方式转换为hex表示方式-------------将hex表示方式转换为rgb表示方式(这里返回rgb数组组合)

      /**  * kevin 2021.1.4  * 将rgb表示方式转换为hex表示方式  * @param {string} rgbColor 传过来的hex格式的颜色  * @returns { ...

  8. liquibase新增字段注释导致表格注释同时变更bug记录

    liquibase是一个用于数据库变更跟踪.版本管理和自动部署的开源工具.它的使用方式方法可以参考官方文档或者其他人的博客,这里不做过多介绍. 1. 问题复现 在使用过程中发现了一个版本bug.这个b ...

  9. docker学习笔记(二)--配置镜像加速器

    前提:docker已经安装好 配置过程 进入至阿里云开发中心,https://dev.aliyun.com/,点击管理中心 管理中心中,点击左侧镜像加速器. 修改配置文件,使用加速器,根据我们目前Do ...

  10. Nginx总结(九)Nginx服务器高性能优化的配置--轻松实现10万并发访问量

    前面讲了如何配置Nginx虚拟主机,如何配置服务日志等很多基础的内容,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category ...