mysql--使用shardingsphere實現分庫分表
阿新 • • 發佈:2021-10-09
一.簡介
本節將繼續通過ShardingSphere實現分庫分表
上一節的分表中已經介紹過了使用的分表策略,接下來介紹一下分庫策略
ShardingSphere提供如下四種分庫策略:
1.standard: #用於單分片鍵的標準分片場景
2.complex: #用於多分片鍵的複合分片場景
3.inline: #行表示式分片策略
4. hint: #Hint分片策略
本篇文章使用的是inline行表示式分片策略
關於各種分片策略,可以到手冊進行檢視
二.資料庫設計
本篇文章使用兩個資料庫,每個資料庫兩張表進行簡單的演示。
ds0庫:
ds1庫於ds0兩個庫資料結構一致
三.編碼階段
1. 引入主要依賴
1 <dependency> 2 <groupId>org.mybatis.spring.boot</groupId> 3 <artifactId>mybatis-spring-boot-starter</artifactId> 4 <version>2.2.0</version> 5 </dependency> 6 <!--druid資料來源--> 7 <dependency> 8View Code<groupId>com.alibaba</groupId> 9 <artifactId>druid-spring-boot-starter</artifactId> 10 <version>1.2.1</version> 11 </dependency> 12 <!--shardingsphere--> 13 <dependency> 14 <groupId>org.apache.shardingsphere</groupId> 15<artifactId>sharding-jdbc-spring-boot-starter</artifactId> 16 <version>4.0.0-RC1</version> 17 </dependency> 18 <dependency> 19 <groupId>org.apache.shardingsphere</groupId> 20 <artifactId>sharding-jdbc-spring-namespace</artifactId> 21 <version>4.0.0-RC1</version> 22 </dependency> 23 <!--jdbc--> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>8.0.22</version> 28 </dependency>
2. 配置檔案
本篇文章資料庫分片策略中使用了inline行表示式分片策略
對age欄位進行了hash取模運算來進行分片;
資料表分片策略同分庫策略
分表需要指定表節點:
actualDataNodes: #由資料來源名 + 表名組成,以小數點分隔。多個表以逗號分隔,支援inline表示式。預設表示使用已知資料來源與邏輯表名稱生成資料節點,用於廣播表(即每個庫中都需要一個同樣的表用於關聯查詢,多為字典表)或只分庫不分表且所有庫的表結構完全一致的情況
1 server: 2 port: 8083 3 4 #mybatis資訊 5 mybatis: 6 mapper-locations: classpath:mapping/*Mapper.xml 7 type-aliases-package: com.example.demo.entity 8 9 Spring: 10 shardingsphere: 11 datasource: 12 names: ds0,ds1 #配置資料來源 分庫分表配置 13 #ds0庫 14 ds0: 15 type: com.alibaba.druid.pool.DruidDataSource 16 driver-class-name: com.mysql.cj.jdbc.Driver 17 url: jdbc:mysql://127.0.0.1:3306/ds0?serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true 18 username: root 19 password: 123456 20 #ds1庫 21 ds1: 22 type: com.alibaba.druid.pool.DruidDataSource 23 driver-class-name: com.mysql.cj.jdbc.Driver 24 url: jdbc:mysql://127.0.0.1:3306/ds1?serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true 25 username: root 26 password: 123456 27 sharding: 28 #預設資料庫分片策略 根據年齡分庫 29 default-database-strategy: 30 #行表示式分片策略 31 inline: 32 #分片欄位名稱 33 sharding-column: age 34 #分片演算法表示式 age 對 2 進行取模運算 35 algorithm-expression: ds$->{age % 2} 36 #資料表分片規則 37 tables: 38 #表名 39 tab_user: 40 #由資料來源名 + 表名組成,以小數點分隔。表節點 41 actual-data-nodes: ds$->{0..1}.tab_user$->{0..1} 42 #分表策略 43 table-strategy: 44 inline: 45 #通過id分表 46 sharding-column: id 47 #分片演算法表示式 48 algorithm-expression: tab_user$->{id % 2} 49 props: 50 sql: 51 show: true 52 swagger: 53 enable: trueView Code
3. 測試
1 /** 2 * 分庫分表測試 3 * @author Lv de yin 4 * @Date 2021/9/3 5 */ 6 @RequestMapping("pareDaAndTab/") 7 @RestController 8 public class PartDatabaseAndPartTableController { 9 10 @Autowired 11 private TabUserServiceImpl tabUserService; 12 13 /** 14 * 模擬插入資料 15 */ 16 List<TabUser> userList = new ArrayList<TabUser>(); 17 18 @PostConstruct 19 private void getData(){ 20 userList.add(new TabUser(1,"小小", "女", 3)); 21 userList.add(new TabUser(2,"爸爸", "男", 30)); 22 userList.add(new TabUser(3,"媽媽", "女", 28)); 23 userList.add(new TabUser(4,"爺爺", "男", 64)); 24 userList.add(new TabUser(5,"奶奶", "女", 62)); 25 } 26 27 @PostMapping("save-user") 28 public Object saveUser(){ 29 return tabUserService.saveForeach(userList); 30 } 31 32 @GetMapping("list-user") 33 public Object listUser(){ 34 return tabUserService.list(); 35 } 36 37 @GetMapping("list-user-order") 38 public Object listUserByOrder(@RequestParam(value = "column") String column,@RequestParam(value = "sort") String sort){ 39 return tabUserService.listBySort(column, sort); 40 } 41 }View Code
4. 測試結果
插入測試:
- 第一條資料小小,通過age欄位對2取模得到1,被分到ds1庫,通過id欄位對2取模得到1,分到tab_user1表。
ds0庫:
tab_user0:
tab_user1:
ds1庫:
tab_user0:
tab_user1:
本文來自部落格園,作者:EchoLv,轉載請註明原文連結:https://www.cnblogs.com/lvdeyinBlog/p/15386325.html