Kafka 连接何时需要 ZooKeeper 配置?

编程入门 行业动态 更新时间:2024-10-20 05:32:09
本文介绍了Kafka 连接何时需要 ZooKeeper 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

Kafka 控制台使用者似乎要求您指定要连接的 ZooKeeper 实例:

The Kafka console consumer seems to require you to specify a ZooKeeper instance to connect to:

./kafka-console-consumer.sh --zookeeper myzk.example:2181 --topic mytopic

但显然可以通过 Java API 直接连接到 Kafka 代理:

But it is clearly possible to connect to a Kafka broker directly via the Java API:

public class KafkaClient {
  public static void main(String[] args) {

    String topic = "mytopic";

    Properties props = new Properties();
    props.put("bootstrap.servers", "kafka.example:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("batch.size", 16384);
    props.put("linger.ms", 1);
    props.put("buffer.memory", 33554432);
    props.put("key.serializer", "org.apache.kafkamon.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafkamon.serialization.StringSerializer");

    Producer<String, String> producer = new KafkaProducer<>(props);

    Callback cb = new Callback() {
        @Override
        void onCompletion(RecordMetadata rdata, Exception exc) {
            if(exc) {
                throw exc;
            }
        }
    }

    producer.send(new ProducerRecord<String, String>(topic, 'somekey', 'someval'), cb);
    producer.close();
  }
}

有没有办法在不指定ZK节点的情况下运行消费者?如果不是,为什么?

推荐答案

这取决于使用的是哪个版本的消费者 API.从最新的 Kafka 版本 0.10.1 开始,直接针对代理的新 API 是控制台消费者使用的默认值.0.10.1 之前的版本默认使用针对 Zookeeper 的旧 API,但可以通过指定如下参数将新的消费者 API 设置为控制台消费者:--new-consumer--使用命令引导服务器 someBroker:9092.

This depends on which version of the consumer API is being used. Starting with the latest Kafka release 0.10.1, the new API which directly targets the brokers is the default used by the console consumer. Versions prior to 0.10.1 default to the older API targeting Zookeeper but can be set to use the new consumer API for the console consumer by specifying parameters like: --new-consumer and --bootstrap-server someBroker:9092 with the command.

这篇关于Kafka 连接何时需要 ZooKeeper 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 17:41:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/943688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Kafka   ZooKeeper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!