我正在尝试使用servlet连接kafkaproducer,并在eclipse中设置我的项目。但当我运行kafkaproducer时,我得到了一个例外:
org.apache.catalina.core.standardwrappervalve invoke severe:servlet[simple\u kafka.simpleproducer]的servlet.service(),路径为[/kafka\u web]引发异常[servlet execution引发异常],根本原因为java.lang.classnotfoundexception:org.apache.kafka.clients.producer.kafkaproducerorg.apache.catalina.loader.webappclassloaderbase.loadeclass(webappclassloaderbase。java:1291)在org.apache.catalina.loader.webappclassloaderbase.loadeclass(webappclassloaderbase。java:1119)在simple\u kafka.simpleproducer.dopost(simpleproducer。java:56)在javax.servlet.http.httpservlet.service(httpservlet。java:661)在javax.servlet.http.httpservlet.service(httpservlet)。java:742)位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:231)在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:166)在org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter。java:52)在org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:193)在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:166)在org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve。java:199)在org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve。java:96)在org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase。java:478)在org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve。java:140)在org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve。java:81)在org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve。java:650)在org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve。java:87)在org.apache.catalina.connector.coyoteadapter.service(coyoteadapter。java:342)在org.apache.coyote.http11.http11processor.service(http11processor。java:803)在org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight。java:66)在org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol。java:868)位于org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint)。java:1459)在org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase。java:49)在java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor。java:1149)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor。java:624)在org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread。java:61)在java.lang.thread.run(线程。java:748)
我的kafkaproducerservlet如下:
package simple_kafka;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
//import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
/**
* Servlet implementation class SimpleProducer
*/
@WebServlet("/producer")
public class SimpleProducer extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SimpleProducer() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
response.getWriter().append("Served at: ").append(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG);
String topicName = "ReplicaTopic";
String key = "Key1";
String value = "Value-1";
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.59:9092,192.168.1.59:9093");
props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer <String, String>(props);
try {
ProducerRecord<String, String> record = new ProducerRecord<>(topicName,key,value);
producer.send(record);
producer.close();
}
catch(Exception e) {
response.getWriter().append("Exception at: ").append((CharSequence) e);
}
// response.sendRedirect("/consumer");
}
}
我在项目属性的buildpath中包含了以下jar:
Kafka2.12-0.11.0.1.jar
Kafka客户端-0.11.0.1.jar
slf4j-api-1.7.25.jar
1条答案
按热度按时间67up9zun1#
经过一天的搜索,我发现您需要包含jar files-webinf->lib文件夹,并且您需要从eclipse中删除您的服务器并重新配置它。做了那件事之后,我就可以经营Kafka制作人了。