一、简介
ShardingSphere已经在2020年4月16日从Apache孵化器毕业,成为Apache顶级项目。
出现背景:当数据库数据巨大时,数据库读写性能将变得很低,为了解决此问题,设计时,可以将数据进行分别存储于不同数据库、不同表中,以降低单表量数量大问题;ShardingSphere它是一套开源的分布式数据库中间件。同于统筹协调分库分表下的数据读写,而让业务开发人员只关注数据层之外的工作,而不用去用业务代码判断操作那个库,那个表。
ShardingSphere JDBC: 目前只支持JAVA
二、数据分片
-
分表
-
垂直分表
垂直分表即专表专用,关系型数据库设计基本都采用的是该模式。将不同的内容拆分到不同的表中。比如保单信息,会拆分为保单基本表policy_0000、保单附件表policy_attachment_0000、保单受益人表policy_beneficiary_0000等。所有表结构都不同。
-
水平分表
水平分表即将数据量过大的表拆分为多张相同结构的表,每张表中都分摊存储数据,来降低数据库查询压力。比如将订单表拆分为 policy_0000、policy_0001、policy_0002。
-
-
分库
-
垂直分库
-
水平分库
水平分库即所有的数据库中存放相同的表信息。比如我们现在是按照租户来区分policy库:
genesis_policy_00,test_policy_00。
-
-
读写分离
为了减少对数据库读的压力,适合读多写少的场景,需要数据库是数据库集群具有主从复制配置,有Master和Slave节点。
三、ShardingSphere-JDBC原理
关键概念:
-
逻辑表:如policy
-
真实表:如policy_0000,policy_0001
-
分片键:如id
-
分片算法: 可自定义
-
分片策略: 可自定义
course: actual-data-nodes: "course_$->{(0..1).collect{String.format(\"%04d\", it)}}" table-strategy: inline: sharding-column: "course_id" algorithm-expression: "policy_$->{String.format(\"%04d\", new BigDecimal(course_id).abs().divideAndRemainder(2)[1].longValue())}"
- 行表达式分片策略: inline 使用Groovy的表达式,提供对SQL语句中的=和IN的分片操作支持,只支持单分片键
-
分布式主键: 内置UUID,SNOWFLAKE,LEAF:https://tech.meituan.com/2017/04/21/mt-leaf.html
-
读写分离:主从复制需自己准备
-
加密&脱敏:内置AESShardingEncryptor,MD5ShardingEncryptor
四、实战
环境准备
代码位置:shardingsphere-jdbc4&shardingsphere-jdbc5-Java文档类资源-CSDN下载
1.数据库环境准备
CREATE TABLE `course_2` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NOT NULL,
`user_id` bigint(20) NOT NULL,
`cstatus` varchar(10) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 创建maven工程
pom.xml中引入
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
3. 创建SpringBoot启动类
@SpringBootApplication
@MapperScan("com.duan.mapper")
public class ShardingJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingJdbcApplication.class, args);
}
}
4.业务代码编写
@Data
public class Course {
@TableId(type = IdType.AUTO)
private Long cid;
private String cname;
private Long userId;
private String cstatus;
}
@Repository
public interface CourseMapper extends BaseMapper<Course> {
}
5.添加配置文件(重点)
单库水平分表配置
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
ds0:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db?serverTimezone=UTC
username: root
password: root
names: ds0
props:
sql:
show: true
sharding:
tables:
course:
actual-data-nodes: ds$->{0}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: course_$->{cid % 2 +1}
sharding-column: cid
6.编写测试类测试
单库多表
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
ds0:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db?serverTimezone=UTC
username: root
password: root
names: ds0
props:
sql:
show: true
sharding:
tables:
course:
actual-data-nodes: ds$->{0}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: course_$->{cid % 2 +1}
sharding-column: cid
多库多表
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: ds0,ds1
ds0:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db1?serverTimezone=UTC
username: root
password: root
ds1:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db2?serverTimezone=UTC
username: root
password: root
props:
sql:
show: true
sharding:
tables:
course:
actual-data-nodes: ds$->{0..1}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: course_$->{cid % 2 +1}
sharding-column: cid
database-strategy:
inline:
algorithm-expression: ds$->{user_id % 2}
sharding-column: user_id
广播表
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: ds0,ds1,ds2
ds0:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db1?serverTimezone=UTC
username: root
password: root
ds1:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/course_db2?serverTimezone=UTC
username: root
password: root
ds2:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/user_db?serverTimezone=UTC
username: root
password: root
props:
sql:
show: true
sharding:
broadcast-tables: t_data
tables:
course:
actual-data-nodes: ds$->{0..1}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: course_$->{cid % 2 +1}
sharding-column: cid
database-strategy:
inline:
algorithm-expression: ds$->{user_id % 2}
sharding-column: user_id
t_user:
actual-data-nodes: ds$->{2}.t_user
database-strategy:
inline:
algorithm-expression: ds2
sharding-column: user_id
key-generator:
column: user_id
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: t_user
sharding-column: user_id
t_data:
key-generator:
column: data_id
type: SNOWFLAKE
五、参考资源
-
官网地址:https://shardingsphere.apache.org/document/current/en/overview/
-
中文文档:https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/