ShardingSphere介绍
1、一套开源的分布式数据库中间件解决方案
2、有三个产品:Sharding-JDBC 和 Sharding-Proxy
3、定位为关系型数据库中间件,合理在分布式环境下使用关系型数据库操作
什么是分库分表
1、数据库数据量不可控的,随着时间和业务发展,造成表里面数据越来越多,如果再去对数据库表 curd 操作时候,造成性能问题。
2、方案 1:从硬件上
3、方案 2:分库分表
为了解决由于数据量过大而造成数据库性能降低问题。
分库分表的方式
1、分库分表有两种方式:垂直切分和水平切分
2、垂直切分:垂直分表和垂直分库
3、水平切分:水平分表和水平分库
垂直分表
(1)操作数据库中某张表,把这张表中一部分字段数据存到一张新表里面,再把这张表另一
部分字段数据存到另外一张表里面
垂直分库
(1)把单一数据库按照业务进行划分,专库专表
水平分库
水平分表
1、应用
(1)在数据库设计时候考虑垂直分库和垂直分表
(2)随着数据库数据量增加,不要马上考虑做水平切分,首先考虑缓存处理,读写分离,使用索引等等方式,如果这些方式不能根本解决问题了,再考虑做水平分库和水平分表
2、分库分表问题
(1)跨节点连接查询问题(分页、排序)
(2)多数据源管理问题
Sharding-JDBC 简介
1、是轻量级的 java 框架,是增强版的 JDBC 驱动
2、Sharding-JDBC
(1)主要目的是:简化对分库分表之后数据相关操作
Sharding-JDBC 实现分库分表
架构
实现
pom.xml
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>5.0.0-alpha</version>
</dependency>
配置
# shardingjdbc 分片策略
# 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names= m1
# 一个实体类对应两张表,覆盖
spring.main.allow- - bean- - definition- - overriding = true
#配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
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/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.u sername= root
spring.shardingsphere.datasource.m1.password= root
#指定 course 表分布情况,配置表在哪个数据库里面,表名称都是什么 m1.course_1 ,m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes= m1.course_$->{1..2}
# 指定 course 表里面主键 cid 生成策略 SNOWFLAKE
spring.shar dingsphere.sharding.tables.course.key-generator.column= cid
spring.shardingsphere.sharding.tables.course.key-generator.type= SNOWFLAKE
# 指定分片策略 约定 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到 course_2表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sha rding-column= cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression= course_$->{cid % 2 + 1}
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
spring.main.allow- - bean- - definition-overriding= true
配置垂直分库策略
# 配置数据源,给数据源起名称,
# 水平分库,配置两个数据源
spring.shardingsphere.datasource.names= m1,m2
# 一个实体类对应两张表,覆盖
spring.main.allow- - bean- - definition- - overriding= true
#配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
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/edu_db_1?s
erverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username= root
spring.shardingsphere.datas ource.m1.password= root
#配置第二个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m2.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name= com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url= jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username= root
spring.shardingsphere.datasource.m2.password= root
#指定数据库分布情况,数据库里面表分布情况 m1 m2 course_1 course_2
spring.shardingsphere.sharding.tables.cours e.actual-data-nodes= m1.course_$->{1..2}
# 指定 course 表里面主键 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column= cid
spring.shardingsphere.sharding.tables.course.key-generator.type= SNOWFLAKE
# 指定表分片策略 约定 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到course_2 表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression= course_$->{cid % 2 + 1}
# 指定数据库分片策略 约定 user_id 是偶数添加 m1,是奇数添加 m2
#spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1}
spring.shardingsphere.sharding.tables.course.database e-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1}
# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true
配置垂直分库策略
# shardingjdbc 分片策略
# 配置数据源,给数据源起名称,
# 水平分库,配置两个数据源
spring.shardingsphere.datasource.names= m1,m2,m0
# 一个实体类对应两张表,覆盖
spring.main.allow- - bean- - definition- -o o verriding= true
#配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
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/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username= root
spring.shardingsphere.datasource.m1.password= root
#配置第二个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere. datasource.m2.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver- - class- - name= com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url= jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphe re.datasource.m2.username= root
spring.shardingsphere.datasource.m2.password= root
#配置第三个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m0.type= com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name= com.mys ql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url= jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username= root
spring.shardingsphere.datasource.m0.password= root
# 配置 user_db 数据库里面 t_user 专库专表
spring.sha rdingsphere.sharding.tables.t_user.actual-data-nodes= m$->{0}.t_user
# 指定 course 表里面主键 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column= user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type= SNOWFLAKE
# 指定表分片策略 约定 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到course_2 表
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column= user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression= t_user
Sharding-JDBC 操作公共表
1、公共表
(1)存储固定数据的表,表数据很少发生变化,查询时候经常进行关联
(2)在每个数据库中创建出相同结构公共表
2、在多个数据库都创建相同结构公共表
配置公共表
# 配置公共表
spring.shardingsphere.sharding.broadcast-tables= t_udict
spring.shardingsphere.sharding.tables.t_udict.key-generator.column= dictid
spring.shardingsphere.sharding.tables.t_udict.key-generator.type= SNOWFLAKE
Sharding-JDBC 实现读写分离
读写分离的概念
读写分离原理
mysql配置主从复制请操作mysql集群方案
配置
1)配置读写分离策略
# user_db 从服务器
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:3307/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.dataso urce.s0.password=root
# 主库从库逻辑数据源定义 ds0 为 user_db
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name= m0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names= s0
# 配置 user_db 数据库里面 t_user 专库专表
#spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{0}.t_user
# t_user 分表策略,固定分配至 ds0 的 t_user 真实表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=ds0.t_user
Sharding-Proxy 简介
1、数据库代理端
2 、Sharding-Proxy 独立应用, 独立应用, 需要 安装服务,进行分库分表或者读写分离配置,启动
使用
3 、安装
(1)下载安装软件
(2)把下载之后压缩文件,解压,启动 bin 目录启动文件就可以了
Sharding-Proxy 配置 (分表)
1 、进入 conf 目录,修改文件 server.yaml ,打开两段内容注释
2 、进入 conf 目录,修改 config-sharding.yaml
(1)复制 mysql 驱动 jar 包到 lib 目录
(2)配置分库分表规则
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0}.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
bindingTables:
- t_order
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${0}
defaultTableStrategy:
none:
3 、启动 Sharding-Proxy 服务
(1)Sharding-Proxy 默认端口号 3307
4 、通过 Sharding-Proxy 启动端口进行连接
(1)打开 cmd 窗口连接 Sharding-Proxy,连接方式和连接 mysql 一样的
(2)进行 sql 命令操作看到只有一个库
(3)在 sharding_db 数据库创建表
(4)向表添加一条记录
Sharding-Proxy 配置 (分库)
1 、创建两个数据库
2 、找到 conf 目录,config-sharding.yaml
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/edu_db_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://127.0.0.1:3306/edu_db_2?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${1..2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2 + 1}
keyGenerator:
type: SNOWFLAKE
column: order_id
bindingTables:
- t_order
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
3 、启动 Sharding-Proxy
4、打开 cmd 仓库,连接 Sharding-Proxy 服务
(1)创建数据库表,向表添加记录
(2)连接本地 3306 的 MySql 数据库服务器,表已经创建出来,表里面有数据