SpringCloud实现分库分表模式下,数据库实时扩容方案

本文源码:GitHub·点这里 || GitEE·点这里

一、项目结构

1、工程结构

2、模块命名

1shard-common-entity: 公共代码块 2shard-open-inte: 开放接口管理 3shard-eureka-7001: 注册中心 4shard-two-provider-80018001 基于两台库的服务 5shard-three-provider-80028002 基于三台库的服务

3、代码依赖结构

4、项目启动顺序

11)shard-eureka-7001: 注册中心 22)shard-two-provider-80018001 基于两台库的服务 33)shard-three-provider-80028002 基于三台库的服务

按照顺序启动,且等一个服务完全启动后,在启动下一个服务,不然可能遇到一些坑。

二、核心代码块

1、8001 服务提供一个对外服务

基于Feign的调用方式 作用:基于两台分库分表的数据查询接口。

1import org.springframework.cloud.netflix.feign.FeignClient; 2import org.springframework.web.bind.annotation.PathVariable; 3import org.springframework.web.bind.annotation.RequestMapping; 4import shard.jdbc.common.entity.TableOne; 5/** 6 * shard-two-provider-8001 7 * 对外开放接口 8 */ 9@FeignClient(value = "shard-provider-8001") 10public interface TwoOpenService { 11 @RequestMapping("/selectOneByPhone/{phone}") 12 TableOne selectOneByPhone(@PathVariable("phone") String phone) ; 13}

2、8002 服务提供一个对外服务

基于Feign的调用方式 作用:基于三台分库分表的数据存储接口。

1import org.springframework.cloud.netflix.feign.FeignClient; 2import org.springframework.web.bind.annotation.RequestBody; 3import org.springframework.web.bind.annotation.RequestMapping; 4import shard.jdbc.common.entity.TableOne; 5 6/** 7 * 数据迁移服务接口 8 */ 9@FeignClient(value = "shard-provider-8002") 10public interface MoveDataService { 11 @RequestMapping("/moveData") 12 Integer moveData (@RequestBody TableOne tableOne) ; 13}

3、基于8002服务数据查询接口

查询流程图

代码块

1/** 2 * 8001 端口 :基于两台分库分表策略的数据查询接口 3 */ 4@Resource 5private TwoOpenService twoOpenService ; 6@Override 7public TableOne selectOneByPhone(String phone) { 8 TableOne tableOne = tableOneMapper.selectOneByPhone(phone); 9 if (tableOne != null){ 10 LOG.info("8002 === >> tableOne :"+tableOne); 11 } 12 // 8002 服务没有查到数据 13 if (tableOne == null){ 14 // 调用 8001 开放的查询接口 15 tableOne = twoOpenService.selectOneByPhone(phone) ; 16 LOG.info("8001 === >> tableOne :"+tableOne); 17 } 18 return tableOne ; 19}

4、基于 8001 数据扫描迁移代码

迁移流程图

代码块

1/** 2 * 8002 端口开放的数据入库接口 3 */ 4@Resource 5private MoveDataService moveDataService ; 6/** 7 * 扫描,并迁移数据 8 * 以 库 db_2 的 table_one_1 表为例 9 */ 10@Override 11public void scanDataRun() { 12 String sql = "SELECT id,phone,back_one backOne,back_two backTwo,back_three backThree FROM table_one_1" ; 13 // dataTwoTemplate 对应的数据库:ds_2 14 List<TableOne> tableOneList = dataTwoTemplate.query(sql,new Object[]{},new BeanPropertyRowMapper<>(TableOne.class)) ; 15 if (tableOneList != null && tableOneList.size()>0){ 16 int i = 0 ; 17 for (TableOne tableOne : tableOneList) { 18 String db_num = HashUtil.moveDb(tableOne.getPhone()) ; 19 String tb_num = HashUtil.moveTable(tableOne.getPhone()) ; 20 // 只演示向数据新加库 ds_4 迁移的数据 21 if (db_num.equals("ds_4")){ 22 i += 1 ; 23 LOG.info("迁移总数数=>" + i + "=>库位置=>"+db_num+"=>表位置=>"+tb_num+"=>数据:【"+tableOne+"】"); 24 // 扫描完成:执行新库迁移和旧库清理过程 25 moveDataService.moveData(tableOne) ; 26 // dataTwoTemplate.update("DELETE FROM table_one_1 WHERE id=? AND phone=?",tableOne.getId(),tableOne.getPhone()); 27 } 28 } 29 } 30}

三、演示执行流程

1、项目流程图

2、测试执行流程

(1)、访问8002 数据查询端口

1http://127.0.0.1:8002/selectOneByPhone/phone20 2日志输出: 38001 服务查询到数据 48001 === >> tableOne :+{tableOne}

(2)、执行8001 数据扫描迁移

http://127.0.0.1:8001/scanData

(3)、再次访问8002 数据查询端口

1http://127.0.0.1:8002/selectOneByPhone/phone20 2日志输出: 38002 服务查询到数据 48002 === >> tableOne :+{tableOne}

四、源代码地址

1GitHub·地址 2https://github.com/cicadasmile/spring-cloud-base 3GitEE·地址 4https://gitee.com/cicadasmile/spring-cloud-base

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Linux系统:Centos7下搭建ClickHouse列式存储数据库

本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Flinuxsystembase)||GitEE·点这里(https://gitee.com/cicadasmile/linuxsystem

SpringCloud微服务:阿里开源组件Nacos,服务和配置管理

源码地址:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Fspringcloudbase)||GitEE·点这里(https://gitee.com/cicadasmile/springcloudba

SpringCloud微服务:基于Nacos组件,整合Dubbo框架

源码地址:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Fspringcloudbase)||GitEE·点这里(https://gitee.com/cicadasmile/springcloud

SpringBoot2 整合Ehcache组件,轻量级缓存管理

本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Fmiddlewareparent)||GitEE·点这里(https://gitee.com/cicadasmile/middleware

SpringCloud微服务(07):Zipkin组件,实现请求链路追踪

本文源码:GitHub·点这里(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fcicadasmile%2Fspringcloudbase)||GitEE·点这里(https://gitee.com/cicadasmile/springcloud