【发布时间】:2021-07-09 01:56:21
【问题描述】:
这不是一个大问题,但我很好奇一些额外的流消费者来自哪里,如果这是我可以更改的设置。
我有一个针对本地 Kafka 代理的非常简单的 Spring Cloud Stream Consumer 设置。这是spring配置
spring:
cloud:
stream:
bindings:
consumer-in-0:
destination: test-topic
group: test-group
还有消费者类本身:
@Bean
Consumer<Message<String>> consumer() {
return message -> System.out.println("Got it: " + message.getPayload());
}
当我运行应用程序时,我可以看到在输出中创建了 3 个消费者。但是当我检查本地代理中的消费者组成员时,它总是只是一个消费者,它总是是第二个创建的消费者(即客户端 ID test-group-2)
为了清楚起见,我使用的是 Spring Boot 版本 2.3.4.RELEASE 和云依赖版本 Hoxton.SR10。
这是 pom 中的依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependencies>
为什么我会得到 3 个消费者?为什么第二个是唯一真正听 Kafka 主题的?
【问题讨论】:
-
我认为您的意思是创建了另一个组
test-group-2,不是吗?而不是另一个话题? -
没有创建多个主题。只有一个主题
test-topic当spring 库创建消费者时,它使用一个消费者ID 来创建它们,该消费者ID 是组名+spring 添加的唯一标识消费者的内容。所以它创建了 3:test-group-1、test-group-2、test-group-3。仍然只有一个组,只有 3 个消费者。不过,消费者 ID 为test-group-2的消费者是唯一真正在听该主题的消费者。
标签: spring-boot spring-kafka spring-cloud-stream