Sharding-JDBC分库分表从入门到熟练(5)

10 篇文章 1 订阅
7 篇文章 0 订阅

5 水平分表 & 垂直分表

​ 在前篇的文档中我们通过实例已经实现过水平分表的演示,这里不再重复介绍。垂直分表依据概念其实是依据表字段的重要性,拆分为多个表,类似与父子表的结构,数据库设计中经常使用,这里也不再进行演示。

6 水平分库

​ 依照前边文档中的案例我们来实现水平分库,水平分库其实是把同一个表的数据按照规则拆分到不同的数据库中。

​ 1、将原有order_db数据库拆分为order_db_1和order_db_2两个数据库,

​ 2、分片规则修改,由于数据库拆分成两个,所以数据源应该配置为两个,分库的策略和分表的策略类似。

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      names: order1,order2
      order1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_1
        username: root
        password: 123456
      order2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_2
        username: root
        password: 123456
    sharding:
      tables:
        t_order:
          databaseStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: order$->{user_id % 2 + 1}

​ 3、测试订单插入和订单查询,代码沿用快速入门阶段代码即可。

​ 4、分库分表整合分片策略。

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      names: order1,order2
      order1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_1
        username: root
        password: 123456
      order2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_2
        username: root
        password: 123456
    sharding:
      tables:
        t_order:
          databaseStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: order$->{user_id % 2 + 1}
          actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
          keyGenerator: #主键策略
            column: order_id
            type: SNOWFLAKE
          tableStrategy:
            inline:
              shardingColumn: order_id
              algorithmExpression: t_order_$->{order_id % 2 + 1}

7 垂直分库

​ 前面已经介绍过垂直分库的基本概念,指按照业务将表进行分类,分布到不同的数据库上面,每个库可以放到不同的服务器上,它的核心理念是专库专用

​ 1、创建数据库user_db

CREATE DATABASE 'user_db' CHARACTER 'utf8';

​ 2、创建用户表

CREATE TABLE 't_user' (
	'user_id' bigint(20) NOT NULL COMMENT '用户id''name' varchar(20) NOT NULL COMMENT '用户姓名''user_type' char(1) NOT NULL COMMENT '用户类型'PRIMARY KEY ('user_id') USING BTREE
);

​ 3、分片规则配置

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      names: order1,order2,userDb
      order1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_1
        username: root
        password: 123456
      order2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_2
        username: root
        password: 123456
      #新增user_db数据源配置
      userDb:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/user_db
        username: root
        password: 123456
    sharding:
      tables:
        t_order:
          databaseStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: order$->{user_id % 2 + 1}
          actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
          keyGenerator: #主键策略
            column: order_id
            type: SNOWFLAKE
          tableStrategy:
            inline:
              shardingColumn: order_id
              algorithmExpression: t_order_$->{order_id % 2 + 1}
         #垂直分库策略;由于没有分库分表,所以节点和表分片策略可以直接写死
         t_user:
          actualDataNodes: userDb.t_user #配置节点
          #配置表分片规则
          tableStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: t_user

8 公共表

​ 公共表属于系统中数据量较小,变动少,而且属于高频联合查询的依赖表,例如参数表、字典表等,可以将这些表每个数据库都保存一份,所有更新操作都同时发送到所有分库节点执行。

​ 1、创建所有数据库中公共表t_dict

CREATE TABLE 't_dict' (
	'dict_id' bigint(20) NOT NULL COMMENT '字典id''type' varchar(20) NOT NULL COMMENT '字典类型''code' varchar(20) NOT NULL COMMENT '字典编码''value' varchar(20) NOT NULL COMMENT '字典值'PRIMARY KEY ('dict_id') USING BTREE
);

​ 2、配置公共表规则

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      names: order1,order2,userDb
      order1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_1
        username: root
        password: 123456
      order2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/order_db_2
        username: root
        password: 123456
      #新增user_db数据源配置
      userDb:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:33068/user_db
        username: root
        password: 123456
    sharding:
      #配置公共表
      broadcast-tables: t_dict
      tables:
        t_order:
          databaseStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: order$->{user_id % 2 + 1}
          actualDataNodes: order$->{1..2}.t_order_$->{1..2} #配置节点
          keyGenerator: #主键策略
            column: order_id
            type: SNOWFLAKE
          tableStrategy:
            inline:
              shardingColumn: order_id
              algorithmExpression: t_order_$->{order_id % 2 + 1}
         t_user:
          actualDataNodes: userDb.t_user #配置节点
          tableStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: t_user
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值