一,shardingjdbc的用途
1,官方站介紹:
Apache ShardingSphere 是一套開源的分布式數據庫中間件解決方案組成的生態圈,
它由 JDBC、Proxy 和 Sidecar(規划中)這 3 款相互獨立,卻又能夠混合部署配合使用的產品組成。
它們均提供標准化的數據分片、分布式事務和數據庫治理功能,
可適用於如 Java 同構、異構語言、雲原生等各種多樣化的應用場景
2,網址:
官方站:
http://shardingsphere.apache.org/index_zh.html
官方示例:
https://github.com/apache/shardingsphere-example
官方文檔(4.x):
https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,演示項目的相關信息
1,項目地址(完整代碼):
https://github.com/liuhongdi/shardingjdbc
2,項目說明:
兩個數據庫資源:saleorder01,saleorder02
下面包含了相同結構的數據表各兩個,分別是:
t_order_1,
t_order_2,
t_order_3,
t_order_4
3,數據庫結構
如圖:
4,項目結構:
如圖:
三,配置文件說明:
1,數據庫的創建sql:
CREATE DATABASE `saleorder01` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
CREATE DATABASE `saleorder02` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
2,數據表的創建sql:
CREATE TABLE `t_order_1` ( `orderId` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', `goodsName` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'name', PRIMARY KEY (`orderId`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='order'
其他三個表sql相同
3,application.properties:
#shardingsphere spring.shardingsphere.datasource.names=saleorder01,saleorder02 spring.shardingsphere.datasource.saleorder01.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.saleorder01.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.saleorder01.jdbc-url=jdbc:mysql://127.0.0.1:3306/saleorder01?characterEncoding=utf-8 spring.shardingsphere.datasource.saleorder01.username=root spring.shardingsphere.datasource.saleorder01.password=passdemo spring.shardingsphere.datasource.saleorder02.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.saleorder02.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.saleorder02.jdbc-url=jdbc:mysql://127.0.0.1:3306/saleorder02?characterEncoding=utf-8 spring.shardingsphere.datasource.saleorder02.username=root spring.shardingsphere.datasource.saleorder02.password=passdemo spring.shardingsphere.sharding.default-data-source-name=saleorder01 spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=orderId spring.shardingsphere.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.shardingjdbc.demo.algorithm.DatabasePreciseShardingAlgorithm spring.shardingsphere.sharding.binding-tables=t_order spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=saleorder0$->{1..1}.t_order_$->{1..2},saleorder0$->{2..2}.t_order_$->{3..4} spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.sharding-column=orderId spring.shardingsphere.sharding.tables.t_order.table-strategy.standard.precise-algorithm-class-name=com.shardingjdbc.demo.algorithm.OrderTablePreciseShardingAlgorithm spring.shardingsphere.props.sql.show=true
說明:
com.shardingjdbc.demo.algorithm.DatabasePreciseShardingAlgorithm:數據庫得到數據源的算法
com.shardingjdbc.demo.algorithm.OrderTablePreciseShardingAlgorithm:t_order表得到表名的算法
spring.shardingsphere.datasource.names=saleorder01,saleorder02: 指定數據源的名字
spring.shardingsphere.sharding.binding-tables=t_order: 指定綁定表的名字
spring.shardingsphere.props.sql.show=true:打印sql
四,java代碼說明
DatabasePreciseShardingAlgorithm.java
public class DatabasePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { Long curValue = shardingValue.getValue(); String curBase = ""; if (curValue > 0 && curValue<=200) { curBase = "saleorder01"; } else { curBase = "saleorder02"; } return curBase; } }
說明:根據id返回數據庫資源名
OrderTablePreciseShardingAlgorithm.java
public class OrderTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) { Long curValue = shardingValue.getValue(); String curTable = ""; if (curValue > 0 && curValue<=100) { curTable = "t_order_1"; } else if (curValue > 100 && curValue<=200) { curTable = "t_order_2"; } else if (curValue > 200 && curValue<=300) { curTable = "t_order_3"; } else { curTable = "t_order_4"; } return curTable; } }
說明:根據id返回數據表名
五,效果演示
1,添加一個訂單:
訪問: /order/add/
2,查看訂單列表:
訪問: /order/list/
六,shardingjdbc使用中的注意事項:
1,如果有的表比較小,可以存在於各個庫中,
這里可以使用公共表(廣播表):例:
spring.shardingsphere.sharding.broadcast-tables=t_dict
寫入時會寫入到各個庫,
讀取時從本地庫中讀取,可以避免跨節點的查詢
2,打開sql顯示,用於調試
spring.shardingsphere.props.sql.show= #是否開啟SQL顯示,默認值: false
spring.shardingsphere.props.sql.show=true
3,如果是用戶表分表,需要使用表內唯一的字段如:用戶名/手機號作為sharding column做拆分
4,即使做了分表,仍然要添加查詢時使用到的索引,
否則效率仍然會成問題
5,不參與分表的數據表,要指定所在的數據源:如下:
spring.shardingsphere.sharding.default-data-source-name=saleorder01
七,查看spring boot版本
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE)
