您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何用ShardingSphere5.0.0-alpha實現mysql讀寫分離”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何用ShardingSphere5.0.0-alpha實現mysql讀寫分離”吧!
在前文搭建的 mysql 主從復制 (一主兩從)基礎上, 我們分別在三個 mysql 上創建了 1 個名為 miaosha
的數據庫, 每個數據庫中都有 1 張用戶表 user_info.
當我們插入數據時, 數據會先插入到主庫, 然后會同步到兩個從庫上, 當我們查詢數據時, 只會去兩個從庫上查詢.
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;
跟前面數據分庫分表的配置一樣.
<?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>
再次強調下, 本文采用的 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
// 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); }
# 插入用戶 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
, 它只是另起了一個章節, 對名稱變更做了說明.
詳細介紹請參考
github
官方文檔的變更歷史
可以看到 讀寫分離配置章節 涉及到讀寫分離的配置還是用的 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 提供的 讀寫分離配置 用的是 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= # 負載均衡算法屬性配置
感謝各位的閱讀,以上就是“如何用ShardingSphere5.0.0-alpha實現mysql讀寫分離”的內容了,經過本文的學習后,相信大家對如何用ShardingSphere5.0.0-alpha實現mysql讀寫分離這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。