使用Servlet

时间:2017-10-31 11:15:19

标签: apache-kafka kafka-producer-api apache-kafka-connect

我正在尝试使用servlet连接KafkaProducer并在eclipse中设置我的项目。但是当我运行KafkaProducer时,我得到一个例外:

  

org.apache.catalina.core.StandardWrapperValve调用SEVERE:   上下文中的servlet [simple_kafka.SimpleProducer]的Servlet.service()   与路径[/ kafka_web]抛出异常[Servlet执行扔了一个   异常]与根本原因java.lang.ClassNotFoundException:   org.apache.kafka.clients.producer.KafkaProducer at   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)     在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)     在simple_kafka.SimpleProducer.doPost(SimpleProducer.java:56)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:661)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:742)at at   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(Thread.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&#c>

  1. Kafka 2.12-0.11.0.1.jar
  2. 卡夫卡的客户端 - 0.11.0.1.jar
  3. SLF4J-API-1.7.25.jar

1 个答案:

答案 0 :(得分:0)

经过一天的搜索,我发现你需要包含JAR文件WEB-INF-&gt; lib文件夹,您需要从eclipse&amp;删除您的服务器重新配置它。在这之后,我能够运行KafkaProducer。