卡夫卡Java消费者从来没有收到任何消息
-
26-12-2019 - |
题
我试图安装一个基本Java消费者收到的消息,从卡夫卡的主题。我已经跟随样品在- https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Example -和这个代号:
package org.example.kafka.client;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
public class KafkaClientMain
{
private final ConsumerConnector consumer;
private final String topic;
private ExecutorService executor;
public KafkaClientMain(String a_zookeeper, String a_groupId, String a_topic)
{
this.consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig(a_zookeeper, a_groupId));
this.topic = a_topic;
}
private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) {
Properties props = new Properties();
props.put("zookeeper.connect", a_zookeeper);
props.put("group.id", a_groupId);
props.put("zookeeper.session.timeout.ms", "1000");
props.put("zookeeper.sync.time.ms", "1000");
props.put("auto.commit.interval.ms", "1000");
props.put("auto.offset.reset", "smallest");
return new ConsumerConfig(props);
}
public void shutdown() {
if (consumer != null) consumer.shutdown();
if (executor != null) executor.shutdown();
}
public void run(int a_numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(a_numThreads));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
System.out.println( "streams.size = " + streams.size() );
// now launch all the threads
//
executor = Executors.newFixedThreadPool(a_numThreads);
// now create an object to consume the messages
//
int threadNumber = 0;
for (final KafkaStream stream : streams) {
executor.submit(new ConsumerTest(stream, threadNumber));
threadNumber++;
}
}
public static void main(String[] args)
{
String zooKeeper = "ec2-whatever.compute-1.amazonaws.com:2181";
String groupId = "group1";
String topic = "test";
int threads = 1;
KafkaClientMain example = new KafkaClientMain(zooKeeper, groupId, topic);
example.run(threads);
}
}
和
package org.example.kafka.client;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
public class ConsumerTest implements Runnable
{
private KafkaStream m_stream;
private int m_threadNumber;
public ConsumerTest(KafkaStream a_stream, int a_threadNumber)
{
m_threadNumber = a_threadNumber;
m_stream = a_stream;
}
public void run()
{
System.out.println( "calling ConsumerTest.run()" );
ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
while (it.hasNext())
{
System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));
}
System.out.println("Shutting down Thread: " + m_threadNumber);
}
}
卡夫卡上运行的EC2主在的问题,我可以发送和接收信息的题目是"测试"使用kafka-console-producer.sh 和kafka-console-consumer.sh 工具。口2181是开放的,可以从计算机的消费者运行(并因此是9092良好的措施,但这似乎并没有对帮助或者)。
不幸的是,我从来没有收到任何消息在我的消费者当我运行这一点。既不现有信息的主题,也不是新发送消息,我发送的使用kafka-console-producer.sh,而消费者的运行。
这是使用卡夫卡0.8.1.1上运行CentOS6.4 64,使用们可根据最新的技术提供一些1.7.0_65.
编辑:总的来说,在消费者节目开始时,我看见这个动物园管理员输出:
[2014-08-01 15:56:38,045] INFO Accepted socket connection from /98.101.159.194:24218 (org.apache.zookeeper.server.NIOServerCnxn)
[2014-08-01 15:56:38,049] INFO Client attempting to establish new session at /98.101.159.194:24218 (org.apache.zookeeper.server.NIOServerCnxn)
[2014-08-01 15:56:38,053] INFO Established session 0x1478e963fb30008 with negotiated timeout 6000 for client /98.101.159.194:24218 (org.apache.zookeeper.server.NIOServerCnxn)
任何想法可能是什么会在这?任何和所有帮助非常赞赏。
解决方案
回答这个我自己的子孙后代,情况下,任何人在类似的问题。
问题是这样的:卡夫卡的经纪人和动物园管理员在一个EC2节点,以及消费者对我的笔记本电脑上运行。当连接到动物园管理员,客户越来越交给一个参考"ip-10-0-x-x。ec2。内部的",这不能解决(默认)从外EC2。这变得清楚一旦我正确配置log4j上的客户,所以我得到了所有的日志的信息。
的解决方法是只要放一条在我的/etc/主机文件,映射ec2内部主机名称的公路由的IP地址。
其他提示
你可以解决这个问题使用设置下列财产在服务器。性文件落在卡夫卡的配置文件夹
广告。主机。name=公共dns的Ec2服务器