本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里
一、項(xiàng)目結(jié)構(gòu)
1、工程結(jié)構(gòu)

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、代碼依賴結(jié)構(gòu)

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ù)查詢接口。
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ù)查詢接口
查詢流程圖

代碼塊
/**
* 8001 端口 :基于兩臺(tái)分庫(kù)分表策略的數(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ù)沒(méi)有查到數(shù)據(jù)
if (tableOne == null){
// 調(diào)用 8001 開(kāi)放的查詢接口
tableOne = twoOpenService.selectOneByPhone(phone) ;
LOG.info("8001 === >> tableOne :"+tableOne);
}
return tableOne ;
}
4、基于 8001 數(shù)據(jù)掃描遷移代碼
遷移流程圖

代碼塊
/**
* 8002 端口開(kāi)放的數(shù)據(jù)入庫(kù)接口
*/
@Resource
private MoveDataService moveDataService ;
/**
* 掃描,并遷移數(shù)據(jù)
* 以 庫(kù) 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ù)庫(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í)行流程
1、項(xiàng)目流程圖

2、測(cè)試執(zhí)行流程
(1)、訪問(wèn)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)、再次訪問(wèn)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
|