一篇学会 Sharding 垂直分库分表

数据库 其他数据库
这种垂直分库分表,实际上就是通过不同的数据源来进行操作的,而通过给mybatis的mapper配置不同的数据源也是能实现的,但是还是看个人选择吧。大家学会如何使用 Sharding-JDBC 进行分库分表了么?

之前的几篇文章,阿粉已经说了这个SpringBoot整合 Sharding-JDBC 实现了水平的分库分表,也是我们在日常的业务中最经常用到的,把数据进行水平分库,比如按照日期分库,按照奇偶性用户ID来水平分库,今天阿粉来说说如何使用 Sharding-JDBC 进行垂直切分表和数据库。

前情回顾之什么是垂直切分

什么是垂直切分,垂直分库是指按照业务将表进行分类,分布到不同的数据库上面,每个库可以放在不同的服务器上,它的核心理念是专库专用,也就是说,我们需要把不同之间的业务进行分库,比如,支付业务我们可以创建一个库,而订单业务我们可以再用另外的一个库保存数据,说起来是简单,实现起来也并没有想象的那么难办。我们看看如何实现。

垂直分表

垂直分表就是将一个表细分,且在同一个库里,正常操作即可。

这种相对来说就压根没必要用sharding-sphere,数据一部分在一个表,和数据存储在另外一个表,那就意味着,这就是两个表存了不同的数据,比如商品服务,我们把商品基本信息放在一张表,商品详情放在一张表,这就相当于是垂直分表了,但是看起来总是这么的奇怪,奇怪归奇怪,他还就是这样的。而垂直分库就不是这样的了。我们来看看如何实现。

垂直分库

第一步

我们还是需要去创建数据库

图片

然后创建我们的指定的表

DROP TABLE IF EXISTS users; 
CREATE TABLE users ( 
id BIGINT(20) PRIMARY KEY, 
username VARCHAR(20) ,
phone VARCHAR(11),
STATUS VARCHAR(11) );
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

第二步

接下来我们就要和之前一样了,开始配置我们的配置数据。

spring:
  application:
    name: sharding-jdbc-simple
  http:
    encoding:
      enabled: true
      charset: UTF-8
      force: true
  main:
    allow-bean-definition-overriding: true

  #定义数据源
  shardingsphere:
    datasource:
      names: db1,db2,db3
      db1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/order?characterEncoding=UTF-8&useSSL=false
        username: root
        password: 123456
      db2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ordersharding?characterEncoding=UTF-8&useSSL=false
        username: root
        password: 123456
        #配置user的数据源
      db3:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8&useSSL=false
        username: root
        password: 123456
        ## 分库策略,以user_id为分片键,分片策略为user_id % 2 + 1,user_id为偶数操作db1数据源,否则操作db2。
    sharding:
      tables:
      #配置db3的数据节点
        users:
          actual-data-nodes: db$->{3}.users
          table-strategy:
            inline:
              sharding- column: id
              algorithm-expression: users
        orderinfo:
          actual-data-nodes: db$->{1..2}.orderinfo
          key-generator:
            column: order_id
            type: SNOWFLAKE
          database-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: db$->{user_id % 2 + 1}
    props:
      sql:
        show: true
server:
  servlet:
    context-path: /sharding-jdbc
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

=接下来就是去写一组插入语句,然后我们把数据插入到数据库测试一下。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RunBoot.class)
public class UsersDaoTest {

    @Autowired
    UsersDao usersDao;

    @Test
    public void testInsert(){

        for (int i = 0; i < 10; i++) {
            Long id = i+100L;
            usersDao.insertUser(id,"大佬"+i, "17458236963","1");
        }
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  /**
     * 新增用户
     * */
    @Insert("insert into users(id,username,phone,status) values(#{id},#{username},#{phone},#{status})")
    int insertUser(@Param("id") Long id, @Param("username") String username, @Param("phone") String phone,@Param("status") String status);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

图片

看着截图的样子,阿粉感觉是没啥问题,我们再去数据库验证一下。

图片

也确定了数据保存进去了,这就是垂直分库

俺么我们什么时候垂直分库呢?答案是根据业务逻辑进行分割。比如我们可以把用户表和用户相关的表分配到用户数据库中,而把商品表和商品相关的数据分配到商品数据库中。

阿粉觉得这种垂直分库分表,实际上就是通过不同的数据源来进行操作的,而通过给mybatis的mapper配置不同的数据源也是能实现的,但是还是看个人选择吧。

大家学会如何使用 Sharding-JDBC 进行分库分表了么?

责任编辑:武晓燕 来源: 鸭血粉丝Tang
相关推荐

2020-07-30 17:59:34

分库分表SQL数据库

2020-11-18 09:39:02

MySQL数据库SQL

2021-07-02 09:45:29

MySQL InnoDB数据

2023-07-24 09:00:00

数据库MyCat

2021-09-07 17:54:04

OpenGauss分区表索引

2019-03-06 14:42:01

数据库分库分表

2023-12-21 18:11:51

数据库分库分表跨库

2022-01-02 08:43:46

Python

2022-08-29 08:00:11

哈希表数组存储桶

2022-02-07 11:01:23

ZooKeeper

2022-12-09 09:21:10

分库分表算法

2021-07-06 08:59:18

抽象工厂模式

2021-05-11 08:54:59

建造者模式设计

2023-01-03 08:31:54

Spring读取器配置

2021-07-05 22:11:38

MySQL体系架构

2022-08-26 09:29:01

Kubernetes策略Master

2023-11-28 08:29:31

Rust内存布局

2022-08-23 08:00:59

磁盘性能网络

2022-04-12 08:30:52

回调函数代码调试

2021-07-02 08:51:29

源码参数Thread
点赞
收藏

51CTO技术栈公众号