3
的Kafka console consumer似乎需要你指定的ZooKeeper實例連接:Kafka連接何時需要ZooKeeper配置?
./kafka-console-consumer.sh --zookeeper myzk.example.com:2181 --topic mytopic
但它顯然是可以直接通過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節點的情況下運行消費者?如果不是,爲什麼?