ShardingSphere-JDBC

一、简介

ShardingSphere已经在2020年4月16日从Apache孵化器毕业,成为Apache顶级项目。

出现背景:当数据库数据巨大时,数据库读写性能将变得很低,为了解决此问题,设计时,可以将数据进行分别存储于不同数据库、不同表中,以降低单表量数量大问题;ShardingSphere它是一套开源的分布式数据库中间件。同于统筹协调分库分表下的数据读写,而让业务开发人员只关注数据层之外的工作,而不用去用业务代码判断操作那个库,那个表。

ShardingSphere JDBC: 目前只支持JAVA

二、数据分片

  1. 分表

    • 垂直分表

      垂直分表即专表专用,关系型数据库设计基本都采用的是该模式。将不同的内容拆分到不同的表中。比如保单信息,会拆分为保单基本表policy_0000、保单附件表policy_attachment_0000、保单受益人表policy_beneficiary_0000等。所有表结构都不同。

    • 水平分表

      水平分表即将数据量过大的表拆分为多张相同结构的表,每张表中都分摊存储数据,来降低数据库查询压力。比如将订单表拆分为 policy_0000、policy_0001、policy_0002。

  1. 分库

    • 垂直分库

      与垂直分表一样,即专库专用。不同的数据库来存储不同模块的表信息。如policy库和claim库

    • 水平分库

          水平分库即所有的数据库中存放相同的表信息。比如我们现在是按照租户来区分policy库:

           genesis_policy_00,test_policy_00。

  2. 读写分离

为了减少对数据库读的压力,适合读多写少的场景,需要数据库是数据库集群具有主从复制配置,有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/

  • 公司文档:ShardingSphere DAL 多租户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值