spring-boot - StreamsException:无法初始化状态,如果 Kafka Streams 的多个实例在同一个状态目录中运行,可能会发生这种情况
问题描述
这是关于在生产中升级现有代码库,它使用从 kafka-clients、kafka-streams、spring-kafka 2.4.0 到 2.6.x 的窗口,以及将 spring-boot-starter-parent 从 2.2.2.RELEASE 升级到 2.3.x因为 2.2 与 kafka-streams 2.6 不兼容。
现有代码在旧版本(2.4.0、2.2 春季版本)中提到了以下这些 bean:
@Bean("DataCompressionCustomTopology")
public Topology customTopology(@Qualifier("CustomFactoryBean") StreamsBuilder streamsBuilder) {
//Your topology code
return streamsBuilder.build();
}
@Bean("GenericKafkaStreams")
public KafkaStreams kStream() {
//Your kafka streams code
return kafkaStreams;
}
现在升级 kafka 流,kafka 客户端到 2.6.2 和 spring kafka 到 2.6.x 后,观察到以下异常:
2021-05-13 12:33:51.954 [Persistence-Realtime-Transformation] [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'CustomFactoryBean'; nested exception is org.springframework.kafka.KafkaException: Could not start stream: ; nested exception is org.apache.kafka.streams.errors.StreamsException: Unable to initialize state, this can happen if multiple instances of Kafka Streams are running in the same state directory
解决方案
如果您的 Spring Cloud Streams Kafka Streams Binder 3.0 风格的应用程序中有一个复杂的 Kafka Streams 拓扑,您可能需要为不同的功能指定不同的应用程序 ID,如下所示:
spring.cloud.stream.function.definition: myFirstStream;mySecondStream
...
spring.cloud.stream.kafka.streams:
binder:
functions:
myFirstStream:
applicationId: app-id-1
mySecondStream:
applicationId: app-id-2
推荐阅读
- awk - 提取单行字符串中两个符号之间的字符串
- google-cloud-platform - 打开 GCP 笔记本时,浏览器内存消耗不断增加
- python - 是否可以让 python 脚本模拟对 AutoHotKey 的按键?
- python - 用放置在附加列中的连续序列号标记数据框中的所有行
- mysql - SQL通过从同一个表中选择插入多个值和父ID
- node.js - 我想创建不和谐的自动提醒机器人,但是如果用户同时在自动提醒列表中写入 2 条消息,则只会出现 1 条自动提醒
- cakephp - 调用未定义的方法 Utils\Model\Behavior\WhoDidItBehavior::getConfig()
- python-3.x - 我尝试制作图形计算器但收到此错误
- api - 我是一名 Python 新手,尝试使用 Open Weather 的免费 API,但收到 {'cod': '404', 'message': 'Internal error'}。我怎样才能解决这个问题?
- node.js - 带有编译和 nodemon 问题的 Typescript npm 脚本