数据库分库分表实践:tech-pdai-spring-demos中的Sharding-JDBC配置指南

发布时间:2026/7/29 21:06:37
数据库分库分表实践:tech-pdai-spring-demos中的Sharding-JDBC配置指南 数据库分库分表实践tech-pdai-spring-demos中的Sharding-JDBC配置指南【免费下载链接】tech-pdai-spring-demosSpring Framework5/SpringBoot 2.5.x Demos项目地址: https://gitcode.com/gh_mirrors/te/tech-pdai-spring-demos在现代应用开发中随着数据量的快速增长数据库分库分表已成为解决性能瓶颈的关键技术。tech-pdai-spring-demos项目提供了基于Sharding-JDBC的完整分库分表解决方案帮助开发者轻松实现数据分片策略。本文将详细介绍如何在该项目中配置和使用Sharding-JDBC进行表级分片适合新手快速上手分库分表实践。为什么选择Sharding-JDBCSharding-JDBC作为轻量级的分布式数据库中间件具有以下核心优势无侵入性完全兼容JDBC和各种ORM框架无需修改业务代码灵活分片策略支持行表达式、自定义算法等多种分片方式读写分离可配合主从架构实现读写分离分布式事务提供柔性事务支持在tech-pdai-spring-demos项目中多个模块展示了Sharding-JDBC的不同应用场景包括241-springboot-demo-shardingjdbc-mybatis-tables表分片243-springboot-demo-shardingjdbc-jpa-tablesJPA集成表分片244-springboot-demo-shardingjdbc-jpa-tenant-db多租户分库246-springboot-demo-shardingjdbc-jpa-masterslave主从架构快速开始表分片环境搭建1. 引入Sharding-JDBC依赖在项目的pom.xml中添加Sharding-JDBC Starter依赖以241-springboot-demo-shardingjdbc-mybatis-tables模块为例dependency groupIdorg.apache.shardingsphere/groupId artifactIdsharding-jdbc-spring-boot-starter/artifactId version4.1.1/version /dependency完整的依赖配置可参考241-springboot-demo-shardingjdbc-mybatis-tables/pom.xml2. 配置数据源与分片规则在application.yml中配置数据源和分片策略核心配置如下spring: shardingsphere: datasource: names: ds ds: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test_db_sharding?allowPublicKeyRetrievaltrueuseSSLfalse username: test password: bfXa4Pt2lUUScy8jakXf sharding: tables: tb_user: actual-data-nodes: ds.tb_user_$-{0..1} table-strategy: inline: sharding-column: id algorithm-expression: tb_user_$-{id % 2} key-generator: column: id type: SNOWFLAKE上述配置实现了使用单个数据源(ds)将tb_user表按id取模2分为tb_user_0和tb_user_1两个实际表使用雪花算法(SNOWFLAKE)生成分布式ID完整配置文件路径241-springboot-demo-shardingjdbc-mybatis-tables/src/main/resources/application.yml核心分片策略详解行表达式分片策略Sharding-JDBC最常用的分片方式通过inline表达式定义分片规则table-strategy: inline: sharding-column: id algorithm-expression: tb_user_$-{id % 2}语法说明$-{}行表达式标识id % 2按id字段取模2结果为0时路由到tb_user_0为1时路由到tb_user_1分布式ID生成配置雪花算法生成全局唯一IDkey-generator: column: id type: SNOWFLAKE props: worker: id: 123worker.id节点ID分布式环境下需保证唯一支持的ID生成器SNOWFLAKE、UUID、LEAF等多场景分库分表示例1. 多租户分库方案在244-springboot-demo-shardingjdbc-jpa-tenant-db模块中实现了基于租户ID的分库策略spring: shardingsphere: sharding: default-database-strategy: inline: sharding-column: tenant_id algorithm-expression: ds_$-{tenant_id % 2}通过default-database-strategy配置默认分库策略所有表都会按tenant_id路由到不同数据库。2. 主从复制与读写分离246-springboot-demo-shardingjdbc-jpa-masterslave模块展示了如何配置读写分离spring: shardingsphere: rules: readwrite-splitting: >git clone https://gitcode.com/gh_mirrors/te/tech-pdai-spring-demos选择合适的Sharding-JDBC模块表分片241-springboot-demo-shardingjdbc-mybatis-tablesJPA集成243-springboot-demo-shardingjdbc-jpa-tables多租户分库244-springboot-demo-shardingjdbc-jpa-tenant-db修改数据库配置编辑application.yml中的数据库连接信息创建对应的分片数据库和表运行测试cd 241-springboot-demo-shardingjdbc-mybatis-tables mvn spring-boot:run常见问题解决1. 分片键必须出现在SQL中错误示例SELECT * FROM tb_user WHERE name test正确示例SELECT * FROM tb_user WHERE id 1 AND name test2. 不支持的SQL函数Sharding-JDBC对部分SQL函数有限制如不支持LAST_INSERT_ID()应使用雪花算法等分布式ID生成策略。3. 事务支持默认情况下Sharding-JDBC使用本地事务如需分布式事务支持需添加额外配置spring: shardingsphere: rules: transaction: type: XA props: xa-transaction-manager-type: Atomikos总结tech-pdai-spring-demos项目提供了丰富的Sharding-JDBC实践案例从基础的表分片到复杂的多租户分库覆盖了大部分实际应用场景。通过本文介绍的配置方法开发者可以快速掌握分库分表技术为高并发、大数据量应用提供可靠的数据存储解决方案。建议结合具体业务场景选择合适的分片策略并通过项目中的测试用例深入理解Sharding-JDBC的实现原理。【免费下载链接】tech-pdai-spring-demosSpring Framework5/SpringBoot 2.5.x Demos项目地址: https://gitcode.com/gh_mirrors/te/tech-pdai-spring-demos创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考