Shardingsphere踩坑

博主在项目中升级ShardingSphere从4.x到5.x时遇到配置错误,具体表现为Inlineshardingalgorithmexpressioncannotbenull。通过阅读源码发现是由于shardingAlgorithmName字段不支持下划线导致。同时,文章提到了在ShardingSphere中整合Seata分布式事务的注意事项,需要在方法上添加@Transactional和@ShardingTransactionType注解,并手动配置事务管理器。尽管遇到了一些挑战,但最终成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用shardingsphere作为数据库中间件,之前用过4.x,发现现在最新版本是5.x,决定尝个鲜。按照官方文档配置后一直报以下错误。

Inline sharding algorithm expression cannot be null

配置完全是按照官方文档配的。

网上搜索半天也没有类似的文章,看来没法偷懒了,只能自己翻源码了!

经过读源码,找到shardingsphere读取配置的代码,

是在shardingsphere-sharding-spring-boot-starter-5.0.0-beta.jar中的
org.apache.shardingsphere.sharding.spring.boot.algorithm.ShardingAlgorithmProvidedBeanRegistry.class
调用它的父类
shardingsphere-jdbc-spring-boot-starter-infra-5.0.0-beta.jar中的
org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.class
中的registerBean()方法读取配置文件。

跟踪代码发现竟然是因为shardingAlgorithmName字段不支持下划线,可是官网例子里明明是下划线呀。

另一个问题是在shardingsphere中整合seata分布式事务,记住一定要在方法上同时加上普通事务注解@Transactional,这样分布式事务@ShardingTransactionType(TransactionType.BASE)才能生效。同时必须手工配置事务管理器,否则也不能生效。

@Configuration
@EnableTransactionManagement
public class TransactionConfig {
	
	@Bean
    public PlatformTransactionManager txManager(final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

也许是因为目前5.x还处于beta版本吧。从4.x到5.x配置有了一些变化,学习了半天,加上调试解决这个问题,一整天过去了,开源软件还是坑挺多的,技术人员苦呀。好在解决了,自己留个记录 ,也是希望对别人有所帮助。

<think>嗯,用户想要了解如何将Spring Cloud Alibaba框架整合Sharding、Druid和PostgreSQL,并且使用properties配置文件。首先,我需要确认用户的具体需求。Spring Cloud Alibaba通常用于微服务架构,而ShardingSphere可能是指分库分表或者读写分离,Druid是数据库连接池,PostgreSQL是数据库。用户可能是在搭建一个分布式系统,需要整合这些组件。 首先,我应该考虑各个组件的版本兼容性。Spring Boot、Spring Cloud Alibaba、ShardingSphere、Druid和PostgreSQL驱动之间需要版本匹配,否则可能会出现兼容性问题。比如,Spring Cloud Alibaba的版本需要与Spring Boot版本对应,ShardingSphere的版本也需要适配。 接下来,依赖配置。用户需要使用Maven或Gradle来管理依赖。需要添加Spring Cloud Alibaba的依赖,比如Nacos用于服务发现和配置管理,Sentinel用于流量控制。然后是ShardingSphere的JDBC驱动,Druid的starter,以及PostgreSQL的驱动。需要确保这些依赖的版本正确,并且没有冲突。 然后是配置文件部分,用户要求使用properties格式。需要配置数据源,ShardingSphere的规则,比如分片策略、分片键、分片算法。Druid的连接池参数,比如初始大小、最大活跃连接数等。同时,可能还需要配置Spring Cloud Alibaba的相关组件,比如Nacos的地址,Sentinel的配置。 在数据源配置方面,使用ShardingSphere的数据源,并指定实际的数据库节点。比如,配置两个数据源ds0和ds1,分别指向不同的PostgreSQL实例。然后定义分片策略,比如根据用户ID取模分片。同时,主键生成策略可能需要配置,比如使用Snowflake算法。 另外,Druid的连接池配置需要正确设置,确保连接池的参数合理,避免资源泄露或性能问题。例如,initialSize、minIdle、maxActive等参数。 可能还需要注意事务管理,ShardingSphereSpring的事务整合是否会有问题,是否需要额外配置。此外,分布式事务可能需要使用Seata,但用户没有提到,所以可能暂时不考虑。 测试部分,用户可能需要编写简单的CRUD操作来验证分片是否生效,数据是否正确地分布在不同的数据源中。同时,检查Druid监控是否正常,连接池是否工作。 可能遇到的包括版本冲突、分片策略配置错误、数据源名称不匹配、Druid配置未生效等。需要提醒用户注意检查日志,确保所有配置正确加载,并且组件初始化成功。 总结步骤应该是:创建项目,配置依赖,编写properties配置文件,定义数据源和分片规则,配置Druid,配置Spring Cloud Alibaba组件,编写测试代码验证。以下是</think>关于整合 **Spring Cloud Alibaba**、**ShardingSphere-JDBC**(分库分表)、**Druid**(连接池)与 **PostgreSQL** 的步骤说明,基于 `properties` 配置文件格式。 --- ### 1. 环境依赖 确保 `pom.xml` 中包含以下核心依赖(版本需匹配): ```xml <!-- Spring Cloud Alibaba --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2022.0.0.0-RC2</version> </dependency> <!-- ShardingSphere-JDBC --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.3.2</version> </dependency> <!-- Druid 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.18</version> </dependency> <!-- PostgreSQL 驱动 --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> </dependency> ``` --- ### 2. 配置文件 (`application.properties`) ```properties # ========== Spring Cloud Alibaba Nacos ========== spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # ========== ShardingSphere 数据源配置 ========== # 定义数据源名称(多个用逗号分隔) spring.shardingsphere.datasource.names=ds0,ds1 # 配置数据源 ds0 spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds0.driver-class-name=org.postgresql.Driver spring.shardingsphere.datasource.ds0.url=jdbc:postgresql://localhost:5432/db0 spring.shardingsphere.datasource.ds0.username=postgres spring.shardingsphere.datasource.ds0.password=123456 # 配置数据源 ds1 spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds1.driver-class-name=org.postgresql.Driver spring.shardingsphere.datasource.ds1.url=jdbc:postgresql://localhost:5432/db1 spring.shardingsphere.datasource.ds1.username=postgres spring.shardingsphere.datasource.ds1.password=123456 # ========== Sharding 分片规则 ========== # 分表策略:user 表按 user_id 取模分片 spring.shardingsphere.rules.sharding.tables.user.actual-data-nodes=ds$->{0..1}.user_$->{0..1} spring.shardingsphere.rules.sharding.tables.user.table-strategy.standard.sharding-column=user_id spring.shardingsphere.rules.sharding.tables.user.table-strategy.standard.sharding-algorithm-name=user-table-inline # 分库策略:按 user_id 取模分库 spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds$->{user_id % 2} # 分表算法配置 spring.shardingsphere.rules.sharding.sharding-algorithms.user-table-inline.type=INLINE spring.shardingsphere.rules.sharding.sharding-algorithms.user-table-inline.props.algorithm-expression=user_$->{user_id % 2} # 分布式主键生成策略 spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123 # ========== Druid 连接池配置 ========== # 连接池通用参数 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.validation-query=SELECT 1 # 监控页面配置(可选) spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* ``` --- ### 3. 关键配置说明 1. **ShardingSphere-JDBC**: - `actual-data-nodes`:定义物理表分布,如 `ds0.user_0, ds1.user_1`。 - 分片策略:通过 `sharding-column` 指定分片键,`algorithm-expression` 定义分片算法(支持 Groovy 表达式)。 - 分布式主键:使用 Snowflake 算法生成唯一 ID。 2. **Druid**: - 通过 `spring.datasource.druid.*` 配置连接池参数。 - 启用监控页面(访问 `/druid` 查看统计信息)。 3. **PostgreSQL**: - 确保数据库驱动与 URL 格式正确(如 `jdbc:postgresql://host:port/dbname`)。 --- ### 4. 验证与调试 1. **启动应用**:检查日志中是否成功加载 ShardingSphere 和 Druid 配置。 2. **监控页面**:访问 `http://localhost:port/druid` 查看 Druid 连接池状态。 3. **分片测试**:插入数据时观察数据是否按规则分布到不同库表。 --- ### 5. 注意事项 - **版本兼容性**:确保 Spring Boot、Spring Cloud Alibaba、ShardingSphere 和 Druid 版本匹配。 - **事务管理**:如需跨库事务,可结合 `Seata`(Spring Cloud Alibaba 生态的分布式事务组件)。 - **SQL 兼容性**:验证 PostgreSQL 语法是否与 ShardingSphere 的路由规则兼容。 如有具体需求(如读写分离、加密等),可进一步调整 ShardingSphere 配置。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值