SpringCloud 기반으로shard-Jdbc의 라이브러리 테이블 모드를 실현하고 데이터베이스 확장 방안

4346 단어 시스템 구조

1. 프로젝트 구조


1. 공사 구조


2. 모듈 이름 지정

shard-common-entity:        
shard-open-inte:              
shard-eureka-7001:          
shard-two-provider-8001: 8001         
shard-three-provider-8002:8002         

3. 코드 의존 구조


4. 프로젝트 시작 순서

(1)shard-eureka-7001:            
(2)shard-two-provider-8001:  8001         
(3)shard-three-provider-8002:8002         

순서대로 시작하고 서비스가 완전히 시작되면 다음 서비스가 시작됩니다. 그렇지 않으면 구덩이가 생길 수 있습니다.

2. 핵심 코드 블록


1. 8001 서비스는 대외 서비스를 제공한다.


Feign 기반 호출 방식 역할: 두 개의 라이브러리 테이블 기반 데이터 조회 인터페이스
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 서비스 데이터 조회 인터페이스 기반


조회 흐름도
코드 블록
/**
 * 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. 8001 데이터 기반 마이그레이션 코드 스캔


마이그레이션 순서도
코드 블록
/**
 * 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 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());
            }
        }
    }
}

3. 실행 절차를 시범적으로 보여준다.


1. 프로젝트 흐름도


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}

4. 소스 코드 주소

GitHub  :    
https://github.com/cicadasmile
    :    
https://gitee.com/cicadasmile

좋은 웹페이지 즐겨찾기