【经验总结】ShardingSphere5.2.1 + Springboot 分库分表 快速开始

📅 发布时间:2026/7/5 21:39:53 👁️ 浏览次数:
【经验总结】ShardingSphere5.2.1 + Springboot 分库分表 快速开始
Sharding Sphere 官方文档地址https://shardingsphere.apache.org/document/current/cn/overview/maven仓库https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc官方的文档写的很详尽到位这里会截取部分文档内容便于理解和快速上手强烈推荐阅读官方文档原文什么是 ShardingSphere一、介绍ShardingSphereApache ShardingSphere 本文后续简称“SS”是一款分布式的数据库生态系统 可以将任意数据库转换为分布式数据库并通过数据分片、弹性伸缩、加密等能力对原有数据库进行增强。Apache ShardingSphere 设计哲学为 Database Plus旨在构建异构数据库上层的标准和生态。 它关注如何充分合理地利用数据库的计算和存储能力而并非实现一个全新的数据库。 它站在数据库的上层视角关注它们之间的协作多于数据库自身。ShardingSphere一般指代的是以下两个产品ShardingSphere-JDBC 定位为轻量级 Java 框架在 Java 的 JDBC 层提供的额外服务。ShardingSphere-Proxy 定位为透明化的数据库代理端通过实现数据库二进制协议对异构语言提供支持。关于 《Database Plus 设计哲学》二、部署形态ShardingSphere-JDBCShardingSphere-Proxy数据库任意MySQL/PostgreSQL连接消耗数高低异构语言仅 Java任意性能损耗低损耗略高无中心化是否静态入口无有直观理解引入服务内部使用配置后实现多数据源管理代理数据库对服务相当于直连数据库ShardingSphere-JDBC 独立部署ShardingSphere-JDBC 定位为轻量级 Java 框架在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库以 jar 包形式提供服务无需额外部署和依赖可理解为增强版的 JDBC 驱动完全兼容 JDBC 和各种 ORM 框架。适用于任何基于 JDBC 的 ORM 框架如JPA, Hibernate, Mybatis, Spring JDBC Template 或直接使用 JDBC支持任何第三方的数据库连接池如DBCP, C3P0, BoneCP, HikariCP 等支持任意实现 JDBC 规范的数据库目前支持 MySQLPostgreSQLOracleSQLServer 以及任何可使用 JDBC 访问的数据库。ShardingSphere-Proxy 独立部署ShardingSphere-Proxy 定位为透明化的数据库代理端通过实现数据库二进制协议对异构语言提供支持。 目前提供 MySQL 和 PostgreSQL 协议透明化数据库操作对 DBA 更加友好。向应用程序完全透明可直接当做 MySQL/PostgreSQL 使用兼容 MariaDB 等基于 MySQL 协议的数据库以及 openGauss 等基于 PostgreSQL 协议的数据库适用于任何兼容 MySQL/PostgreSQL 协议的的客户端如MySQL Command Client, MySQL Workbench, Navicat 等。混合部署架构进阶暂不考虑三、快速开始选项采用ShardingSphere-JDBC单机部署结合springboot管理postgresql数据库快速开始最新的5.5.0配置手册不够详致这里找到历史的的使用手册SPRING BOOT STARTERhttps://shardingsphere.apache.org/document/5.0.0/cn/user-manual/https://shardingsphere.apache.org/document/5.2.1/cn/user-manual/关于版本选择这里使用5.0.0版本5.0.0的spring-boot-starter的版本存在一些bug和官方文档对不起来改用5.2.1一、环境准备必备的依赖dependency groupIdorg.apache.shardingsphere/groupId artifactIdshardingsphere-jdbc-core-spring-boot-starter/artifactId version${shardingsphere.version}/version /dependency搭配一些环境基础的依赖dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.4.2/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.postgis/groupId artifactIdpostgis-jdbc/artifactId version1.3.3/version scopecompile/scope /dependencyyaml读取错误问题和解决方案解决方案https://blog.csdn.net/weixin_47899191/article/details/130743334报错信息如下*************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.apache.shardingsphere.infra.util.yaml.constructor.ShardingSphereYamlConstructor$1.init(ShardingSphereYamlConstructor.java:44)解决方案覆盖springboot2.x的SnakeYAML依赖如果你是sringboot3.x可能不需要作者没有尝试。原因参考github上的Issueshttps://github.com/apache/shardingsphere/issues/21476引入依赖覆盖springboot默认版本即解决dependency groupIdorg.yaml/groupId artifactIdsnakeyaml/artifactId version1.33/version /dependency二、快速配置官方示例参考官方给的示例配置如下可以转换成yml格式更具有可视化# 配置真实数据源 spring.shardingsphere.datasource.namesds0,ds1 # 配置第 1 个数据源 spring.shardingsphere.datasource.ds0.typecom.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-namecom.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-urljdbc:mysql://localhost:3306/ds0 spring.shardingsphere.datasource.ds0.usernameroot spring.shardingsphere.datasource.ds0.password # 配置第 2 个数据源 spring.shardingsphere.datasource.ds1.typecom.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-namecom.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-urljdbc:mysql://localhost:3306/ds1 spring.shardingsphere.datasource.ds1.usernameroot spring.shardingsphere.datasource.ds1.password # 配置 t_order 表规则 spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodesds$-{0..1}.t_order$-{0..1} # 配置分库策略 spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-columnuser_id spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-namedatabase_inline # 配置分表策略 spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-columnorder_id spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-nametable_inline # 省略配置 t_order_item 表规则... # ... # 配置 分片算法 spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.typeINLINE spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm-expressionds_${user_id % 2} spring.shardingsphere.rules.sharding.sharding-algorithms.table_inline.typeINLINE spring.shardingsphere.rules.sharding.sharding-algorithms.table_inline.props.algorithm-expressiont_order_${order_id % 2}快速配置yaml测试可行结合potgresql配置如下server: port: 9696 spring: shardingsphere: mode: type: Standalone repository: type: JDBC props: # 禁用执行SQL用于获取表元数据 sql-show: true # 禁用执行SQL用于获取数据库元数据 # check-table-metadata-enabled: false datasource: # 配置真实数据源 相当于ds0,ds1 names: ds0,ds1 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: org.postgresql.Driver jdbc-url: jdbc:postgresql://localhost:5432/sd0 username: postgres password: root ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: org.postgresql.Driver jdbc-url: jdbc:postgresql://localhost:5432/sd1 username: postgres password: root rules: sharding: tables: #这里以student表为例 student: # 表名的分片规则 # 由数据源名 表名组成参考 Inline 语法规则 actual-data-nodes: ds$-{0..1}.student$-{0..1} # 分布式序列策略 key-generate-strategy: # 自增列名称缺省表示不使用自增主键生成器 column: id # 分布式序列算法名称 key-generator-name: snowflake # 配置分库策略缺省表示使用默认分库策略以下的分片策略只能选其一standard/complex/hint/none database-strategy: # 用于单分片键的标准分片场景 standard: # 分片列名称 这里指定age作为分库键 sharding-column: age # 分片算法名称 sharding-algorithm-name: student_age_inline # 配置分表策略分库键class_id分库策略student_class_id_inline table-strategy: standard: sharding-column: class_id sharding-algorithm-name: student_class_id_inline # 配置分片算法 sharding-algorithms: # 分库策略根据age取余2 student_age_inline: type: INLINE props: algorithm-expression: ds$-{age % 2} # 分表策略根据classid取余2 student_class_id_inline: type: INLINE props: algorithm-expression: student$-{class_id % 2} key-generators: # 配置主键生成算法-雪花算法 snowflake: type: SNOWFLAKE配置逻辑梳理只抄配置不如理解配置逻辑可选手续配置SS的运行模式不配置就默认单机可选SS的运行配置必选数据源配置 datasource:告诉SS数据库信息声明数据源别名分别给每个数据源声明连接池驱动地址账号密码等必选配置分库分表测试rules:指定要分库分表的表名配置数据节点告诉SS哪些库哪些表中有这张表的数据配置分库/分表策略告诉SS如何分配这张表的数据分库建分库策略必选配置分库/分表策略三、问题记录① 使用 Spring Boot 2.x 集成 ShardingSphere 时配置文件中的属性设置不生效以及 “Inline sharding algorithm expression cannot be null”异常解决方法来自官方FAQhttps://shardingsphere.apache.org/document/5.2.1/cn/faq/需要特别注意Spring Boot 2.x 环境下配置文件的属性名称约束为仅允许小写字母、数字和短横线即[a-z][0-9]和-。 原因如下: Spring Boot 2.x 环境下ShardingSphere 通过 Binder 来绑定配置文件属性名称不规范如驼峰或下划线等会导致属性设置不生效从而校验属性值时抛出NullPointerException异常。参考以下错误示例下划线示例database_inline驼峰示例databaseInline我在配置过程中也有遇到这个问题通过将shardingsphere-jdbc-core-spring-boot-starter依赖从版本5.0.0升级到5.2.1即可解决遇到类似的问题两种解决方案都可以考虑。② …getRowIdLifetime() 方法尚未被实作。问题记录和解决.报错信息如下AI建议这个错误表明你正在使用的PostgreSQL JDBC驱动版本不支持getRowIdLifetime()方法。这个方法是在JDBC 4.1规范中引入的如果你的驱动版本较旧可能不会实现这个方法。解决方案没找到合适的解决方案提升pgsql驱动到最高版本也无解阅读源码后发现这个方法没有实现已经被代码低层catch查阅官方git的议题org.postgresql.jdbc.PgDatabaseMetaData.getRowIdLifetime() 方法尚未被实作也可以找到作者的回复“It doesn’t matter”也就是其实不影响使用PS我之前使用3.0.0版本的SS其实没有这个问题不过不影响使用就算了吧反正It doesn’t matter