温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-02-25 18:43:13 来源:网络 阅读:249 作者:艾弗森哇 栏目:数据库

一、项目结构

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的调用方式 作用:基于两台分库分表的数据查询接口。郑州治疗不孕不育哪里好: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
 * 对外开放接口
 */@FeignClient(value = "shard-provider-8001")
public interface TwoOpenService {
    @RequestMapping("/selectOneByPhone/{phone}")    TableOne selectOneByPhone(@PathVariable("phone") String phone) ;
}

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

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

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 端口 :基于两台分库分表策略的数据查询接口
 */@Resourceprivate TwoOpenService twoOpenService ;@Overridepublic 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实现分库分表模式下,数据库实时扩容方

代码块

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

/**
 * 8002 端口开放的数据入库接口
 */@Resourceprivate MoveDataService moveDataService ;/**
 * 扫描,并迁移数据
 * 以 库 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 对应的数据库: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());
            }
        }
    }
}

三、演示执行流程

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

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}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI