好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 淺談訂單重構之 MySQL 分庫分表實戰篇

淺談訂單重構之 MySQL 分庫分表實戰篇

熱門標簽:拉卡拉外呼系統 云南電商智能外呼系統價格 大眾點評星級酒店地圖標注 話務外呼系統怎么樣 臨清電話機器人 高清地圖標注道路 外東北地圖標注 400電話可以辦理嗎 智能外呼系統復位

一、目標

本文將完成如下目標:

  • 分表數量: 256    分庫數量: 4
  • 以用戶ID(user_id) 為數據庫分片Key
  • 最后測試訂單創建,更新,刪除, 單訂單號查詢,根據user_id查詢列表操作。

架構圖:

表結構如下:

CREATE TABLE `order_XXX` (
  `order_id` bigint(20) unsigned NOT NULL,
  `user_id` int(11) DEFAULT '0' COMMENT '訂單id',
  `status` int(11) DEFAULT '0' COMMENT '訂單狀態',
  `booking_date` datetime DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`order_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_bdate` (`booking_date`),
  KEY `idx_ctime` (`create_time`),
  KEY `idx_utime` (`update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注:  000= XXX = 255, 本文重點在于分庫分表實踐, 只保留具有代表性字段,其它場景可以在此基礎上做改進。

全局唯一ID設計

要求:1.全局唯一 2:粗略有序 3:可反解出庫編號

  • 1bit + 39bit時間差 + 8bit機器號 + 8bit用戶編號(庫號) + 8bit自增序列

訂單號組成項 保留字段 毫秒級時間差 機器數 用戶編號(表編號) 自增序列
所占字節(單位bit) 1 39 8 8 8

單機最大QPS: 256000 使用壽命: 17年

二、環境準備

1、基本信息

版本 備注
SpringBoot 2.1.10.RELEASE
Mango 1.6.16 wiki地址:https://github.com/jfaster/mango
HikariCP 3.2.0
Mysql 5.7 測試使用docker一鍵搭建

2、數據庫環境準備

進入mysql:

#主庫
 mysql -h 172.30.1.21 -uroot -pbytearch

#從庫
 mysql -h 172.30.1.31 -uroot -pbytearch


進入容器

#主
docker exec -it db_1_master /bin/bash

#從
docker exec -it db_1_slave /bin/bash


查看運行狀態

#主
docker exec db_1_master sh -c 'mysql -u root -pbytearch -e "SHOW MASTER STATUS \G"'
#從
docker exec db_1_slave sh -c 'mysql -u root -pbytearch -e "SHOW SLAVE STATUS \G"' 

3、建庫 導入分表

(1)在mysql master實例分別建庫

172.30.1.21(   o rder_db_ 1) ,  172.30.1.22( order_db_2) ,

172.30.1.23( ord er_db_3) ,   172.30.1.24( order_db_4 )

(2)依次導入建表SQL 命令為

mysql -uroot -pbytearch -h172.30.1.21 order_db_1fast-cloud-mysql-sharding/doc/sql/order_db_1.sql;
mysql -uroot -pbytearch -h172.30.1.22 order_db_2fast-cloud-mysql-sharding/doc/sql/order_db_2.sql;
mysql -uroot -pbytearch -h172.30.1.23 order_db_3fast-cloud-mysql-sharding/doc/sql/order_db_3.sql;
mysql -uroot -pbytearch -h172.30.1.24 order_db_4fast-cloud-mysql-sharding/doc/sql/order_db_4.sql;  

三、配置實踐

1、pom文件  

     !-- mango 分庫分表中間件 --> 
            dependency>
                groupId>org.jfaster/groupId>
                artifactId>mango-spring-boot-starter/artifactId>
                version>2.0.1/version>
            /dependency>
         
             !-- 分布式ID生成器 -->
            dependency>
                groupId>com.bytearch/groupId>
                artifactId>fast-cloud-id-generator/artifactId>
                version>${version}/version>
            /dependency>

            !-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            dependency>
                groupId>mysql/groupId>
                artifactId>mysql-connector-java/artifactId>
                version>6.0.6/version>
            /dependency>

2、常量配置

package com.bytearch.fast.cloud.mysql.sharding.common;

/**
 * 分庫分表策略常用常量
 */
public class ShardingStrategyConstant {
    /**
     * database 邏輯名稱 ,真實庫名為 order_db_XXX
     */
    public static final String LOGIC_ORDER_DATABASE_NAME = "order_db";
    /**
     * 分表數 256,一旦確定不可更改
     */
    public static final int SHARDING_TABLE_NUM = 256;

    /**
     * 分庫數, 不建議更改, 可以更改,但是需要DBA遷移數據
     */
    public static final int SHARDING_DATABASE_NODE_NUM = 4;
}

3、yml 配置

4主4從數據庫配置, 這里僅測試默認使用root用戶密碼,生產環境不建議使用root用戶。

mango:
  scan-package: com.bytearch.fast.cloud.mysql.sharding.dao
  datasources:
    - name: order_db_1
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://172.30.1.21:3306/order_db_1?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
        user-name: root
        password: bytearch
        maximum-pool-size: 10
        connection-timeout: 3000
      slaves:
        - driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://172.30.1.31:3306/order_db_1?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
          user-name: root
          password: bytearch
          maximum-pool-size: 10
          connection-timeout: 3000
    - name: order_db_2
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://172.30.1.22:3306/order_db_2?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
        user-name: root
        password: bytearch
        maximum-pool-size: 10
        connection-timeout: 3000
      slaves:
        - driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://172.30.1.32:3306/order_db_2?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
          user-name: root
          password: bytearch
          maximum-pool-size: 10
          connection-timeout: 3000
    - name: order_db_3
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://172.30.1.23:3306/order_db_3?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
        user-name: root
        password: bytearch
        maximum-pool-size: 10
        connection-timeout: 3000
      slaves:
        - driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://172.30.1.33:3306/order_db_3?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
          user-name: root
          password: bytearch
          maximum-pool-size: 10
          connection-timeout: 3000
    - name: order_db_4
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://172.30.1.24:3306/order_db_4?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
        user-name: root
        password: bytearch
        maximum-pool-size: 10
        connection-timeout: 3000
      slaves:
        - driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://172.30.1.34:3306/order_db_4?useUnicode=truecharacterEncoding=utf8autoReconnect=truerewriteBatchedStateconnectTimeout=1000socketTimeout=5000useSSL=false
          user-name: root
          password: bytearch
          maximum-pool-size: 10
          connection-timeout: 300

4、分庫分表策略

1). 根據order_id為shardKey分庫分表策略

package com.bytearch.fast.cloud.mysql.sharding.strategy;

import com.bytearch.fast.cloud.mysql.sharding.common.ShardingStrategyConstant;
import com.bytearch.id.generator.IdEntity;
import com.bytearch.id.generator.SeqIdUtil;
import org.jfaster.mango.sharding.ShardingStrategy;

/**
 * 訂單號分庫分表策略
 */
public class OrderIdShardingStrategy implements ShardingStrategyLong, Long> {
    @Override
    public String getDataSourceFactoryName(Long orderId) {
        if (orderId == null || orderId  0L) {
            throw new IllegalArgumentException("order_id is invalid!");
        }
        IdEntity idEntity = SeqIdUtil.decodeId(orderId);
        if (idEntity.getExtraId() >= ShardingStrategyConstant.SHARDING_TABLE_NUM) {
            throw new IllegalArgumentException("sharding table Num is invalid, tableNum:" + idEntity.getExtraId());
        }
        //1. 計算步長
        int step = ShardingStrategyConstant.SHARDING_TABLE_NUM / ShardingStrategyConstant.SHARDING_DATABASE_NODE_NUM;
        //2. 計算出庫編號
        long dbNo = Math.floorDiv(idEntity.getExtraId(), step) + 1;
        //3. 返回數據源名
        return String.format("%s_%s", ShardingStrategyConstant.LOGIC_ORDER_DATABASE_NAME, dbNo);
    }

    @Override
    public String getTargetTable(String logicTableName, Long orderId) {
        if (orderId == null || orderId  0L) {
            throw new IllegalArgumentException("order_id is invalid!");
        }
        IdEntity idEntity = SeqIdUtil.decodeId(orderId);
        if (idEntity.getExtraId() >= ShardingStrategyConstant.SHARDING_TABLE_NUM) {
            throw new IllegalArgumentException("sharding table Num is invalid, tableNum:" + idEntity.getExtraId());
        }
        // 基于約定,真實表名為 logicTableName_XXX, XXX不足三位補0
        return String.format("%s_%03d", logicTableName, idEntity.getExtraId());
    }
}

2). 根據user_id 為shardKey分庫分表策略

package com.bytearch.fast.cloud.mysql.sharding.strategy;

import com.bytearch.fast.cloud.mysql.sharding.common.ShardingStrategyConstant;
import org.jfaster.mango.sharding.ShardingStrategy;

/**
 * 指定分片KEY 分庫分表策略
 */
public class UserIdShardingStrategy implements ShardingStrategyInteger, Integer> {

    @Override
    public String getDataSourceFactoryName(Integer userId) {
        //1. 計算步長 即單庫放得表數量
        int step = ShardingStrategyConstant.SHARDING_TABLE_NUM / ShardingStrategyConstant.SHARDING_DATABASE_NODE_NUM;
        //2. 計算出庫編號
        long dbNo = Math.floorDiv(userId % ShardingStrategyConstant.SHARDING_TABLE_NUM, step) + 1;
        //3. 返回數據源名
        return String.format("%s_%s", ShardingStrategyConstant.LOGIC_ORDER_DATABASE_NAME, dbNo);
    }

    @Override
    public String getTargetTable(String logicTableName, Integer userId) {
        // 基于約定,真實表名為 logicTableName_XXX, XXX不足三位補0
        return String.format("%s_%03d", logicTableName, userId % ShardingStrategyConstant.SHARDING_TABLE_NUM);
    }
}

5、dao層編寫

1). OrderPartitionByIdDao

package com.bytearch.fast.cloud.mysql.sharding.dao;

import com.bytearch.fast.cloud.mysql.sharding.common.ShardingStrategyConstant;
import com.bytearch.fast.cloud.mysql.sharding.pojo.entity.OrderEntity;
import com.bytearch.fast.cloud.mysql.sharding.strategy.OrderIdShardingStrategy;
import org.jfaster.mango.annotation.*;

@DB(name = ShardingStrategyConstant.LOGIC_ORDER_DATABASE_NAME, table = "order")
@Sharding(shardingStrategy = OrderIdShardingStrategy.class)
public interface OrderPartitionByIdDao {

    @SQL("INSERT INTO #table (order_id, user_id, status, booking_date, create_time, update_time) VALUES" +
            "(:orderId,:userId,:status,:bookingDate,:createTime,:updateTime)"
    )
    int insertOrder(@TableShardingBy("orderId") @DatabaseShardingBy("orderId") OrderEntity orderEntity);

    @SQL("UPDATE #table set update_time = now()" +
            "#if(:bookingDate != null),booking_date = :bookingDate #end " +
            "#if (:status != null), status = :status #end" +
            "WHERE order_id = :orderId"
    )
    int updateOrderByOrderId(@TableShardingBy("orderId") @DatabaseShardingBy("orderId") OrderEntity orderEntity);


    @SQL("SELECT * FROM #table WHERE order_id = :1")
    OrderEntity getOrderById(@TableShardingBy @DatabaseShardingBy Long orderId);

    @SQL("SELECT * FROM #table WHERE order_id = :1")
    @UseMaster
    OrderEntity getOrderByIdFromMaster(@TableShardingBy @DatabaseShardingBy Long orderId);

6、單元測試

@SpringBootTest(classes = {Application.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class ShardingTest {
    @Autowired
    OrderPartitionByIdDao orderPartitionByIdDao;

    @Autowired
    OrderPartitionByUserIdDao orderPartitionByUserIdDao;

    @Test
    public void testCreateOrderRandom() {
        for (int i = 0; i  20; i++) {
            int userId = ThreadLocalRandom.current().nextInt(1000,1000000);
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(SeqIdUtil.nextId(userId % ShardingStrategyConstant.SHARDING_TABLE_NUM));
            orderEntity.setStatus(1);
            orderEntity.setUserId(userId);
            orderEntity.setCreateTime(new Date());
            orderEntity.setUpdateTime(new Date());
            orderEntity.setBookingDate(new Date());
            int ret = orderPartitionByIdDao.insertOrder(orderEntity);
            Assert.assertEquals(1, ret);
        }
    }

    @Test
    public void testOrderAll() {
        //insert
        int userId = ThreadLocalRandom.current().nextInt(1000,1000000);
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(SeqIdUtil.nextId(userId % ShardingStrategyConstant.SHARDING_TABLE_NUM));
        orderEntity.setStatus(1);
        orderEntity.setUserId(userId);
        orderEntity.setCreateTime(new Date());
        orderEntity.setUpdateTime(new Date());
        orderEntity.setBookingDate(new Date());
        int i = orderPartitionByIdDao.insertOrder(orderEntity);
        Assert.assertEquals(1, i);

        //get from master
        OrderEntity orderInfo = orderPartitionByIdDao.getOrderByIdFromMaster(orderEntity.getOrderId());
        Assert.assertNotNull(orderInfo);
        Assert.assertEquals(orderInfo.getOrderId(), orderEntity.getOrderId());

        //get from slave
        OrderEntity slaveOrderInfo = orderPartitionByIdDao.getOrderById(orderEntity.getOrderId());
        Assert.assertNotNull(slaveOrderInfo);
        //update
        OrderEntity updateEntity = new OrderEntity();
        updateEntity.setOrderId(orderInfo.getOrderId());
        updateEntity.setStatus(2);
        updateEntity.setUpdateTime(new Date());
        int affectRows = orderPartitionByIdDao.updateOrderByOrderId(updateEntity);
        Assert.assertTrue( affectRows > 0);
    }

    @Test
    public void testGetListByUserId() {
        int userId = ThreadLocalRandom.current().nextInt(1000,1000000);
        for (int i = 0; i  5; i++) {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(SeqIdUtil.nextId(userId % ShardingStrategyConstant.SHARDING_TABLE_NUM));
            orderEntity.setStatus(1);
            orderEntity.setUserId(userId);
            orderEntity.setCreateTime(new Date());
            orderEntity.setUpdateTime(new Date());
            orderEntity.setBookingDate(new Date());
            orderPartitionByIdDao.insertOrder(orderEntity);
        }
        try {
            //防止主從延遲引起的校驗錯誤
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ListOrderEntity> orderListByUserId = orderPartitionByUserIdDao.getOrderListByUserId(userId);
        Assert.assertNotNull(orderListByUserId);
        Assert.assertTrue(orderListByUserId.size() == 5);
    }
}

大功告成:

四、總結

本篇主要介紹Java版使用Mango框架實現Mysql分庫分表實戰,分庫分表中間件也可以使用類似于ShardingJDBC,或者自研。

以上分庫分表數量僅供演示參考,實際工作中分表數量、分庫數量、是根據公司實際業務數據增長速度, 高峰期QPS,物理機器配置等等因素計算。

到此這篇關于淺談訂單重構之 MySQL 分庫分表實戰篇的文章就介紹到這了,更多相關MySQL 分庫分表內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL分庫分表與分區的入門指南
  • mysql死鎖和分庫分表問題詳解
  • MySQL 分表分庫怎么進行數據切分
  • MySql分表、分庫、分片和分區知識深入詳解
  • MySql分表、分庫、分片和分區知識點介紹
  • MySQL分庫分表總結講解
  • mysql分表分庫的應用場景和設計方式
  • mysql數據庫分表分庫的策略
  • MyBatis實現Mysql數據庫分庫分表操作和總結(推薦)
  • MYSQL數據庫數據拆分之分庫分表總結
  • Mysql數據庫分庫和分表方式(常用)
  • MYSQL性能優化分享(分庫分表)
  • MySQL分庫分表詳情

標簽:福州 溫州 揚州 無錫 阿里 定西 山西 三明

巨人網絡通訊聲明:本文標題《淺談訂單重構之 MySQL 分庫分表實戰篇》,本文關鍵詞  淺談,訂單,重構,之,MySQL,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《淺談訂單重構之 MySQL 分庫分表實戰篇》相關的同類信息!
  • 本頁收集關于淺談訂單重構之 MySQL 分庫分表實戰篇的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海e行智慧版| 春动莲房| 最新男同versios视频| 正点嫩模大尺度写真在线视频 | 粉嫩AV四季AV绯色AV第一区| www.日韩三级| 古代黄色小说| 女人下面喷水视频| 停电了被男同桌狂揉我奶胸动态图| 台湾白嫩bbwbbw| 久久青青草原国产精品免费| 主人变态调教男私奴视频| 国产微拍精品一区二区视频| 日本老肥熟| 一级特黄aa大片欧美| 亚洲Va欧美va国产综合剧情| 日照市| 美女隐私秘?无遮挡网站| 杨过小龙女级婬片A级艳片| 日韩欧美色图| 武松金莲纯肉H爽文| 天干天干天夜夜爽啪| 抱着cao才爽视频| 日日噜噜夜夜狠狠扒开双腿| 日本二区视频| 引诱亲女乱第24部分阅读| 日韩久久精品一区二区三区下载| 久久久影院亚洲精品| 姐姐能不能给我| 美女被叉叉的影院| 囯产精品久久久久久久久免费蜜桃| 欧美AV无码国产精品色草莓| 国产高清1024永久免费| 色戒在线观看无删减版观看| 婬乱生活H肉NP学生和老师| 国产传媒久久久久精品A片| 国产福利在线观看第二区| 精品人妻无码一区二区三区网站| 爽好舒服快大小柔动态图| 久久国产精品自线拍免费| 台湾gaysexchina同性free|