Spring Kafka NewTopic TopicBuilder,--command-config选项?

问题描述

当我使用kafka-topics.bat创建主题时,我会这样做:

kafka-topics.bat --bootstrap-server%host%:%port%--create --topic %% t --partitions%partitions%-复制因子%replication_factor%--config max.message.bytes =%max_message_bytes%--config min.insync.replicas =%min_insync_replicas%--config tention.ms =%retention_ms%--command-config client.properties

我正在尝试使用2.3中引入的Spring Kafka TopicBuilder 转换上述内容。但是我不知道如何转换 command-config选项。有可能吗?

根据documentation,其余部分很简单:

@Bean
public NewTopic topic(){
    return TopicBuilder.name("topic-name")
        .partitions(x)
        .replicas(x)
        .config(TopicConfig.MAX_MESSAGE_BYTES_CONFIG,"xxx")
        .config(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG,"xxx")
        .config(TopicConfig.RETENTION_MS_CONFIG,"xxx")
        .build();
}

解决方法

终于找到了!如果可以帮助某人,请采取以下解决方案:

@Bean
public KafkaAdmin admin() {
    Map<String,Object> configs = new HashMap<>();
    configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServers);
    configs.put("security.protocol","SASL_PLAINTEXT");
    configs.put("sasl.mechanism","PLAIN");
    configs.put("sasl.jaas.config","org.apache.kafka.common.security.plain.PlainLoginModule required " + 
                                    "username=username " + 
                                    "password=password;");
    return new KafkaAdmin(configs);
}

干杯

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...