Spring Cloud Stream 4.0.0与Spring Boot 3集成PollableMessageSource报错
Spring Cloud Stream 4.0.0轮询消费者启动报错解决方案
问题描述
尝试在Spring Cloud Stream 4.0.0中实现轮询消费者,配置完成后启动程序出现以下报错:Parameter 0 of method poller required a bean of type 'org.springframework.cloud.stream.binder.PollableMessageSource' that could not be found
旧版本中通过@EnableBinding(PolledProcessor.class)可正常实现,但新版本配置后无法运行,当前配置如下:
application.properties配置
spring.cloud.stream.pollable-source=myDestination
Java代码
@Slf4j @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ApplicationRunner poller(PollableMessageSource destIn, MessageChannel destOut) { return args -> { log.info("polling"); }; } }
pom.xml依赖配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2022.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.1</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream</artifactId> </dependency> </dependencies> </project>
解决步骤
1. 添加具体消息中间件的Binder依赖
Spring Cloud Stream仅为核心框架,必须引入对应消息中间件的Binder依赖,否则无法创建PollableMessageSource实例。
以RabbitMQ为例,添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
若使用Kafka,添加:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-kafka</artifactId> </dependency>
2. 修正PollableMessageSource的注入逻辑
在Spring Cloud Stream 3.x+的函数式编程模型中,轮询源的Bean需与配置的pollable-source名称对应,通过@Qualifier明确指定注入目标:
修改后的Java代码:
@Slf4j @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ApplicationRunner poller(@Qualifier("myDestination") PollableMessageSource destIn) { return args -> { log.info("polling"); // 执行实际轮询操作 boolean messageReceived = destIn.poll(message -> { log.info("Received message content: {}", message.getPayload()); }); log.info("Message received status: {}", messageReceived); }; } }
3. 移除过时注解(若存在)
新版本已废弃@EnableBinding注解,确保代码中未使用该注解,完全采用函数式编程模型。
4. 验证配置一致性
确保spring.cloud.stream.pollable-source=myDestination中的名称,与@Qualifier指定的名称完全一致,避免因名称不匹配导致Bean无法找到。
内容的提问来源于stack exchange,提问作者user3908406
相关产品推荐
相关产品推荐

