9

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

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

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

4

3 回答 3

13

@BeanSpring Kafka 允许您通过在应用程序上下文中声明 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();
}

如果您不使用 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);
}

如果要编辑现有主题的配置,则必须使用,这是在主题级别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();

@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();
        }

    }
于 2019-06-11T14:15:38.563 回答
1

我想您可以为此使用管理客户端(https://kafka.apache.org/22/javadoc/index.html?org/apache/kafka/clients/admin/AdminClient.html)。您可以在应用程序中创建管理客户端实例,并使用 create 或 alter topic 命令来操作主题配置,包括保留。

于 2019-06-11T09:58:46.567 回答
0

要使用指定保留时间以编程方式创建主题AdminClient,请执行以下操作:

NewTopic topic = new NewTopic(topicName, numPartitions, replicationFactor);
topic.configs(Map.of(TopicConfig.RETENTION_MS_CONFIG, retentionMs.toString()));
adminClient.createTopics(List.of(topic));
于 2020-06-01T08:50:07.527 回答