Kafka SimpleConsumer cannot connect to zookeeper :

2019-08-10 03:29发布

问题:

I'm trying to connect to a remote kafka zookeeper (zookeeperhost:1234) via SimpleConsumer, and pulling metadata for the topics that I've specified. However, I get this error:

RunnerMain failed :java.io.EOFException: Received -1 when reading from channel, socket has likely been closed.

I ran telnet zookeeperhost 1234 and got the following:

Trying <ip addr of host>...
Connected to zookeeperhost.somedomain.com.
Escape character is '^]'.

So it looks like I am able to reach the service, there is no security/permission issue.

Here is my code:

SimpleConsumer consumer = new SimpleConsumer ("zookeeperhost", 1234, 100000, 64 * 1024, "leader_lookup");
List<String> topics = Collections.singletonList("some_event");
TopicMetadataRequest req = new TopicMetadataRequest(topics);
TopicMetadataResponse resp = consumer.send(req); //this is the line that is causing the error
List<TopicMetadata> metaData = resp.topicsMetadata();

回答1:

You need to connect SimpleConsumer to a kafka leader broker (port 9092 by default) not a zookeeper. Check this link for the details how to use SimpleConsumer: https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+SimpleConsumer+Example