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


ShardingSphere實現分庫分表+讀寫分離

有關分庫分表前面寫了六篇博客:

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

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

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

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

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

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

這篇博客通過ShardingSphere實現分庫分表 + 讀寫分離,並在文章最下方附上項目Github地址

一、項目概述

1、技術架構

項目總體技術選型

SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4  + MySQL + lombok(插件)

2、項目說明

場景 在實際開發中,如果數據庫壓力大我們可以通過 分庫分表 的基礎上進行 讀寫分離,來減緩數據庫壓力。

3、數據庫設計

分庫 ms單庫分庫分為 ms0庫 和 ms1庫。

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

讀寫分離 數據寫入ms0庫 和 ms1庫,數據讀取 sl0庫 和 sl1庫。

如圖

ms0 ---主庫

ms1 ---主庫

sl0 ---從庫

sl1 ---從庫

說明 初始數據的時候,這邊只有 sl0從庫 我插入了一條數據。那是因為我們這個項目中Mysql服務器並沒有實現主從部署,這四個庫都在同一服務器上,所以

做不到主數據庫數據自動同步到從數據庫。所以這里在從數據庫建一條數據。等下驗證的時候,我們只需驗證數據是否存入ms0ms1,數據讀取是否在sl0sl1

具體的創建表SQL也會放到GitHub項目里


二、核心代碼

說明 完整的代碼會放到GitHub上,這里只放一些核心代碼。

1、application.properties

server.port=8082

#指定mybatis信息
mybatis.config-location=classpath:mybatis-config.xml
#打印sql
spring.shardingsphere.props.sql.show=true
#數據源 
spring.shardingsphere.datasource.names=master0,slave0,master1,slave1

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/ms0?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=root

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/sl0?characterEncoding=utf-8
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=root

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

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

#根據年齡分庫
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=master$->{age % 2}
#根據id分表
spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master$->{0..1}.tab_user$->{0..1}
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 2}

#指定master0為主庫,slave0為它的從庫
spring.shardingsphere.sharding.master-slave-rules.master0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.master0.slave-data-source-names=slave0
#指定master1為主庫,slave1為它的從庫
spring.shardingsphere.sharding.master-slave-rules.master1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.master1.slave-data-source-names=slave1

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:8082/save-user

我們可以從商品接口代碼中可以看出,它會批量插入5條數據。我們先看控制台輸出SQL語句

我們可以從SQL語句可以看出 master0master1 庫中都插入了數據。

我們再來看數據庫

ms0.tab_user0

ms0.tab_user1

ms1.tab_user0

ms1.tab_user1

完成分庫分表插入數據。

2、獲取數據

這里獲取列表接口的SQL。

  select *  from tab_user 

請求接口結果

結論 從接口返回的結果可以很明顯的看出,數據存儲在主庫,而數據庫的讀取在從庫。

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

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


參考

1、ShardingSphere中文文檔

2、ShardingSphere官網

3、Shardingsphere Github庫



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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM