2017-03-28 43 views
0

我一直在尋找java客戶端(卡夫卡消費者)消費來自多個經紀人的消息。請諮詢如何在java中編寫Kafka Consumer Client以使用來自多個代理的消息?

下面是使用簡單分區程序將消息發佈到多個代理的代碼。

主題使用複製因子「2」和分區「3」創建。

public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) 
{ 
    List<PartitionInfo> partitions = cluster.partitionsForTopic(topic); 
    int numPartitions = partitions.size(); 
    logger.info("Number of Partitions " + numPartitions); 
    if (keyBytes == null) 
    { 
     int nextValue = counter.getAndIncrement(); 
     List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic); 
     if (availablePartitions.size() > 0) 
     { 
      int part = toPositive(nextValue) % availablePartitions.size(); 
      int selectedPartition = availablePartitions.get(part).partition(); 
      logger.info("Selected partition is " + selectedPartition); 
      return selectedPartition; 
     } 
     else 
     { 
      // no partitions are available, give a non-available partition 
      return toPositive(nextValue) % numPartitions; 
     } 
    } 
    else 
    { 
     // hash the keyBytes to choose a partition 
     return toPositive(Utils.murmur2(keyBytes)) % numPartitions; 
    } 

} 


public void publishMessage(String message , String topic) 
{ 
    Producer<String, String> producer = null; 
    try 
    { 
    producer = new KafkaProducer<>(producerConfigs()); 
    logger.info("Topic to publish the message --" + this.topic); 
    for(int i =0 ; i < 10 ; i++) 
    { 
    producer.send(new ProducerRecord<String, String>(this.topic, message)); 
    logger.info("Message Published Successfully"); 
    } 
    } 
    catch(Exception e) 
    { 
     logger.error("Exception Occured " + e.getMessage()) ; 
    } 
    finally 
    { 
    producer.close(); 
    } 
} 

public Map<String, Object> producerConfigs() 
{ 
    loadPropertyFile(); 
    Map<String, Object> propsMap = new HashMap<>(); 
    propsMap.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList); 
    propsMap.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); 
    propsMap.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); 
    propsMap.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, SimplePartitioner.class); 
    propsMap.put(ProducerConfig.ACKS_CONFIG, "1"); 
    return propsMap; 
} 

public Map<String, Object> consumerConfigs() { 
    Map<String, Object> propsMap = new HashMap<>(); 
    System.out.println("properties.getBootstrap()" + properties.getBootstrap()); 
    propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrap()); 
    propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); 
    propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, properties.getAutocommit()); 
    propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, properties.getTimeout()); 
    propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); 
    propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); 
    propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, properties.getGroupid()); 
    propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, properties.getAutooffset()); 
    return propsMap; 
} 

@KafkaListener(id = "ID1", topics = "${config.topic}", group = "${config.groupid}") 
public void listen(ConsumerRecord<?, ?> record) 
{ 
    logger.info("Message Consumed " + record); 
    logger.info("Partition From which Record is Received " + record.partition()); 
    this.message = record.value().toString(); 
} 

bootstrap.servers = [本地主機:9092,本地主機:9093,本地主機:9094]

回答

0

如果使用普通的Java用戶,它會自動從多個代理讀取。沒有你需要編寫的特殊代碼。只需訂閱您想要消費的主題,消費者就可以自動連接到相應的經紀商。您只提供一個「單一入口點」經紀人 - 客戶端會自動找出集羣的所有其他經紀人。

+0

感謝您的回覆。我用簡單的卡夫卡消費者嘗試過。但是,消息不消耗。我編輯了我的問題以發佈消費者邏輯。 – Gopi

+0

我不知道'@ KafkaListener'的作用......也許這有助於:http://docs.confluent.io/current/clients/consumer.html –

+0

謝謝你看看。 – Gopi

相關問題