您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot整合sharding-jdbc實現自定義分庫分表的方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot整合sharding-jdbc實現自定義分庫分表的方法是什么”吧!
SpringBoot整合sharding-jdbc實現分庫分表與讀寫分離
用于數據庫/表拆分的關鍵字段
ex: 用戶表根據user_id取模拆分到不同的數據庫中
可參考:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding
精確分片算法
范圍分片算法
復合分片算法
Hint分片算法
3、分片策略(分片鍵+分片算法)
行表達式分片策略
標準分片策略
復合分片策略
Hint分片策略
不分片策略
可查看源碼 org.apache.shardingsphere.core.yaml.config.sharding.YamlShardingStrategyConfiguration
溫馨小提示:詳情可查看案例demo源碼
這里先貼出完整的application.yml
配置,后面實現每一種分片策略時,放開其相應配置即可~
# sharding-jdbc配置 spring: shardingsphere: # 是否開啟SQL顯示 props: sql: show: true # ====================== ↓↓↓↓↓↓ 數據源配置 ↓↓↓↓↓↓ ====================== datasource: names: ds-master-0,ds-slave-0-1,ds-slave-0-2,ds-master-1,ds-slave-1-1,ds-slave-1-2 # ====================== ↓↓↓↓↓↓ 配置第1個主從庫 ↓↓↓↓↓↓ ====================== # 主庫1 ds-master-0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3306/ds0?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root # 主庫1-從庫1 ds-slave-0-1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3307/ds0?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root # 主庫1-從庫2 ds-slave-0-2: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3307/ds0?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root # ====================== ↓↓↓↓↓↓ 配置第2個主從庫 ↓↓↓↓↓↓ ====================== # 主庫2 ds-master-1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3306/ds1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root # 主庫2-從庫1 ds-slave-1-1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3307/ds1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root # 主庫2-從庫2 ds-slave-1-2: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://127.0.0.1:3307/ds1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false # MySQL在高版本需要指明是否進行SSL連接 解決則加上 &useSSL=false username: root password: root sharding: # ====================== ↓↓↓↓↓↓ 讀寫分離配置 ↓↓↓↓↓↓ ====================== master-slave-rules: ds-master-0: # 主庫 masterDataSourceName: ds-master-0 # 從庫 slaveDataSourceNames: - ds-slave-0-1 - ds-slave-0-2 # 從庫查詢數據的負載均衡算法 目前有2種算法 round_robin(輪詢)和 random(隨機) # 算法接口 org.apache.shardingsphere.spi.masterslave.MasterSlaveLoadBalanceAlgorithm # 實現類 RandomMasterSlaveLoadBalanceAlgorithm 和 RoundRobinMasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithmType: ROUND_ROBIN ds-master-1: masterDataSourceName: ds-master-1 slaveDataSourceNames: - ds-slave-1-1 - ds-slave-1-2 loadBalanceAlgorithmType: ROUND_ROBIN # ====================== ↓↓↓↓↓↓ 分庫分表配置 ↓↓↓↓↓↓ ====================== tables: t_user: actual-data-nodes: ds-master-$->{0..1}.t_user$->{0..1} # 配置屬性可參考 org.apache.shardingsphere.core.yaml.config.sharding.YamlShardingStrategyConfiguration # =========== ↓↓↓↓↓↓ 行表達式分片策略 ↓↓↓↓↓↓ =========== # 在配置中使用 Groovy 表達式,提供對 SQL語句中的 = 和 IN 的分片操作支持,只支持單分片健。 # # ====== ↓↓↓↓↓↓ 分庫 ↓↓↓↓↓↓ ====== # database-strategy: # inline: # sharding-column: user_id # 添加數據分庫字段(根據字段插入數據到哪個庫 ex:user_id) # algorithm-expression: ds-master-$->{user_id % 2} # 根據user_id取模拆分到不同的庫中 # # ====== ↓↓↓↓↓↓ 分表 ↓↓↓↓↓↓ ====== # table-strategy: # inline: # sharding-column: sex # 添加數據分表字段(根據字段插入數據到哪個表 ex:sex) # algorithm-expression: t_user$->{sex % 2} # 分片算法表達式 => 根據用戶性別取模拆分到不同的表中 # =========== ↓↓↓↓↓↓ 標準分片策略 ↓↓↓↓↓↓ =========== # 精確分片算法 => sql在分庫/分表鍵上執行 = 與 IN 時觸發計算邏輯,否則不走分庫/分表,全庫/全表執行。 # database-strategy: # standard: # sharding-column: user_id # 分庫用到的鍵 # precise-algorithm-class-name: com.zhengqing.demo.config.sharding.precise.MyDbPreciseShardingAlgorithm # 自定義分庫算法實現類 # table-strategy: # standard: # sharding-column: sex # 添加數據分表字段(根據字段插入數據到那個表 ex:sex) # precise-algorithm-class-name: com.zhengqing.demo.config.sharding.precise.MyTablePreciseShardingAlgorithm # 自定義分表算法實現類 # 范圍分片算法 => sql在分庫/分表鍵上執行 BETWEEN AND、>、<、>=、<= 時觸發計算邏輯,否則不走分庫/分表,全庫/全表執行。 # database-strategy: # standard: # sharding-column: user_id # precise-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyDbPreciseShardingAlgorithm # range-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyDbRangeShardingAlgorithm # table-strategy: # standard: # sharding-column: sex # precise-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyTablePreciseShardingAlgorithm # range-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyTableRangeShardingAlgorithm # =========== ↓↓↓↓↓↓ 復合分片策略 ↓↓↓↓↓↓ =========== # SQL 語句中有>,>=, <=,<,=,IN 和 BETWEEN AND 等操作符,不同的是復合分片策略支持對多個分片健操作。 # database-strategy: # complex: # sharding-columns: user_id,sex # algorithm-class-name: com.zhengqing.demo.config.sharding.complex.MyDbComplexKeysShardingAlgorithm # table-strategy: # complex: # sharding-columns: user_id,sex # algorithm-class-name: com.zhengqing.demo.config.sharding.complex.MyTableComplexKeysShardingAlgorithm # =========== ↓↓↓↓↓↓ hint分片策略 ↓↓↓↓↓↓ =========== # 通過 Hint API實現個性化配置 => 可查看 com.zhengqing.demo.service.impl.UserServiceImpl.listPageForHint database-strategy: hint: algorithm-class-name: com.zhengqing.demo.config.sharding.hint.MyDbHintShardingAlgorithm table-strategy: hint: algorithm-class-name: com.zhengqing.demo.config.sharding.hint.MyTableHintShardingAlgorithm
1、行表達式分片策略
# =========== ↓↓↓↓↓↓ 行表達式分片策略 ↓↓↓↓↓↓ =========== # 在配置中使用 Groovy 表達式,提供對 SQL語句中的 = 和 IN 的分片操作支持,只支持單分片健。 # ====== ↓↓↓↓↓↓ 分庫 ↓↓↓↓↓↓ ====== database-strategy: inline: sharding-column: user_id # 添加數據分庫字段(根據字段插入數據到哪個庫 ex:user_id) algorithm-expression: ds-master-$->{user_id % 2} # 根據user_id取模拆分到不同的庫中 # ====== ↓↓↓↓↓↓ 分表 ↓↓↓↓↓↓ ====== table-strategy: inline: sharding-column: sex # 添加數據分表字段(根據字段插入數據到哪個表 ex:sex) algorithm-expression: t_user$->{sex % 2} # 分片算法表達式 => 根據用戶性別取模拆分到不同的表中
2、標準分片策略
A: 精確分片算法
# 精確分片算法 => sql在分庫/分表鍵上執行 = 與 IN 時觸發計算邏輯,否則不走分庫/分表,全庫/全表執行。 database-strategy: standard: sharding-column: user_id # 分庫用到的鍵 precise-algorithm-class-name: com.zhengqing.demo.config.sharding.precise.MyDbPreciseShardingAlgorithm # 自定義分庫算法實現類 table-strategy: standard: sharding-column: sex # 添加數據分表字段(根據字段插入數據到那個表 ex:sex) precise-algorithm-class-name: com.zhengqing.demo.config.sharding.precise.MyTablePreciseShardingAlgorithm # 自定義分表算法實現類
@Slf4j public class MyDbPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { /** * 分片策略 * * @param dbNameList 所有數據源 * @param shardingValue SQL執行時傳入的分片值 * @return 數據源名稱 */ @Override public String doSharding(Collection<String> dbNameList, PreciseShardingValue<Long> shardingValue) { log.info("[MyDbPreciseShardingAlgorithm] SQL執行時傳入的分片值: [{}]", shardingValue); // 根據user_id取模拆分到不同的庫中 Long userId = shardingValue.getValue(); for (String dbNameItem : dbNameList) { if (dbNameItem.endsWith(String.valueOf(userId % 2))) { return dbNameItem; } } return null; } }
@Slf4j public class MyTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Byte> { /** * 分片策略 * * @param tableNameList 所有表名 * @param shardingValue SQL執行時傳入的分片值 * @return 表名 */ @Override public String doSharding(Collection<String> tableNameList, PreciseShardingValue<Byte> shardingValue) { log.info("[MyTablePreciseShardingAlgorithm] SQL執行時傳入的分片值: [{}]", shardingValue); // 根據用戶性別取模拆分到不同的表中 Byte sex = shardingValue.getValue(); for (String tableNameItem : tableNameList) { if (tableNameItem.endsWith(String.valueOf(sex % 2))) { return tableNameItem; } } return null; } }
B: 范圍分片算法
# 范圍分片算法 => sql在分庫/分表鍵上執行 BETWEEN AND、>、<、>=、<= 時觸發計算邏輯,否則不走分庫/分表,全庫/全表執行。 database-strategy: standard: sharding-column: user_id precise-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyDbPreciseShardingAlgorithm range-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyDbRangeShardingAlgorithm table-strategy: standard: sharding-column: sex precise-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyTablePreciseShardingAlgorithm range-algorithm-class-name: com.zhengqing.demo.config.sharding.range.MyTableRangeShardingAlgorithm
@Slf4j public class MyDbPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { /** * 分片策略 * * @param dbNameList 所有數據源 * @param shardingValue SQL執行時傳入的分片值 * @return 數據源名稱 */ @Override public String doSharding(Collection<String> dbNameList, PreciseShardingValue<Long> shardingValue) { log.info("[MyDbPreciseShardingAlgorithm] SQL執行時傳入的分片值: [{}]", shardingValue); // 根據user_id取模拆分到不同的庫中 Long userId = shardingValue.getValue(); for (String dbNameItem : dbNameList) { if (dbNameItem.endsWith(String.valueOf(userId % 2))) { return dbNameItem; } } return null; } }
@Slf4j public class MyDbRangeShardingAlgorithm implements RangeShardingAlgorithm<Long> { @Override public Collection<String> doSharding(Collection<String> dbNameList, RangeShardingValue<Long> shardingValue) { log.info("[MyDbRangeShardingAlgorithm] shardingValue: [{}]", shardingValue); List<String> result = Lists.newLinkedList(); int dbSize = dbNameList.size(); // 從sql 中獲取 Between 1 and 1000 的值 // lower:1 // upper:1000 Range<Long> rangeValue = shardingValue.getValueRange(); Long lower = rangeValue.lowerEndpoint(); Long upper = rangeValue.upperEndpoint(); // 根據范圍值取偶選擇庫 for (Long i = lower; i <= upper; i++) { for (String dbNameItem : dbNameList) { if (dbNameItem.endsWith(String.valueOf(i % 2))) { result.add(dbNameItem); } if (result.size() >= dbSize) { return result; } } } return result; } }
@Slf4j public class MyTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Byte> { /** * 分片策略 * * @param tableNameList 所有表名 * @param shardingValue SQL執行時傳入的分片值 * @return 表名 */ @Override public String doSharding(Collection<String> tableNameList, PreciseShardingValue<Byte> shardingValue) { log.info("[MyTablePreciseShardingAlgorithm] SQL執行時傳入的分片值: [{}]", shardingValue); // 根據用戶性別取模拆分到不同的表中 Byte sex = shardingValue.getValue(); for (String tableNameItem : tableNameList) { if (tableNameItem.endsWith(String.valueOf(sex % 2))) { return tableNameItem; } } return null; } }
@Slf4j public class MyTableRangeShardingAlgorithm implements RangeShardingAlgorithm<Byte> { @Override public Collection<String> doSharding(Collection<String> tableNameList, RangeShardingValue<Byte> shardingValue) { log.info("[MyTableRangeShardingAlgorithm] shardingValue: [{}]", shardingValue); Set<String> tableNameResultList = new LinkedHashSet<>(); Range<Byte> rangeValue = shardingValue.getValueRange(); Byte lower = rangeValue.lowerEndpoint(); Byte upper = rangeValue.upperEndpoint(); // between 0 and 1 // 根據性別值選擇表 for (String tableNameItem : tableNameList) { if (tableNameItem.endsWith(String.valueOf(lower)) || tableNameItem.endsWith(String.valueOf(upper))) { tableNameResultList.add(tableNameItem); } } return tableNameResultList; } }
3、復合分片策略
# =========== ↓↓↓↓↓↓ 復合分片策略 ↓↓↓↓↓↓ =========== # SQL 語句中有>,>=, <=,<,=,IN 和 BETWEEN AND 等操作符,不同的是復合分片策略支持對多個分片健操作。 database-strategy: complex: sharding-columns: user_id,sex algorithm-class-name: com.zhengqing.demo.config.sharding.complex.MyDbComplexKeysShardingAlgorithm table-strategy: complex: sharding-columns: user_id,sex algorithm-class-name: com.zhengqing.demo.config.sharding.complex.MyTableComplexKeysShardingAlgorithm
@Slf4j public class MyDbComplexKeysShardingAlgorithm implements ComplexKeysShardingAlgorithm<String> { @Override public Collection<String> doSharding(Collection<String> dbNameList, ComplexKeysShardingValue<String> complexKeysShardingValue) { log.info("[MyDbComplexKeysShardingAlgorithm] complexKeysShardingValue: [{}]", complexKeysShardingValue); List<String> dbResultList = new ArrayList<>(); int dbSize = dbNameList.size(); // 得到每個分片健對應的值 // 用戶id 范圍查詢 Range<String> rangeUserId = complexKeysShardingValue.getColumnNameAndRangeValuesMap().get("user_id"); // 性別 List<String> sexValueList = this.getShardingValue(complexKeysShardingValue, "sex"); // 對兩個分片健進行邏輯操作,選擇最終數據進哪一庫? TODO for (String sex : sexValueList) { String suffix = String.valueOf(Long.parseLong(sex) % 2); for (String dbNameItem : dbNameList) { if (dbNameItem.endsWith(suffix)) { dbResultList.add(dbNameItem); } if (dbResultList.size() >= dbSize) { return dbResultList; } } } return dbResultList; } private List<String> getShardingValue(ComplexKeysShardingValue<String> shardingValues, final String key) { List<String> valueList = new ArrayList<>(); Map<String, Collection<String>> columnNameAndShardingValuesMap = shardingValues.getColumnNameAndShardingValuesMap(); if (columnNameAndShardingValuesMap.containsKey(key)) { valueList.addAll(columnNameAndShardingValuesMap.get(key)); } return valueList; } }
@Slf4j public class MyTableComplexKeysShardingAlgorithm implements ComplexKeysShardingAlgorithm<Long> { @Override public Collection<String> doSharding(Collection<String> tableNameList, ComplexKeysShardingValue<Long> complexKeysShardingValue) { log.info("[MyTableComplexKeysShardingAlgorithm] complexKeysShardingValue: [{}]", complexKeysShardingValue); Set<String> tableNameResultList = new LinkedHashSet<>(); int tableSize = tableNameList.size(); // 用戶id 范圍查詢 Range<Long> rangeUserId = complexKeysShardingValue.getColumnNameAndRangeValuesMap().get("user_id"); Long lower = rangeUserId.lowerEndpoint(); Long upper = rangeUserId.upperEndpoint(); // 根據user_id選擇表 TODO ... for (String tableNameItem : tableNameList) { if (tableNameItem.endsWith(String.valueOf(lower % 2)) || tableNameItem.endsWith(String.valueOf(upper % 2))) { tableNameResultList.add(tableNameItem); } if (tableNameResultList.size() >= tableSize) { return tableNameResultList; } } return tableNameResultList; } }
4、Hint分片策略
#=========== ↓↓↓↓↓↓ hint分片策略 ↓↓↓↓↓↓ =========== # 通過 Hint API實現個性化配置 => 可查看 com.zhengqing.demo.service.impl.UserServiceImpl.listPageForHint database-strategy: hint: algorithm-class-name: com.zhengqing.demo.config.sharding.hint.MyDbHintShardingAlgorithm table-strategy: hint: algorithm-class-name: com.zhengqing.demo.config.sharding.hint.MyTableHintShardingAlgorithm
@Slf4j public class MyDbHintShardingAlgorithm implements HintShardingAlgorithm<Integer> { @Override public Collection<String> doSharding(Collection<String> dbNameList, HintShardingValue<Integer> hintShardingValue) { log.info("[MyDbHintShardingAlgorithm] hintShardingValue: [{}]", hintShardingValue); Collection<String> dbResultList = new ArrayList<>(); int dbSize = dbNameList.size(); for (String dbNameItem : dbNameList) { for (Integer shardingValue : hintShardingValue.getValues()) { if (dbNameItem.endsWith(String.valueOf(shardingValue % 2))) { dbResultList.add(dbNameItem); } if (dbResultList.size() >= dbSize) { return dbResultList; } } } return dbResultList; } }
@Slf4j public class MyTableHintShardingAlgorithm implements HintShardingAlgorithm<Integer> { @Override public Collection<String> doSharding(Collection<String> tableNameList, HintShardingValue<Integer> hintShardingValue) { log.info("[MyTableHintShardingAlgorithm] hintShardingValue: [{}]", hintShardingValue); Collection<String> tableResultList = new ArrayList<>(); int tableSize = tableNameList.size(); Collection<Integer> hintShardingValueValueList = hintShardingValue.getValues(); for (String tableName : tableNameList) { for (Integer shardingValue : hintShardingValueValueList) { if (tableName.endsWith(String.valueOf(shardingValue % 2))) { tableResultList.add(tableName); } if (tableResultList.size() >= tableSize) { return tableResultList; } } } return tableResultList; } }
使用時動態觸發如下:
public IPage<User> listPageForHint() { // 清除掉上一次的規則,否則會報錯 HintManager.clear(); // HintManager API 工具類實例 HintManager hintManager = HintManager.getInstance(); // 庫 => 主要是將value值傳送到 MyDbHintShardingAlgorithm 中做邏輯分庫處理 hintManager.addDatabaseShardingValue("t_user", 100); hintManager.addDatabaseShardingValue("t_user", 1000); // 指定表的分片健 => 指定查t_user0 hintManager.addTableShardingValue("t_user", 0); // hintManager.addTableShardingValue("t_user", 1); // 讀寫分離強制讀主庫,避免造成主從復制導致的延遲 hintManager.setMasterRouteOnly(); // 查詢數據 Page<User> result = this.userMapper.selectPage(new Page<>(1, 10), new LambdaQueryWrapper<User>() .eq(User::getSex, "0") .between(User::getUserId, 1L, 1000L) ); // 清除規則 hintManager.close(); return result; }
感謝各位的閱讀,以上就是“SpringBoot整合sharding-jdbc實現自定義分庫分表的方法是什么”的內容了,經過本文的學習后,相信大家對SpringBoot整合sharding-jdbc實現自定義分庫分表的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。