通過ShardingAlgorithm的實現,可以進一步發現分片策略的靈活和強大;可以實現一致性hash算法、按時間分片算法、以及mod算法等;
更進一步,可以對同一個表按業務需求實現不同的分片算法,比如原來按年分片的業務表,比如隨着業務量的擴展,需要提高分片頻率,
可是又不想進行大量歷史數據遷移,可以在某一時刻開始按月或者按日分片;當然前提是要維護一個相對復雜的分片算法;
下面展示一個自定義分片算法原型,留作業務擴展;
業務模型和上一篇的inline表達式一樣,下面進行核心代碼說明:
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內容:
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: standard: sharding-column: gender precise-algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.DbShardingAlgorithm table-strategy: complex: sharding-columns: id algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.MemberTblComplexKeySharding 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)database數據源的sharding算法,實現了PreciseShardingAlgorithm
package com.chong.mcspcshardingdbtable.sharding; import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue; import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm; import org.springframework.stereotype.Component; import java.util.Collection; @Component public class DbShardingAlgorithm implements PreciseShardingAlgorithm<Integer> { @Override public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) { Integer index = preciseShardingValue.getValue() % 2; for (String dataSourceName : collection) { if (dataSourceName.endsWith(index + "")) { return dataSourceName; } } throw new UnsupportedOperationException(); } }
4)table的sharding算法,實現了ComplexKeysShardingAlgorithm
package com.chong.mcspcshardingdbtable.sharding; import com.google.common.collect.Range; import io.shardingsphere.api.algorithm.sharding.ListShardingValue; import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue; import io.shardingsphere.api.algorithm.sharding.RangeShardingValue; import io.shardingsphere.api.algorithm.sharding.ShardingValue; import io.shardingsphere.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * 通過復合分片鍵進行演示,覆蓋Precise,Range,List三種類型的ShardingValue。 * 項目中應根據實際情況實現: * 1精確分片PreciseShardingAlgorithm、 * 2范圍分片RangeShardingAlgorithm * 3復合分片ComplexKeysShardingAlgorithm * 4非SQL解析分片HintShardingAlgorithm */ @Component public class MemberTblComplexKeySharding implements ComplexKeysShardingAlgorithm { private static String shardingColumn1 = "id"; // todo: 業務擴展 shardingcolumn2...n private static String targetLogicTable = "mc_member"; @Override public Collection<String> doSharding(Collection<String> logicTables, Collection<ShardingValue> shardingValues) { // 當設置多個shardingcolumn時,重寫下面邏輯,根據shardingValues參數和實際分表業務規則計算出實際的actualTable List<Long> ids = new ArrayList<>(); // todo: 業務擴展 sharding-cloumn-parame-value-List2...n ShardingValue shardingValue = getShardingValue(shardingValues, shardingColumn1); // todo: 業務擴展 shardingValue2..n if (shardingValue instanceof PreciseShardingValue) { PreciseShardingValue<Long> preciseShardingValue = (PreciseShardingValue<Long>) shardingValue; Long id = preciseShardingValue.getValue(); ids.add(id); } else if (shardingValue instanceof RangeShardingValue) { RangeShardingValue<Long> rangeShardingValue = (RangeShardingValue<Long>) shardingValue; Range<Long> range = rangeShardingValue.getValueRange(); for (Long index = range.lowerEndpoint(); index <= range.upperEndpoint(); index++) { ids.add(index); } } else if (shardingValue instanceof ListShardingValue) { ListShardingValue<Long> listShardingValue = (ListShardingValue<Long>) shardingValue; ids.addAll(listShardingValue.getValues()); } return getActualTables(logicTables, ids, targetLogicTable); // todo:業務擴展 傳參 sharding-cloumn-parame-value-List2...n } /** * 根據sql語句中的id值,和logicTable進行拼接,獲取實際要查詢的表. * 實際業務中遇到多個分片列時,除了ids還需要考慮其他key值,合並計算對邏輯表進行組裝 */ private List<String> getActualTables(Collection<String> logicTables, List<Long> ids, String targetLogicTable) { List<String> actualTables = new ArrayList<>(); if (logicTables.contains(targetLogicTable)) { for (Long id : ids) { // 作為演示,僅對id%2作為表后綴的匹配規則,今后應根據實際情況重置分片列和匹配規則。 String actualTableName = targetLogicTable + id%2; if(!actualTables.contains(actualTableName)) { actualTables.add(targetLogicTable + (id % 2)); } } } if (actualTables.size() == 0) { throw new UnsupportedOperationException(); } return actualTables; } /** * 根據預知的分拆列獲取到對應的shardingvalue對象 */ private ShardingValue getShardingValue(Collection<ShardingValue> shardingValues, String column) { for (ShardingValue sv : shardingValues) { if (sv.getColumnName().equals(column)) { return sv; } } throw new UnsupportedOperationException(); } }
5)啟動類
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); } }
其他的controller、serveice、entity、repository等就不展示了。源碼放置git,有需要的聯系我就行。