天天看點

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址

本文源碼: GitHub·點這裡 || GitEE·點這裡

一、項目結構

1、工程結構

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址

2、子產品命名

shard-common-entity:   公共代碼塊
shard-open-inte:        開放接口管理
shard-eureka-7001:      注冊中心
shard-two-provider-8001: 8001 基于兩台庫的服務
shard-three-provider-8002:8002 基于三台庫的服務           

3、代碼依賴結構

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址

4、項目啟動順序

(1)shard-eureka-7001:        注冊中心
(2)shard-two-provider-8001:  8001 基于兩台庫的服務
(3)shard-three-provider-8002:8002 基于三台庫的服務           

按照順序啟動,且等一個服務完全啟動後,在啟動下一個服務,不然可能遇到一些坑。

二、核心代碼塊

1、8001 服務提供一個對外服務

基于Feign的調用方式

作用:基于兩台分庫分表的資料查詢接口。

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import shard.jdbc.common.entity.TableOne;
/**
 * shard-two-provider-8001
 * 對外開放接口
 */
@FeignClient(value = "shard-provider-8001")
public interface TwoOpenService {
    @RequestMapping("/selectOneByPhone/{phone}")
    TableOne selectOneByPhone(@PathVariable("phone") String phone) ;
}           

2、8002 服務提供一個對外服務

作用:基于三台分庫分表的資料存儲接口。

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import shard.jdbc.common.entity.TableOne;

/**
 * 資料遷移服務接口
 */
@FeignClient(value = "shard-provider-8002")
public interface MoveDataService {
    @RequestMapping("/moveData")
    Integer moveData (@RequestBody TableOne tableOne) ;
}           

3、基于8002服務資料查詢接口

查詢流程圖

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址

代碼塊

/**
 * 8001 端口 :基于兩台分庫分表政策的資料查詢接口
 */
@Resource
private TwoOpenService twoOpenService ;
@Override
public TableOne selectOneByPhone(String phone) {
    TableOne tableOne = tableOneMapper.selectOneByPhone(phone);
    if (tableOne != null){
        LOG.info("8002 === >> tableOne :"+tableOne);
    }
    // 8002 服務沒有查到資料
    if (tableOne == null){
        // 調用 8001 開放的查詢接口
        tableOne = twoOpenService.selectOneByPhone(phone) ;
        LOG.info("8001 === >> tableOne :"+tableOne);
    }
    return tableOne ;
}           

4、基于 8001 資料掃描遷移代碼

遷移流程圖

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址
/**
 * 8002 端口開放的資料入庫接口
 */
@Resource
private MoveDataService moveDataService ;
/**
 * 掃描,并遷移資料
 * 以 庫 db_2 的 table_one_1 表為例
 */
@Override
public void scanDataRun() {
    String sql = "SELECT id,phone,back_one backOne,back_two backTwo,back_three backThree FROM table_one_1" ;
    // dataTwoTemplate 對應的資料庫:ds_2
    List<TableOne> tableOneList = dataTwoTemplate.query(sql,new Object[]{},new BeanPropertyRowMapper<>(TableOne.class)) ;
    if (tableOneList != null && tableOneList.size()>0){
        int i = 0 ;
        for (TableOne tableOne : tableOneList) {
            String db_num = HashUtil.moveDb(tableOne.getPhone()) ;
            String tb_num = HashUtil.moveTable(tableOne.getPhone()) ;
            // 隻示範向資料新加庫 ds_4 遷移的資料
            if (db_num.equals("ds_4")){
                i += 1 ;
                LOG.info("遷移總數數=>" + i + "=>庫位置=>"+db_num+"=>表位置=>"+tb_num+"=>資料:【"+tableOne+"】");
                // 掃描完成:執行新庫遷移和舊庫清理過程
                moveDataService.moveData(tableOne) ;
                // dataTwoTemplate.update("DELETE FROM table_one_1 WHERE id=? AND phone=?",tableOne.getId(),tableOne.getPhone());
            }
        }
    }
}           

三、示範執行流程

1、項目流程圖

SpringCloud實作分庫分表模式下,資料庫實時擴容方案一、項目結構二、核心代碼塊三、示範執行流程四、源代碼位址

2、測試執行流程

(1)、通路8002 資料查詢端口

http://127.0.0.1:8002/selectOneByPhone/phone20
日志輸出:
8001 服務查詢到資料
8001 === >> tableOne :+{tableOne}           

(2)、執行8001 資料掃描遷移

http://127.0.0.1:8001/scanData           

(3)、再次通路8002 資料查詢端口

http://127.0.0.1:8002/selectOneByPhone/phone20
日志輸出:
8002 服務查詢到資料
8002 === >> tableOne :+{tableOne}           

四、源代碼位址

GitHub·位址
https://github.com/cicadasmile/spring-cloud-base
GitEE·位址
https://gitee.com/cicadasmile/spring-cloud-base