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

查看:841
本文介绍了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 -bootstrap-server 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天全站免登陆