消息传递—kafka java使用者从不接收任何消息

ffscu2ro  于 2021-06-07  发布在  Kafka
关注(0)|答案(2)|浏览(447)

我正在尝试设置一个基本的java使用者来接收来自kafka主题的消息。我在-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);
    }
}

kafka正在讨论的ec2主机上运行,我可以使用kafka-console-producer.sh和kafka-console-consumer.sh工具发送和接收主题为“test”的消息。端口2181是开放的,可以从用户运行的机器上获得(9092也是如此,但这似乎也没有帮助)。
不幸的是,当我运行这个时,我从未在我的消费者中收到任何消息。既不是关于主题的现有消息,也不是我在使用者运行时使用kafka-console-producer.sh发送的新发送的消息。
这是使用在centos 6.4 x64上运行的kafka 0.8.1.1,使用openjdk 1.7.0@65。
编辑:fwiw,当消费者程序启动时,我看到这个zookeeper输出:

[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)

你知道这是怎么回事吗?非常感谢您的帮助。

ogsagwnx

ogsagwnx1#

您可以通过在kafka config文件夹下的server.properties文件中设置以下属性来解决此问题
advised.host.name=ec2服务器的公共dns

scyqe7ek

scyqe7ek2#

为子孙后代回答这个问题,以防其他人遇到类似的问题。
问题是:kafka代理和zookeeper在ec2节点上,而消费者在本地运行的笔记本电脑上。当连接到zookeeper时,客户机得到了一个对“ip-10-0-x-x.ec2.internal”的引用,该引用(默认情况下)不会从ec2外部解析。一旦我在客户机上正确配置了log4j,这样我就可以得到所有的日志消息。
解决方法是在my/etc/hosts文件中放入一个条目,将ec2内部主机名Map到可公开路由的ip地址。

相关问题