這篇文章主要講解了“如何用ShardingSphere5.0.0-alpha實(shí)現(xiàn)MySQL讀寫(xiě)分離”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql讀寫(xiě)分離”吧!
目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、準(zhǔn)格爾網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
在前文搭建的 mysql 主從復(fù)制 (一主兩從)基礎(chǔ)上, 我們分別在三個(gè) mysql 上創(chuàng)建了 1 個(gè)名為 miaosha
的數(shù)據(jù)庫(kù), 每個(gè)數(shù)據(jù)庫(kù)中都有 1 張用戶表 user_info.
當(dāng)我們插入數(shù)據(jù)時(shí), 數(shù)據(jù)會(huì)先插入到主庫(kù), 然后會(huì)同步到兩個(gè)從庫(kù)上, 當(dāng)我們查詢數(shù)據(jù)時(shí), 只會(huì)去兩個(gè)從庫(kù)上查詢.
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;
跟前面數(shù)據(jù)分庫(kù)分表的配置一樣.
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.nimo read-write-splitting-demo 0.0.1-SNAPSHOT read-write-splitting-demo 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.apache.shardingsphere shardingsphere-jdbc-core-spring-boot-starter 5.0.0-alpha com.alibaba druid 1.2.3 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
再次強(qiáng)調(diào)下, 本文采用的 shardingsphere 版本是 5.0.0-alpha. 不同版本配置會(huì)有差異. 這里先貼出能使用的配置, 后面會(huì)對(duì)官方提供的配置進(jìn)行吐槽!!!
server: port: 8777 spring: shardingsphere: props: sql-show: true # 配置 3 個(gè)數(shù)據(jù)源 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: # 負(fù)載均衡算法 round-robin: type: ROUND_ROBIN # 這里是最神經(jīng)病的地方, 不配置就報(bào)錯(cuò)! 配置吧又不知道配置什么 props: # 你也可以配置 xxx: 123, yyy: 4342 但是必須得有一個(gè)屬性, 隨便編 default: 0 data-sources: # 這個(gè)名字就隨便起 prds: # 主庫(kù) primary-data-source-name: ds0 # 從庫(kù) replica-data-source-names: ds1,ds2 load-balancer-name: round_robin # enabled: true mybatis: typeAliasesPackage: com.nimo.readwritesplitting.entity mapperLocations: classpath:mapper/*.xml
// sqlinsert into user_info(id, username, password) values (#{id}, #{username}, #{password}) // 新增一個(gè)用戶信息 @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
下面是發(fā)起查詢兩次請(qǐng)求后, 打印出來(lái)的日志(有刪減).
官方5.x 版本對(duì) 讀寫(xiě)分離的名稱進(jìn)行了修改.
原先叫 read-write-splitting
, 后來(lái)改為了 replica-query
, 所以讀寫(xiě)分離的配置也發(fā)生了變化
# 官方文檔 5.x spring.shardingsphere.rules.readwrite-splitting.xxxxxxxxxx # 官網(wǎng) demo 5.0.0-alpha spring.shardingsphere.rules.replica-query.xxxxxxxxxx=
但是 官網(wǎng)文檔關(guān)于讀寫(xiě)分離的那一章節(jié), 并沒(méi)有做修改, 還是用的 read-write-splitting
, 它只是另起了一個(gè)章節(jié), 對(duì)名稱變更做了說(shuō)明.
詳細(xì)介紹請(qǐng)參考
github
官方文檔的變更歷史
可以看到 讀寫(xiě)分離配置章節(jié)涉及到讀寫(xiě)分離的配置還是用的 readwrite-splitting
比如 spring.shardingsphere.rules.readwrite-splitting.xxxxxxxxxx
spring.shardingsphere.datasource.names= spring.shardingsphere.rules.readwrite-splitting.data-sources..primary-data-source-name= spring.shardingsphere.rules.readwrite-splitting.data-sources. .replica-data-source-names= spring.shardingsphere.rules.readwrite-splitting.data-sources. .load-balancer-name= # Load balance algorithm configuration spring.shardingsphere.rules.readwrite-splitting.load-balancers. .type= spring.shardingsphere.rules.readwrite-splitting.load-balancers. .props.xxx=
這里還有一個(gè)需要注意的地方, 在看官網(wǎng) demo 時(shí), 需要選擇下對(duì)應(yīng)的分支/標(biāo)簽.
官網(wǎng) demo 提供的 讀寫(xiě)分離配置用的是 replica-query
字段, 比如: spring.shardingsphere.rules.replica-query.xxxxxxxxxx=
官網(wǎng) 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= # 主數(shù)據(jù)源 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 # 從數(shù)據(jù)源 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 # 從數(shù)據(jù)源 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 # 負(fù)載均衡算法 spring.shardingsphere.rules.replica-query.load-balancers.round_robin.type=ROUND_ROBIN # 主庫(kù) spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=primary_ds # 從庫(kù) 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
最可怕的是不管是使用官網(wǎng)提供的 demo, 還是按照官網(wǎng)文檔提供的配置去配置, 項(xiàng)目都運(yùn)行不起來(lái).
本文基于ShardingSphere5.0.0-alpha
實(shí)現(xiàn)了 mysql 讀寫(xiě)分離, 在此之前, 還需要搭建一套 mysql 的主從復(fù)制架構(gòu).
另外, 官方 5.x 版本對(duì) 讀寫(xiě)分離的配置進(jìn)行了調(diào)整, 由 readwrite-splitting
改為了 replica-query
.
最后就是以下的配置不可少. props
下的配置可以隨意配置. 比如 xxx=yyy, abc=123;
當(dāng)然原因還沒(méi)有搞清楚, 后面有時(shí)間會(huì)研究一下.
spring.shardingsphere.rules.readwrite-splitting.load-balancers..props.xxx= # 負(fù)載均衡算法屬性配置
感謝各位的閱讀,以上就是“如何用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql讀寫(xiě)分離”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql讀寫(xiě)分離這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!