SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

This article Source: GitHub · Click here || GitEE · Click here

First, the project structure

1, engineering structures

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

2, module name

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

3, dependent on the code structure

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

4, project start-up sequence

(1)shard-eureka-7001:        注册中心
(2)shard-two-provider-8001:  8001 基于两台库的服务
(3)shard-three-provider-8002:8002 基于三台库的服务

In order to start and complete service after waiting for a start, a start in the service, otherwise you might encounter some pit.

Second, the core block

1,8001 provide a service external services

Called Feign based
action: Based on the data of two sub-library sub-table query interface.

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 provide a service external services

Called Feign based
role: data storage interfaces of the three sub-libraries based on the score sheet.

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, based on 8002 data service query interface

Queries flow chart

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

Block

/**
 * 8001 端口 :基于两台分库分表策略的数据查询接口
 */
@Resource
private TwoOpenService twoOpenService ;
@Override
public 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, based on the scan data migration code 8001

Migration flow chart

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

Block

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

Third, demonstrate the implementation process

1, a flow chart of the project

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

2, the test execution flow

(1), 8002 data query access port

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

(2), performs scan data migration 8001

http://127.0.0.1:8001/scanData

(3) again to access data query port 8002

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

Fourth, the source address

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

SpringCloud achieve ShardJdbc sub-library sub-table mode, the database expansion program

Guess you like

Origin blog.51cto.com/14439672/2443993