1. 程式人生 > 其它 >如何使用sharding-jdbc完成讀寫分離?

如何使用sharding-jdbc完成讀寫分離?

1.先搭建好一個MySQL的主從叢集,可以參考MySQL高階

2.在專案中匯入相關依賴(記得重新整理Maven)

<!--讀寫分離-->
<dependency>
	<groupId>org.apache.shardingsphere</groupId>
	<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
	<version>5.1.0</version>
</dependency>

3.編寫一個application-sharding.yml配置檔案,可以參考
官方文件
.但是推薦參考最新版本的,不然很多坑.當然也可以使用下面配置好的,親測可用!!!

spring:
  shardingsphere:
    datasource:
      names: master1,slave1,slave2  # 指定所有資料來源的名字
      master1:
        type: com.zaxxer.hikari.HikariDataSource # 資料來源型別
        url: jdbc:mysql://192.168.137.137:3306/qmall_product?useSSL=false # 資料庫連線地址
        username: root # 使用者名稱
        password: root # 密碼
        driver-class-name: com.mysql.jdbc.Driver # 資料庫驅動

      slave1:
        type: com.zaxxer.hikari.HikariDataSource # 資料來源型別
        url: jdbc:mysql://192.168.137.137:3307/qmall_product?useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver

      slave2:
        type: com.zaxxer.hikari.HikariDataSource # 資料來源型別
        url: jdbc:mysql://192.168.137.137:3308/qmall_product?useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver
    rules:
      readwrite-splitting:  # 配置讀寫分離規則
        data-sources:
          ds_0:   # 給一套叢集起個名
            type: static
            props:
              auto-aware-data-source-name: master1
              write-data-source-name: master1
              read-data-source-names: slave1,slave2
            load-balancer-name: read-random
        load-balancers:
          read-random:
            type: ROUND_ROBIN # 輪詢負載均衡
    props:
      sql-show: true # 是否列印sql
      sql-simple: true # 列印簡單的sql
  • 寫完上面的配置檔案別忘了在application.yml中啟用引入一下
spring:
  profiles:
    include: sharding # 引入application-sharding.yml

4.編寫測試程式碼

package com.qbb.qmall;

import com.qbb.qmall.model.product.BaseCategory1;
import com.qbb.qmall.product.ProductApplication;
import com.qbb.qmall.product.mapper.BaseCategoryMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author QiuQiu&LL (個人部落格:https://www.cnblogs.com/qbbit)
 * @version 1.0
 * @date 2022-05-29  15:48
 * @Description:
 */
@SpringBootTest(classes = ProductApplication.class)
public class ShardingTest {

    @Autowired
    private BaseCategoryMapper baseCategoryMapper;

    @Test
    public void write() {
        BaseCategory1 baseCategory1 = new BaseCategory1();
        baseCategory1.setName("qiuqiu");
        baseCategoryMapper.insert(baseCategory1);
    }
}
  • 寫資料是操作的master1庫

  • 下面測試一下讀資料

 @Test
public void read() {
	BaseCategory1 one = baseCategoryMapper.selectById(18);
	System.out.println("one = " + one);
	BaseCategory1 two = baseCategoryMapper.selectById(18);
	System.out.println("two = " + two);
	BaseCategory1 three = baseCategoryMapper.selectById(18);
	System.out.println("three = " + three);
	BaseCategory1 four = baseCategoryMapper.selectById(18);
	System.out.println("four = " + four);
}
  • 因為我們上面配置的是輪詢的負載均衡策略,所以是如上效果