Kafka 连接何时需要 ZooKeeper 配置? [英] When does Kafka connection require ZooKeeper config?

查看:69
本文介绍了Kafka 连接何时需要 ZooKeeper 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.com: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.com: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.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆