一,shardingjdbc的用途
http://shardingsphere.apache.org/index_zh.html
https://github.com/apache/shardingsphere-example
https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
https://github.com/liuhongdi/shardingjdbc
2,项目说明:
两个数据库资源:saleorder01,saleorder02
下面包含了相同结构的数据表各两个,分别是:
t_order_1,
t_order_2,
t_order_3,
t_order_4
3,数据库结构
如图:
4,项目结构:
如图:
三,配置文件说明:
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‘ */
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相同
#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代码说明
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返回数据库资源名
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返回数据表名
五,效果演示
六,shardingjdbc使用中的注意事项:
spring.shardingsphere.sharding.broadcast-tables=t_dict
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.sharding.default-data-source-name=saleorder01
七,查看spring boot版本
. ____ _ __ _ _ /\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE)
spring boot:用shardingjdbc实现多数据源的分库分表(shardingsphere 4.1.1/spring boot 2.3.1)