You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Kafka应用创建Topic遇ReplicationFactor异常问题

Spring Boot Kafka 创建Topic时副本数配置问题

在Windows环境下使用基于ZooKeeper的传统Kafka,运行Spring Boot Kafka应用时,试图通过应用自动创建Topic,相关配置及遇到的问题如下:

配置类代码

@Configuration
public class KafkaConfig {

    @Bean
    NewTopic createTopic() {
        return TopicBuilder.name("product-created-events-topic")
                .partitions(3)
                .replicas(3)
                .configs(Map.of("min.insync.replicas", "2"))
                .build();
    }
}

应用配置文件(application.properties)

spring.application.name=Product-Microservice
server.port=0
server.kafka.producer.bootstrap-servers=localhost:9092, localhost:9094
spring-kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring-kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer

报错信息

此时Kafka服务器抛出如下错误:

[2024-06-17 09:45:17,355] INFO [Admin Manager on Broker 0]: Error processing create topic request CreatableTopic(name='product-created-events-topic', numPartitions=3, replicationFactor=3, assignments=[], configs=[CreateableTopicConfig(name='min.insync.replicas', value='2')]) (kafka.server.ZkAdminManager)
org.apache.kafka.common.errors.InvalidReplicationFactorException: Replication factor: 3 larger than available brokers: 1.

修改后的结果

将配置类中的replicas(3)改为replicas(1)后,Topic成功创建,但仅包含1个副本,详情如下:

Topic: product-created-events-topic     TopicId: ---- PartitionCount: 3       ReplicationFactor: 1    Configs: min.insync.replicas=2
        Topic: product-created-events-topic     Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: product-created-events-topic     Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: product-created-events-topic     Partition: 2    Leader: 0       Replicas: 0     Isr: 0

内容的提问来源于stack exchange,提问作者Nirav Parekh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 07:55:05