聲明
- 本文會基於
Springboot + mybatis + shardingsphere + mysql5.6 + druid
進行實戰講解 - 本文的實戰內容爲根據前文搭建的 mysql 主從複製架構 實現 mysql 的讀寫分離
- 本文的內容只涉及 讀寫分離, 並沒有跟前面文章中的分庫分表的內容串起來.
- 本文采用的 shardingsphere 版本是 5.0.0-alpha, 具體見 pom 文件
- 本文涉及的源碼請參考 讀寫分離
- 如果看官方文檔時, 請選擇對應的版本, 並且官網也有坑 !!!
- 文中貼出的源碼可能會有誤, 請以上傳到 gitee 的源碼爲準.
正文
需求
在前文搭建的 mysql 主從複製 (一主兩從)基礎上, 我們分別在三個 mysql 上創建了 1 個名爲 miaosha
的數據庫, 每個數據庫中都有 1 張用戶表 user_info.
當我們插入數據時, 數據會先插入到主庫, 然後會同步到兩個從庫上, 當我們查詢數據時, 只會去兩個從庫上查詢.
準備工作
1. 數據庫表
create database miaosha;
DROP TABLE IF EXISTS `miaosha`.`user_info`;
CREATE TABLE `miaosha`.`user_info`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`username` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`phone` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`password` varchar(128) COLLATE utf8_bin NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8
COLLATE = utf8_bin;
2. pom 依賴
跟前面數據分庫分表的配置一樣.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.nimo</groupId>
<artifactId>read-write-splitting-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>read-write-splitting-demo</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>
<!-- 阿里數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml
再次強調下, 本文采用的 shardingsphere 版本是 5.0.0-alpha. 不同版本配置會有差異. 這裏先貼出能使用的配置, 後面會對官方提供的配置進行吐槽!!!
server:
port: 8777
spring:
shardingsphere:
props:
sql-show: true
# 配置 3 個數據源
datasource:
names: ds0,ds1,ds2
common:
type: com.alibaba.druid.pool.DruidDataSource
ds0:
url: jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
username: root
password: '123456'
driver-class-name: com.mysql.cj.jdbc.Driver
ds1:
url: jdbc:mysql://127.0.0.1:3340/miaosha?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
username: root
password: '123456'
driver-class-name: com.mysql.cj.jdbc.Driver
ds2:
url: jdbc:mysql://127.0.0.1:3341/miaosha?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
username: root
password: '123456'
driver-class-name: com.mysql.cj.jdbc.Driver
rules:
replica-query:
load-balancers:
# 負載均衡算法
round-robin:
type: ROUND_ROBIN
# 這裏是最神經病的地方, 不配置就報錯! 配置吧又不知道配置什麼
props:
# 你也可以配置 xxx: 123, yyy: 4342 但是必須得有一個屬性, 隨便編
default: 0
data-sources:
# 這個名字就隨便起
prds:
# 主庫
primary-data-source-name: ds0
# 從庫
replica-data-source-names: ds1,ds2
load-balancer-name: round_robin
# enabled: true
mybatis:
typeAliasesPackage: com.nimo.readwritesplitting.entity
mapperLocations: classpath:mapper/*.xml
4. 主要代碼
// sql
<insert id="addUser" parameterType="com.nimo.shardingdemo.entity.UserInfo">
insert into user_info(id, username, password) values (#{id}, #{username}, #{password})
</insert>
// 新增一個用戶信息
@PostMapping("userinfo")
public Object addUserInfo(@RequestBody UserInfo userInfo) {
return userInfoMapper.addUser(userInfo);
}
5. 測試命令
# 插入用戶
curl -X POST --location "http://localhost:8777/userinfo" \
-H "Content-Type: application/json" \
-d "{
\"username\": \"wangbadan\",
\"password\": \"123456\"
}"
# 查詢 id 爲 7 的用戶
curl -X GET http://localhost:8777/userinfo/7
下面是發起查詢兩次請求後, 打印出來的日誌(有刪減).
對官網的吐槽
官方5.x 版本對 讀寫分離 的名稱進行了修改.
原先叫 read-write-splitting
, 後來改爲了 replica-query
, 所以讀寫分離的配置也發生了變化
# 官方文檔 5.x
spring.shardingsphere.rules.readwrite-splitting.xxxxxxxxxx
# 官網 demo 5.0.0-alpha
spring.shardingsphere.rules.replica-query.xxxxxxxxxx=
但是 官網文檔關於讀寫分離的那一章節, 並沒有做修改, 還是用的 read-write-splitting
, 它只是另起了一個章節, 對名稱變更做了說明.
詳細介紹請參考
先看下官方最新 5.x 文檔
可以看到 讀寫分離配置章節 涉及到讀寫分離的配置還是用的 readwrite-splitting
比如 spring.shardingsphere.rules.readwrite-splitting.xxxxxxxxxx
spring.shardingsphere.datasource.names=
spring.shardingsphere.rules.readwrite-splitting.data-sources.<readwrite-splitting-data-source-name>.primary-data-source-name=
spring.shardingsphere.rules.readwrite-splitting.data-sources.<readwrite-splitting-data-source-name>.replica-data-source-names=
spring.shardingsphere.rules.readwrite-splitting.data-sources.<readwrite-splitting-data-source-name>.load-balancer-name=
# Load balance algorithm configuration
spring.shardingsphere.rules.readwrite-splitting.load-balancers.<load-balance-algorithm-name>.type=
spring.shardingsphere.rules.readwrite-splitting.load-balancers.<load-balance-algorithm-name>.props.xxx=
再來看下官網提供的 demo
這裏還有一個需要注意的地方, 在看官網 demo 時, 需要選擇下對應的分支/標籤.
官網 demo 提供的 讀寫分離配置 用的是 replica-query
字段, 比如: spring.shardingsphere.rules.replica-query.xxxxxxxxxx=
官網 demo 提供的配置
spring.shardingsphere.datasource.names=primary_ds,replica_ds_0,replica_ds_1
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=root
spring.shardingsphere.datasource.common.password=
# 主數據源 primary_ds
spring.shardingsphere.datasource.primary_ds.jdbc-url=jdbc:mysql://localhost:3306/demo_primary_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
# 從數據源 replica_ds_0
spring.shardingsphere.datasource.replica_ds_0.jdbc-url=jdbc:mysql://localhost:3306/demo_replica_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
# 從數據源 replica_ds_1
spring.shardingsphere.datasource.replica_ds_1.jdbc-url=jdbc:mysql://localhost:3306/demo_replica_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
# 負載均衡算法
spring.shardingsphere.rules.replica-query.load-balancers.round_robin.type=ROUND_ROBIN
# 主庫
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=primary_ds
# 從庫
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=replica_ds_0,replica_ds_1
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round_robin
最可怕的是不管是使用官網提供的 demo, 還是按照官網文檔提供的配置去配置, 項目都運行不起來.
總結
本文基於ShardingSphere5.0.0-alpha
實現了 mysql 讀寫分離, 在此之前, 還需要搭建一套 mysql 的主從複製架構.
另外, 官方 5.x 版本對 讀寫分離 的配置進行了調整, 由 readwrite-splitting
改爲了 replica-query
.
最後就是以下的配置不可少. props
下的配置可以隨意配置. 比如 xxx=yyy, abc=123;
當然原因還沒有搞清楚, 後面有時間會研究一下.
spring.shardingsphere.rules.readwrite-splitting.load-balancers.<load-balance-algorithm-name>.props.xxx= # 負載均衡算法屬性配置
下一篇文章會將 分庫分表 與 讀寫分離 的配置進行整合.