kafka 0.9.0.1 java,Kafka 0.9.0.1 Java消费者陷入awaitMetadataUpdate()

I'm trying to get a simple Kafka Consumer to work using the Java API v0.9.0.1. The kafka server I'm using is a docker container, also running version 0.9.0.1. Below is the consumer code:

public class Consumer {

public static void main(String[] args) throws IOException {

KafkaConsumer consumer;

try (InputStream props = Resources.getResource("consumer.props").openStream()) {

Properties properties = new Properties();

properties.load(props);

consumer = new KafkaConsumer<>(properties);

}

consumer.subscribe(Arrays.asList("messages"));

try {

while (true) {

ConsumerRecords records = consumer.poll(100);

for (ConsumerRecord record : records)

System.out.println("Message received: " + record.value());

}

}catch(WakeupException ex){

System.out.println("Exception caught " + ex.getMessage());

}finally{

consumer.close();

System.out.println("After closing KafkaConsumer");

}

}

}

However, when starting the consumer, it calls the poll(100) method above and never returns. Debugging, it looks like it gets stuck running the following method in org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient forever:

public void awaitMetadataUpdate() {

int version = this.metadata.requestUpdate();

do {

this.poll(9223372036854775807L);

} while(this.metadata.version() == version);

}

(both version and this.metadata.version() always seem to be == 2). Additionally, though it throws no errors, messages from my java producer never seen to make it to the queue. I've verified that using the command line kafka tools, I can both send and receive messages from the queue.

Anyone have any clue what's going on here?

解决方案

In case this helps anyone else with similar issues, the solution for me was to set the following environment variables:

ADVERTISED_HOST=localhost

ADVERTISED_PORT=9092

(of course, the values here may change to suit your installation)

Apparently the command line consumer and producer scripts can manage to find and communicate with the broker correctly without these env variables being set, but the Java API implementations cannot. No errors are thrown either, just an infinite loop on the first poll when it tries to update the metadata.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值