如何在使用 Spring 创建期间配置 kafka 主题保留策略?

Rac*_*R K 8 java spring spring-mvc apache-kafka spring-boot

我需要在创建期间配置特定主题的保留策略。我试图寻找解决方案,我只能找到如下命令级别的更改命令

./bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic my-topic --config reserved.ms=1680000

有人可以让我知道在创建过程中配置它的方法,例如 spring-mvc 中的 xml 或属性文件配置。

Ser*_*mar 10

Spring Kafka 允许您通过@Bean在应用程序上下文中声明s 来创建新主题。这将需要KafkaAdmin应用程序上下文中的类型 bean,如果使用 Spring Boot,它将自动创建。您可以按如下方式定义您的主题:

@Bean
public NewTopic myTopic() {
    return TopicBuilder.name("my-topic")
            .partitions(4)
            .replicas(3)
            .config(TopicConfig.RETENTION_MS_CONFIG, "1680000")
            .build();
}
Run Code Online (Sandbox Code Playgroud)

如果您不使用 Spring Boot,则还必须定义KafkaAdminbean:

@Bean
public KafkaAdmin admin() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
    return new KafkaAdmin(configs);
}
Run Code Online (Sandbox Code Playgroud)

如果要编辑现有主题的配置,则必须使用AdminClient,这是retention.ms在主题级别更改 的代码段:

Map<String, Object> config = new HashMap<>();                
config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
                         
AdminClient client = AdminClient.create(config);
                         
ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, "new-topic");
            
// Update the retention.ms value
ConfigEntry retentionEntry = new ConfigEntry(TopicConfig.RETENTION_MS_CONFIG, "1680000");
Map<ConfigResource, Config> updateConfig = new HashMap<>();
updateConfig.put(resource, new Config(Collections.singleton(retentionEntry)));

AlterConfigOp op = new AlterConfigOp(retentionEntry, AlterConfigOp.OpType.SET);
Map<ConfigResource, Collection<AlterConfigOp>> configs = new HashMap<>(1);
configs.put(resource, Arrays.asList(op));

AlterConfigsResult alterConfigsResult = client.incrementalAlterConfigs(configs);
        alterConfigsResult.all();
Run Code Online (Sandbox Code Playgroud)

可以使用这种@PostConstruct接收NewTopicbean 的方法自动设置配置。


    @Autowired
    private Set<NewTopic> topics;

    @PostConstruct
    public void reconfigureTopics() throws ExecutionException, InterruptedException {

        try (final AdminClient adminClient = AdminClient.create(Map.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServers))) {
            adminClient.incrementalAlterConfigs(topics.stream()
                .filter(topic -> topic.configs() != null)
                .collect(Collectors.toMap(
                    topic -> new ConfigResource(ConfigResource.Type.TOPIC, topic.name()),
                    topic -> topic.configs().entrySet()
                        .stream()
                        .map(e -> new ConfigEntry(e.getKey(), e.getValue()))
                        .peek(ce -> log.debug("configuring {} {} = {}", topic.name(), ce.name(), ce.value()))
                        .map(ce -> new AlterConfigOp(ce, AlterConfigOp.OpType.SET))
                        .collect(Collectors.toList())
                )))
                .all()
                .get();
        }

    }
Run Code Online (Sandbox Code Playgroud)