电竞比分网-中国电竞赛事及体育赛事平台

分享

基于SpringCloud實(shí)現(xiàn)Shard-Jdbc的分庫分表模式,數(shù)據(jù)庫擴(kuò)容方案

 丹楓無跡 2021-08-07

本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里

一、項(xiàng)目結(jié)構(gòu)

1、工程結(jié)構(gòu)

2、模塊命名

shard-common-entity:   公共代碼塊
shard-open-inte:        開放接口管理
shard-eureka-7001:      注冊(cè)中心
shard-two-provider-8001: 8001 基于兩臺(tái)庫的服務(wù)
shard-three-provider-8002:8002 基于三臺(tái)庫的服務(wù)

3、代碼依賴結(jié)構(gòu)

4、項(xiàng)目啟動(dòng)順序

(1)shard-eureka-7001:        注冊(cè)中心
(2)shard-two-provider-8001:  8001 基于兩臺(tái)庫的服務(wù)
(3)shard-three-provider-8002:8002 基于三臺(tái)庫的服務(wù)

按照順序啟動(dòng),且等一個(gè)服務(wù)完全啟動(dòng)后,在啟動(dòng)下一個(gè)服務(wù),不然可能遇到一些坑。

二、核心代碼塊

1、8001 服務(wù)提供一個(gè)對(duì)外服務(wù)

基于Feign的調(diào)用方式
作用:基于兩臺(tái)分庫分表的數(shù)據(jù)查詢接口。

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
 * 對(duì)外開放接口
 */
@FeignClient(value = "shard-provider-8001")
public interface TwoOpenService {
    @RequestMapping("/selectOneByPhone/{phone}")
    TableOne selectOneByPhone(@PathVariable("phone") String phone) ;
}

2、8002 服務(wù)提供一個(gè)對(duì)外服務(wù)

基于Feign的調(diào)用方式
作用:基于三臺(tái)分庫分表的數(shù)據(jù)存儲(chǔ)接口。

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;

/**
 * 數(shù)據(jù)遷移服務(wù)接口
 */
@FeignClient(value = "shard-provider-8002")
public interface MoveDataService {
    @RequestMapping("/moveData")
    Integer moveData (@RequestBody TableOne tableOne) ;
}

3、基于8002服務(wù)數(shù)據(jù)查詢接口

查詢流程圖

代碼塊

/**
 * 8001 端口 :基于兩臺(tái)分庫分表策略的數(shù)據(jù)查詢接口
 */
@Resource
private TwoOpenService twoOpenService ;
@Override
public TableOne selectOneByPhone(String phone) {
    TableOne tableOne = tableOneMapper.selectOneByPhone(phone);
    if (tableOne != null){
        LOG.info("8002 === >> tableOne :"+tableOne);
    }
    // 8002 服務(wù)沒有查到數(shù)據(jù)
    if (tableOne == null){
        // 調(diào)用 8001 開放的查詢接口
        tableOne = twoOpenService.selectOneByPhone(phone) ;
        LOG.info("8001 === >> tableOne :"+tableOne);
    }
    return tableOne ;
}

4、基于 8001 數(shù)據(jù)掃描遷移代碼

遷移流程圖

代碼塊

/**
 * 8002 端口開放的數(shù)據(jù)入庫接口
 */
@Resource
private MoveDataService moveDataService ;
/**
 * 掃描,并遷移數(shù)據(jù)
 * 以 庫 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 對(duì)應(yīng)的數(shù)據(jù)庫: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()) ;
            // 只演示向數(shù)據(jù)新加庫 ds_4 遷移的數(shù)據(jù)
            if (db_num.equals("ds_4")){
                i += 1 ;
                LOG.info("遷移總數(shù)數(shù)=>" + i + "=>庫位置=>"+db_num+"=>表位置=>"+tb_num+"=>數(shù)據(jù):【"+tableOne+"】");
                // 掃描完成:執(zhí)行新庫遷移和舊庫清理過程
                moveDataService.moveData(tableOne) ;
                // dataTwoTemplate.update("DELETE FROM table_one_1 WHERE id=? AND phone=?",tableOne.getId(),tableOne.getPhone());
            }
        }
    }
}

三、演示執(zhí)行流程

1、項(xiàng)目流程圖

2、測(cè)試執(zhí)行流程

(1)、訪問8002 數(shù)據(jù)查詢端口

http://127.0.0.1:8002/selectOneByPhone/phone20
日志輸出:
8001 服務(wù)查詢到數(shù)據(jù)
8001 === >> tableOne :+{tableOne}

(2)、執(zhí)行8001 數(shù)據(jù)掃描遷移

http://127.0.0.1:8001/scanData

(3)、再次訪問8002 數(shù)據(jù)查詢端口

http://127.0.0.1:8002/selectOneByPhone/phone20
日志輸出:
8002 服務(wù)查詢到數(shù)據(jù)
8002 === >> tableOne :+{tableOne}

四、源代碼地址

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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多