简体   繁体   English

为什么这个使用者(在servlet上运行)为什么没有收到来自kafka主题的消息?

[英]Why does this consumer (running on a servlet) not receive any messages from the kafka topic?

I've setted up an kafka-consumer on my servlet. 我已经在我的servlet上设置了kafka-consumer。 I read from my kafka topic and want to print the latest value using the doGet-method of the servlet. 我从kafka主题中阅读,并希望使用servlet的doGet方法打印最新值。

But if I send the doGet I just get a "null" back... 但是,如果我发送doGet,我将得到一个“空”的回信...

I'm not sure, but I found another post, where somebody mentioned, that this might be because there is no connection to the zookeeper servers. 我不确定,但是我发现了另一篇文章,有人提到这可能是因为与Zookeeper服务器没有连接。 So I tried to fix this adding the zookeeper.connection to the properties of the consumer; 因此,我尝试解决此问题,将zookeeper.connection添加到使用者的属性中; even if it's outdated. 即使已经过时。

Hopefully someone could give me advice about this. 希望有人可以给我一些建议。

Code of my Servlet: 我的Servlet代码:

public class KafkaServlet extends HttpServlet implements Runnable {

public String kafkaMessage;
private Properties props;
private KafkaConsumer<String, String> consumer;
Thread Trans;

/**
*   setting up the properties and other consumer using the constructor of KafkaServlet
*/
public KafkaServlet() {
    props = new Properties();
    props.put("bootstrap.servers", "localhost:9092"); //replace 'localhost' with the actual IP to get it working.
    props.put("zookeeper.connect", "localhost:2181");
    props.put("auto.offset.reset", "earliest");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    consumer = new KafkaConsumer<>(props);
    consumer.subscribe(Arrays.asList("kafkaTopic"));
}

/**
*   setup the thread and run it using the init-method
*/
@Override
public void init (ServletConfig config) throws ServletException {
    super.init(config);
    Trans = new Thread(this);
    Trans.setPriority(Thread.MIN_PRIORITY);
    Trans.start();
}

/**
*   implement the doPost-Method if we want to use it 
*/
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    //empty -- nothing to post here
}

/**
*   doGet will get print out our returned message from kafka
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();
String title = "Reading Parameters from kafkaTopic";

out.println("<HTML>" +
            "<BODY>\n" +
            "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
            "<UL>\n" +
            "  <LI>MESSAGE: "
            + kafkaMessage + "\n" +
            "</UL>\n" +
            "</BODY></HTML>");
}

/**
*   thread, which grabs the messages from kafka and stores the latest one in "kafkaMessage"
*/
@Override
public void run() {
    try {
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                //KafkaMessage.setMessage(record.value());
                kafkaMessage = record.value();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        consumer.close();
    }
}}

EDIT: Latest logfiles from "localhost.log": 编辑:来自“ localhost.log”的最新日志文件:

19-Dec-2016 13:42:42.773 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log SessionListener: contextDestroyed() 19-Dec-2016 13:42:42.774 INFO [localhost-startStop-2] org.apache.catalina.core.ApplicationContext.log ContextListener: contextDestroyed() 19-Dec-2016 13:42:54.742 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized() 19-Dec-2016 13:42:54.745 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()

I figured it out: the problem, why I did not receive any messages was the line props.put("bootstrap.servers", "localhost:9092"); 我发现了:问题,为​​什么我没有收到任何消息,是props.put("bootstrap.servers", "localhost:9092"); .

I had to change the "localhost" to my actual IP of my VM. 我必须将“ localhost”更改为虚拟机的实际IP。 This is kinda strange, because every other property I configured (on apache storm; kafka; another servlet which contains an kafka producer) was working well with "localhost". 这有点奇怪,因为我配置的所有其他属性(在Apache风暴中; kafka;另一个包含kafka生产者的servlet)在“ localhost”下运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消费者不读取来自 Kafka 主题的消息(Akka Stream Kafka) - Consumer does not read messages from the Kafka topic (Akka Stream Kafka) Kafka Spark Streaming Consumer是否不会从Kafka Console Producer接收任何消息? - Kafka Spark Streaming Consumer will not receive any messages from Kafka Console Producer? 重启Kafka生产者/消费者后,消费者未收到消息 - Consumer does not receive messages after kafka producer/consumer restart 运行在不同计算机上的Kafka用户组可以接收唯一消息吗? - Can Kafka consumer group running on different machines receive unique messages? Kafka服务器包含消息,但使用者无法接收任何消息 - Kafka server contains messages but consumer cannot receive any 从特定主题中检索 Kafka 消费者的最后 n 条消息 - Retrieve last n messages of Kafka consumer from a particular topic 无法从 Kafka 中的消费者向死信主题发送消息 - Unable to send messages to dead letter topic from consumer in Kafka 来自主题的 Kafka 消息在消费者重启后被重放 - Kafka messages from topic are being replayed after consumer restart 如果另一个运行,关于分区主题的kafka使用者无法启动 - kafka consumer on partitioned topic does not start if another running Kafka 消费者不从队列中读取消息 - Kafka consumer does not read messages from the queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM