简体   繁体   English

没有消费者连接时,Kafka经纪人可以保留消息吗?

[英]Can a Kafka broker retain messages while there are no consumers connected?

I am trying to build a pub/sub application and I am exploring the best tools out there. 我正在尝试构建发布/订阅应用程序,并且正在探索最好的工具。 I am currently looking at Kafka and have a little demo app already running. 我目前正在查看Kafka,并且已经有一个演示应用程序正在运行。 However, I am running into a conceptual issue. 但是,我遇到了一个概念性问题。

I have a producer ( Java code): 我有一个生产者( Java代码):

    String topicName = "MyTopic;
    String key = "MyKey";

    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092,localhost:9093");
    props.put("acks", "all");
    props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
    Producer<String, byte[]> producer = new KafkaProducer <String, byte[]>(props);

    byte[] data = <FROM ELSEWHERE>;
    ProducerRecord<String, byte[]> record = new ProducerRecord<String, byte[]>(topicName, key, data);

    try {
        RecordMetadata result = producer.send(record).get();
    }
    catch (Exception e) {
        // Nothing for now
    }
    producer.close();

When I start a consumer via the Kakfa command line tools: 当我通过Kakfa命令行工具启动使用者时:

kafka-console-consumer --bootstrap-server localhost:9092 --topic MyTopic

and then I execute the producer code, I see the data message show up on my consumer terminal. 然后执行生产者代码,我在用户终端上看到数据消息。

However , if I do not run the consumer prior executing the producer, the message appears "lost". 但是 ,如果我在执行生产者之前运行消费者,则消息显示为“丢失”。 When I start the consumer (after executing the producer), nothing appears in the consumer terminal. 当我启动使用者(执行生产者之后)时,使用者终端中什么也没有出现。

Does anyone know if it's possible to have the Kafka broker retain messages while there are no consumers connected? 没有人知道在没有消费者连接的情况下是否可以让Kafka经纪人保留消息吗? If so, how? 如果是这样,怎么办?

Append --from-beginning to the console consumer command to have it start consuming from the earliest offset. --from-beginning附加到控制台使用者命令,以使其从最早的偏移量开始消耗。 This is actually about the offset reset strategy which is controlled by config auto.offset.reset . 这实际上是关于由config auto.offset.reset控制的偏移量重置策略。 Here is what this config means: 这是此配置的含义:

What to do when there is no initial offset in Kafka or if the current offset does not exist any more on the server (eg because that data has been deleted): 当Kafka中没有初始偏移量或服务器上不再存在当前偏移量时(例如因为该数据已被删除),该怎么办:

earliest : automatically reset the offset to the earliest offset earliest :将偏移量自动重置为最早的偏移量

latest : automatically reset the offset to the latest offset latest :自动将偏移量重置为最新偏移量

none : throw exception to the consumer if no previous offset is found for the consumer's group anything else: throw exception to the consumer. none :如果未找到使用者组的先前偏移量,则向使用者引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM