[英]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.