本例主要參看官方的配置進行作業,實現簡單的mod算法分庫分表,對於分庫分表的理解比較合適。
生產環境上建議自定義分庫分表用算法(下一篇隨筆介紹)。
1)關鍵部分的pom依賴:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2)關鍵yml配置:
2.1)數據源:6個數據源,兩個集群,分別為1主2從;
主1:master0;從1:master0salve0;從2:master0slave1
主2:master1;從1:master1slave0;從2:master1slave1
2.2)作為demo演示,會員表進行分庫和分表;每個庫里有兩張表:mc_member0和mc_member1;
2.3)分庫規則:gender=1(男)時 gender%2 -> master1庫; gender=2(女)時 gender%2 -> master0庫;
2.4)分表規則:id偶數時,id%2->mc_member0表;id為奇數時,id%2->mc_member1表;
2.5)注意事項1:對邏輯表配置分片策略時,數據庫分片策略設置邏輯數據源,不使用物理庫名;表分片策略內,使用物理表名;
2.6)配置讀寫分離:master-slave-rules下的rule邏輯名可以自定義如 ms1和ds1等。
sharding: jdbc: datasource: names: master0,master0salve0,master0slave1,master1,master1slave0,master1slave1 master0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master0salve0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master0slave1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1slave0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1slave1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root config: sharding: tables: mc_member: actual-nodes: mcspcsharding$->{0..1}.mc_member$->{0..1} database-strategy: inline: sharding-column: gender algorithm-expression: master$->{gender % 2} table-strategy: inline: sharding-column: id algorithm-expression: mc_member$->{id % 2} binding-tables: mc_member # 多個時逗號隔開 broadcast-tables: mc_master master-slave-rules: ms0: master-data-source-name: master0 slave-data-source-names: master0salve0,master0slave1 ms1: master-data-source-name: master1 slave-data-source-names: master1slave0,master1slave1 props: sql: show: true
3)啟動類。因為數據源的重復注入問題,需要進行如下配置:
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
package com.chong.mcspcshardingdbtable; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.ComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class}) @EnableDiscoveryClient @EnableTransactionManagement @ComponentScan(basePackages = {"com.chong.common","com.chong.mcspcshardingdbtable"}) public class McSpcShardingDbTableApplication { public static void main(String[] args) { SpringApplication.run(McSpcShardingDbTableApplication.class, args); } }
其他的sever和repository等不在貼碼,自己試驗下應該很容易實現。