這篇文章主要講解了“ShardingSphere5.0.0-alpha如何實現(xiàn)MySQL分庫分表”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ShardingSphere5.0.0-alpha如何實現(xiàn)mysql分庫分表”吧!
高昌網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,高昌網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為高昌上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的高昌做網(wǎng)站的公司定做!
本文會基于 Springboot + mybatis + shardingsphere + mysql5.6 + druid 進行實戰(zhàn)講解
本文在上一篇文章[數(shù)據(jù)分表]的基礎(chǔ)上增加了 分庫的功能
本文不會介紹 shardingsphere 以及分庫分表的相關(guān)概念
本文采用的 shardingsphere 版本是 5.0.0-alpha, 具體見 pom 文件
本文涉及的源碼請參考 分庫
如果看官方文檔時, 請選擇對應(yīng)的版本 !!!
文中涉及的源碼可能會有誤, 請以上傳到 gitee 的源碼為準.
我們有兩個數(shù)據(jù)庫 miaosha2 和 miaosha3, 每個數(shù)據(jù)庫中都有 2 張被拆分過的用戶表 user_info0 和 user_info1
當(dāng)我們往用戶表插數(shù)據(jù)時, 會按照一定的規(guī)則(根據(jù)自增id取模), 落到某個 miaosha 庫中的某張 user_info 表中.
create database miaosha2; DROP TABLE IF EXISTS `miaosha2`.`user_info0`; CREATE TABLE `miaosha2`.`user_info0` ( `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 AUTO_INCREMENT = 7 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; DROP TABLE IF EXISTS `miaosha2`.`user_info1`; CREATE TABLE `miaosha2`.`user_info1` ( `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 AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; create database miaosha3; DROP TABLE IF EXISTS `miaosha3`.`user_info0`; CREATE TABLE `miaosha3`.`user_info0` ( `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 AUTO_INCREMENT = 7 DEFAULT CHARSET = utf8 COLLATE = utf8_bin; DROP TABLE IF EXISTS `miaosha3`.`user_info1`; CREATE TABLE `miaosha3`.`user_info1` ( `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 AUTO_INCREMENT = 6 DEFAULT CHARSET = utf8 COLLATE = utf8_bin;
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.nimo shardingsphere-demo 0.0.1-SNAPSHOT shardingsphere-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
再次強調(diào)下, 本文采用的 shardingsphere 版本是 5.0.0-alpha. 不同版本配置會有差異.
本文在上一篇文章的基礎(chǔ)上增加, 并修改了幾個配置, 下面的源碼中有標記出來
添加了一個數(shù)據(jù)源配置
添加了一個分庫策略
添加了一個分庫算法
server: port: 8777 spring: shardingsphere: # 展示修改以后的sql語句 props: sql-show: true datasource: # (這里增加了一個 ds1 的數(shù)據(jù)源) names: ds0,ds1 common: type: com.alibaba.druid.pool.DruidDataSource ds0: url: jdbc:mysql://127.0.0.1:3306/miaosha2?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:3306/miaosha3?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8 username: root password: '123456' driver-class-name: com.mysql.cj.jdbc.Driver rules: sharding: # 分布式序列算法配置 key-generators: # 此處必須要配置,否則會導(dǎo)致報錯 snowflake: type: SNOWFLAKE props: worker-id: 123 # 配置 user_info 表 tables: user_info: # 分庫策略 (新增的配置) database-strategy: standard: sharding-column: id sharding-algorithm-name: database-inline # 配置user_info的分庫分表的規(guī)則 (增加了數(shù)據(jù)源的配置) actual-data-nodes: ds$->{0..1}.user_info$->{0..1} # 單分片鍵的標準分片 table-strategy: standard: sharding-column: id sharding-algorithm-name: table-inline # 主鍵id生成策略(雪花算法) key-generate-strategy: key-generator-name: snowflake column: id # 配置分片算法 sharding-algorithms: # 通過 id 取模的方式確定數(shù)據(jù)落到哪個庫 (新增的配置) database-inline: type: INLINE props: algorithm-expression: ds$->{id % 2} # 通過 id 取模的方式確定數(shù)據(jù)落到哪個表 table-inline: type: INLINE props: algorithm-expression: user_info$->{id % 2} enabled: true mybatis: typeAliasesPackage: com.nimo.shardingdatabasedemo.entity mapperLocations: classpath:mapper/*.xml
// sqlinsert into user_info(id, username, password) values (#{id}, #{username}, #{password}) // 新增一個用戶信息 @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\" }"
感謝各位的閱讀,以上就是“ShardingSphere5.0.0-alpha如何實現(xiàn)mysql分庫分表”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ShardingSphere5.0.0-alpha如何實現(xiàn)mysql分庫分表這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!