我在我的servlet上设置了一个kafka-consumer。我从我的kafka主题中读取并希望使用servlet的doGet方法打印最新值。
但是如果我发送了doGet,我就会得到一个" null"回来...
我不确定,但我发现另一篇文章,有人提到,这可能是因为没有与zookeeper服务器的连接。所以我试图解决这个问题,将zookeeper.connection
添加到消费者的属性中;即使它已经过时了。
希望有人可以就此提出建议。
我的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();
}
}}
编辑:来自&#34; localhost.log&#34;的最新日志文件:
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()
答案 0 :(得分:0)
我明白了:问题,为什么我没有收到任何消息就是bin
行。
我不得不改变&#34; localhost&#34;到我的VM的实际IP。这有点奇怪,因为我配置的每个其他属性(在apache风暴; kafka;另一个包含kafka生产者的servlet)与&#34; localhost&#34;一起运行良好。