一、Sharding Sphere简介
定义:Apache ShardingSphere 是一款开源分布式数据库生态项目,由 JDBC、Proxy 和 Sidecar(规划中) 3 款产品组成。其核心采用可插拔架构,通过组件扩展功能。对上以数据库协议及 SQL 方式提供诸多增强功能,包括数据分片、访问路由、数据安全等;对下原生支持 MySQL、PostgreSQL、SQL Server、Oracle 等多种数据存储引擎。 Apache ShardingSphere 项目理念,是提供数据库增强计算服务平台,进而围绕其上构建生态。充分利用现有数据库的计算与存储能力,通过插件化方式增强其核心能力,为企业解决在数字化转型中面临的诸多使用难点,为加速数字化应用赋能。
二、分库分表
垂直分库、分表
2.1 垂直分表:比如将一个表中多个字段拆分成不同的表,常用的字段放一个表中、不常用的数据放在不同的表中(比如课程基本信息表和课程描述表)
2.2 垂直分库:将不同业务的表放在不同的数据库中(比如课程和订单表)
水平分库、分表
2.3 水平分表:在同一个数据库中新建相同数据结构的表
2.4 水平分库:在不同的数据库中新建相同数据结构的表
分库分表应用和问题
1、应用
1 在数据库设计时考虑垂直分库和垂直分表
2 随着数据库数据量增加,不要马上考虑做水平切分,首先考虑缓存处理,读写分离,使用索引等方式,如果这些方式不能根本解决问题了,再考虑做水平分库和水平分表
2、分库分表问题
1 跨节点连接查询问题(分页、排序)
2 多数据源管理问题
三、Sharding-JDBC
3.1 概念:
ShardingSphere-JDBC 是 Apache ShardingSphere 的第一个产品,也是 Apache ShardingSphere 的前身。定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库,以 jar 包形式提供服务,无需额外部署和依赖,可理解为增强版的 JDBC 驱动,完全兼容 JDBC 和各种 ORM 框架。
- 适用于任何基于 JDBC 的 ORM 框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template 或直接使用 JDBC;
- 支持任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, HikariCP 等;
- 支持任意实现 JDBC 规范的数据库,目前支持 MySQL,PostgreSQL,Oracle,SQLServer 以及任何可使用 JDBC 访问的数据库。
ShardingSphere-JDBC | ShardingSphere-Proxy | |
---|---|---|
数据库 | 任意 | MySQL/PostgreSQL |
连接消耗数 | 高 | 低 |
异构语言 | 仅 Java | 任意 |
性能 | 损耗低 | 损耗略高 |
无中心化 | 是 | 否 |
静态入口 | 无 | 有 |
引入依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.2</version>
</dependency>
3.2 数据库创建语句
CREATE TABLE `course_1` (
`cid` bigint NOT NULL,
`cname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`user_id` bigint NOT NULL,
`cstatus` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
CREATE TABLE `course_2` (
`cid` bigint NOT NULL,
`cname` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`user_id` bigint NOT NULL,
`cstatus` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
CREATE TABLE`t_user` (
`user_id` bigint NOT NULL,
`user_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `t_dict` (
`dict_id` bigint NOT NULL,
`u_states` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`u_value` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`dict_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
3.3 水平分表
前提:分别创建course_db1数据库,库中创建course_1和course_2表
条件:根据cid取模的奇、偶保存到不同库中
# 设置模式
spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.overwrite=true
# 设置数据源名称
spring.shardingsphere.datasource.names=ds_0
# 根据数据源
spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_0.jdbcUrl=jdbc:mysql://localhost:3306/course_db1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_0.username=root
spring.shardingsphere.datasource.ds_0.password=
# 标准分片表配置,配置物理表
spring.shardingsphere.rules.sharding.tables.course.actual-data-nodes=ds_$->{0}.course_$->{1..2}
# 设置主键及生成策略,雪花算法
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column=cid
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=SNOWFLAKE
# 分表策略
# 指定分片策略 设置cid为偶数时保存到1表,为奇数时保存到2表
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-column=cid
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-algorithm-name=course_inline
# 保存策略
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.props.algorithm-expression=course_$->{cid%2+1}
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true
# 是否允许使用相同名称重新注册不同的bean实现
spring.main.allow-bean-definition-overriding=true
# 分布式序列配置(该项为必需)
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
3.4 水平分库分表
前提:分别创建course_db1,course_db2数据库,库中创建course_1和course_2表
条件:保存数据库根据user_id保存,保存表根据cid保存
# 设置模式
spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.overwrite=true
# 设置数据源名称
spring.shardingsphere.datasource.names=ds_0,ds_1
# 设置数据源
spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_0.jdbcUrl=jdbc:mysql://localhost:3306/course_db1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_0.username=root
spring.shardingsphere.datasource.ds_0.password=
spring.shardingsphere.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_1.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_1.jdbcUrl=jdbc:mysql://localhost:3306/course_db2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_1.username=root
spring.shardingsphere.datasource.ds_1.password=
# 设置绑定策略
# 绑定数据源绑定的表,配置表再那个数据库里面,表名称都是什么,ds_0.course_1,ds_0.course_2
spring.shardingsphere.rules.sharding.tables.course.actual-data-nodes=ds_$->{0..1}.course_$->{1..2}
# 设置主键及生成策略,雪花算法
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column=cid
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=SNOWFLAKE
# 分片策略
# 数据库
spring.shardingsphere.rules.sharding.tables.course.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.course.database-strategy.standard.sharding-algorithm-name=database-inline
# 表
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-column=cid
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-algorithm-name=course_inline
# 数据库保存算法
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds_$->{user_id%2}
# 表保存算法
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.props.algorithm-expression=course_$->{cid%2+1}
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.main.allow-bean-definition-overriding=true
3.5 定表保存
前提:创建user_db数据库,库中创建t_user表
条件:保存到指定数据库中
# 设置模式
spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.overwrite=true
# 设置数据源名称
spring.shardingsphere.datasource.names=ds_2
# 设置数据源
spring.shardingsphere.datasource.ds_2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_2.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_2.jdbcUrl=jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_2.username=root
spring.shardingsphere.datasource.ds_2.password=
# 绑定数据源绑定的表
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=ds_2.t_user
# 设置主键及生成策略,雪花算法
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.column=user_id
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.key-generator-name=SNOWFLAKE
spring.shardingsphere.rules.sharding.tables.t_user.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.t_user.database-strategy.standard.sharding-algorithm-name=user-inline
spring.shardingsphere.rules.sharding.sharding-algorithms.user-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.user-inline.props.algorithm-expression=ds_2
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.main.allow-bean-definition-overriding=true
3.6 多个数据库中相同表同时保存(广播表)
前提:创建course_db1, course_db2,user_db数据库,库中创建t_dcit表
条件:多个不同的数据库同时保存
# 设置模式
spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.overwrite=true
# 设置数据源名称
spring.shardingsphere.datasource.names=ds_0,ds_1,ds_2
# 设置数据源
spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_0.jdbcUrl=jdbc:mysql://localhost:3306/course_db1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_0.username=root
spring.shardingsphere.datasource.ds_0.password=
spring.shardingsphere.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_1.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_1.jdbcUrl=jdbc:mysql://localhost:3306/course_db2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_1.username=root
spring.shardingsphere.datasource.ds_1.password=
spring.shardingsphere.datasource.ds_2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_2.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds_2.jdbcUrl=jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds_2.username=root
spring.shardingsphere.datasource.ds_2.password=
# 广播表规则列表
spring.shardingsphere.rules.sharding.broadcast-tables[0]=t_dict
# 绑定数据源绑定的表,配置表再那个数据库里面,表名称都是什么,ds_0.course_1,ds_0.course_2
spring.shardingsphere.rules.sharding.tables.course.actual-data-nodes=ds_$->{0..1}.course_$->{1..2}
# 设置主键及生成策略,雪花算法
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column=cid
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=SNOWFLAKE
# 分库策略
# 所有数据库都按照user_id解析
#spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
#spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database-inline
# course按照user_id解析
spring.shardingsphere.rules.sharding.tables.course.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.course.database-strategy.standard.sharding-algorithm-name=database-inline
# 分表策略
# 指定分片策略 设置cid为偶数时保存到1表,为奇数时保存到2表
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-column=cid
spring.shardingsphere.rules.sharding.tables.course.table-strategy.standard.sharding-algorithm-name=course_inline
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds_$->{user_id%2}
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.course_inline.props.algorithm-expression=course_$->{cid%2+1}
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.main.allow-bean-definition-overriding=true
spring.shardingsphere.rules.sharding.broadcast-tables[0]=t_dict