溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

發(fā)布時(shí)間:2020-02-25 18:43:13 來(lái)源:網(wǎng)絡(luò) 閱讀:261 作者:艾弗森哇 欄目:數(shù)據(jù)庫(kù)

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

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

SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

2、模塊命名

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

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

SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

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

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

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

二、核心代碼塊

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

基于Feign的調(diào)用方式 作用:基于兩臺(tái)分庫(kù)分表的數(shù)據(jù)查詢(xún)接口。鄭州治療不孕不育哪里好:http://www.zzfkyy120.com/

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ì)外開(kāi)放接口
?*/@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)分庫(kù)分表的數(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ù)查詢(xún)接口

查詢(xún)流程圖?SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

代碼塊

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

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

遷移流程圖?SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

代碼塊

鄭州不孕不育檢查:http://wapyyk.39.net/zz3/zonghe/1d427.html

/**
?*?8002?端口開(kāi)放的數(shù)據(jù)入庫(kù)接口
?*/@Resourceprivate?MoveDataService?moveDataService?;/**
?*?掃描,并遷移數(shù)據(jù)
?*?以?庫(kù)?db_2?的?table_one_1?表為例
?*/@Overridepublic?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ù)庫(kù):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ù)新加庫(kù)?ds_4?遷移的數(shù)據(jù)
????????????if?(db_num.equals("ds_4")){
????????????????i?+=?1?;
????????????????LOG.info("遷移總數(shù)數(shù)=>"?+?i?+?"=>庫(kù)位置=>"+db_num+"=>表位置=>"+tb_num+"=>數(shù)據(jù):【"+tableOne+"】");????????????????//?掃描完成:執(zhí)行新庫(kù)遷移和舊庫(kù)清理過(guò)程
????????????????moveDataService.moveData(tableOne)?;????????????????//?dataTwoTemplate.update("DELETE?FROM?table_one_1?WHERE?id=??AND?phone=?",tableOne.getId(),tableOne.getPhone());
????????????}
????????}
????}
}

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

https://www.jianshu.com/p/fc56f6221728

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

SpringCloud實(shí)現(xiàn)分庫(kù)分表模式下,數(shù)據(jù)庫(kù)實(shí)時(shí)擴(kuò)容方

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

(1)、訪問(wèn)8002 數(shù)據(jù)查詢(xún)端口

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

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

http://127.0.0.1:8001/scanData

(3)、再次訪問(wèn)8002 數(shù)據(jù)查詢(xún)端口

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


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI