1. 程式人生 > >分庫分表(6)--- SpringBoot+ShardingSphere實現分表+ 讀寫分離

分庫分表(6)--- SpringBoot+ShardingSphere實現分表+ 讀寫分離

分庫分表(6)--- ShardingSphere實現分表+ 讀寫分離

有關分庫分表前面寫了五篇部落格:

1、分庫分表(1) --- 理論

2、分庫分表(2) --- ShardingSphere(理論)

3、分庫分表(3) ---SpringBoot + ShardingSphere實現讀寫分離

4、分庫分表(4) ---SpringBoot + ShardingSphere 實現分表

5、分庫分表(5) ---SpringBoot + ShardingSphere 實現分庫分表

這篇部落格通過ShardingSphere實現分表不分庫+讀寫分離,並在文章最下方附上專案Github地址

一、專案概述

1、技術架構

專案總體技術選型

SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4  + MySQL + lombok(外掛)

2、專案說明

場景 在實際開發中,如果表的資料過大,我們可能需要把一張表拆分成多張表,這裡就是通過ShardingSphere實現分表+讀寫分離功能,但不分庫。

3、資料庫設計

分表 tab_user單表拆分為tab_user0表 和 tab_user1表。

讀寫分離 資料寫入master庫 ,資料讀取 slave庫 。

如圖

master庫

slave庫

說明 初始資料的時候,這邊只有 slave從庫的tab_user0 我插入了一條資料。那是因為我們這個專案中Mysql伺服器並沒有實現主從部署,這兩個庫都在同一伺服器上,所以

做不到主資料庫資料自動同步到從資料庫。所以這裡在從資料庫建一條資料。等下驗證的時候,我們只需驗證資料是否存入master庫,資料讀取是否在slave庫

具體的建立表SQL也會放到GitHub專案裡


二、核心程式碼

說明 完整的程式碼會放到GitHub上,這裡只放一些核心程式碼。

1、application.properties

server.port=8084

#指定mybatis資訊
mybatis.config-location=classpath:mybatis-config.xml

#資料庫
spring.shardingsphere.datasource.names=master0,slave0

spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/master?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456

spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://localhost:3306/slave?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=root

#資料分表規則
#指定所需分的表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master0.tab_user$->{0..1}
#指定主鍵
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id
#分表規則為主鍵除以2取模
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 2}

# 讀寫分離
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=ms
#這裡配置讀寫分離的時候一定要記得新增主庫的資料來源名稱 這裡為master0
spring.shardingsphere.sharding.master-slave-rules.master0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.master0.slave-data-source-names=slave0

#列印sql
spring.shardingsphere.props.sql.show=true

Sharding-JDBC可以通過JavaYAMLSpring名稱空間Spring Boot Starter四種方式配置,開發者可根據場景選擇適合的配置方式。具體可以看官網。

2、UserController

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 模擬插入資料
     */
    List<User> userList = Lists.newArrayList();
    /**
     * 初始化插入資料
     */
    @PostConstruct
    private void getData() {
        userList.add(new User(1L,"小小", "女", 3));
        userList.add(new User(2L,"爸爸", "男", 30));
        userList.add(new User(3L,"媽媽", "女", 28));
        userList.add(new User(4L,"爺爺", "男", 64));
        userList.add(new User(5L,"奶奶", "女", 62));
    }
    /**
     * @Description: 批量儲存使用者
     */
    @PostMapping("save-user")
    public Object saveUser() {
        return userService.insertForeach(userList);
    }
    /**
     * @Description: 獲取使用者列表
     */
    @GetMapping("list-user")
    public Object listUser() {
        return userService.list();
    }


三、測試驗證

1、批量插入資料

請求介面

localhost:8084/save-user

我們可以從商品介面程式碼中可以看出,它會批量插入5條資料。我們先看控制檯輸出SQL語句

我們可以從SQL語句可以看出 master0資料來源 中 tab_user0 表插入了三條資料,而 tab_user1 表中插入兩條資料

我們再來看資料庫

master.tab_user0

master.tab_user1

完成分表插入資料。

2、獲取資料

我們來獲取列表介面的SQL。

  select *  from tab_user 

請求介面結果

結論 從介面返回的結果可以很明顯的看出,資料儲存在master主庫,而資料庫的讀取在slave從庫。

注意 ShardingSphere並不支援CASE WHENHAVINGUNION (ALL)有限支援子查詢。這個官網有詳細說明。

Github地址:https://github.com/yudiandemingzi/spring-boot-sharding-sphere


參考

1、ShardingSphere中文文件

2、ShardingSphere官網

3、Shardingsphere Github庫




 我相信,無論今後的道路多麼坎坷,只要抓住今天,遲早會在奮鬥中嚐到人生的甘甜。抓住人生中的一分一秒,勝過虛度中的一月一年!(20)

<