Kafka: Get broker host from ZooKeeper

前端 未结 5 1040
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 13:32

For particular reasons I need to use both - ConsumerGroup (a.k.a. high-level consumer) and SimpleConsumer (a.k.a. low-level consumer) to read from

5条回答
  •  天涯浪人
    2020-12-02 14:02

     public KafkaProducer(String zookeeperAddress, String topic) throws IOException,
            KeeperException, InterruptedException {
    
        this.zookeeperAddress = zookeeperAddress;
        this.topic = topic;
    
        ZooKeeper zk = new ZooKeeper(zookeeperAddress, 10000, null);
        List brokerList = new ArrayList();
    
        List ids = zk.getChildren("/brokers/ids", false);
        for (String id : ids) {
            String brokerInfoString = new String(zk.getData("/brokers/ids/" + id, false, null));
            Broker broker = Broker.createBroker(Integer.valueOf(id), brokerInfoString);
            if (broker != null) {
                brokerList.add(broker.connectionString());
            }
        }
    
        props.put("serializer.class", KAFKA_STRING_ENCODER);
        props.put("metadata.broker.list", String.join(",", brokerList));
        producer = new Producer(new ProducerConfig(props));
    }
    

提交回复
热议问题