无法在使用者控制台中查看从Java API生成的kafka消息

时间:2016-02-15 08:57:00

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

我试图制作一些消息并放入主题,然后从控制台消费者处获取相同内容。

使用的代码:

import  java.util.Date;
import  java.util.Properties;
import  kafka.javaapi.producer.Producer;
import  kafka.producer.KeyedMessage;
import  kafka.producer.ProducerConfig;


public  class   SimpleProducer  {
    private static  Producer<String,String> producer;
    public  SimpleProducer()    {
            Properties  props   =   new Properties();

            //  Set the broker  list    for requesting  metadata    to  find    the lead    broker
            props.put("metadata.broker.list","172.22.96.56:9092,172.22.96.56:9093,172.22.96.56:9094");

            //This  specifies   the serializer  class   for keys    
            props.put("serializer.class",   "kafka.serializer.StringEncoder");

            //  1   means   the producer    receives    an  acknowledgment  once    the lead replica    
            //  has received    the data.   This    option  provides    better  durability  as  the     
            //  client  waits   until   the server  acknowledges    the request as successful.
            props.put("request.required.acks",  "1");

            ProducerConfig  config  =   new ProducerConfig(props);
            producer    =   new Producer<String,    String>(config);
    }
    public  static  void    main(String[]   args)   {
            int argsCount   =   args.length;
            if  (argsCount  ==  0   ||  argsCount   ==  1)
                    throw   new IllegalArgumentException(
                            "Please provide topic   name    and Message count   as  arguments");

            String  topic   =   (String)    args[0];
            String  count   =   (String)    args[1];
            int messageCount    =   Integer.parseInt(count);
            System.out.println("Topic   Name    -   "   +   topic);
            System.out.println("Message Count   -   "   +   messageCount);
            SimpleProducer  simpleProducer  =   new SimpleProducer();
            simpleProducer.publishMessage(topic,    messageCount);
    }
    private void    publishMessage(String   topic,  int messageCount)   {
            for (int    mCount  =   0;  mCount  <   messageCount;   mCount++)   {
                    String  runtime =   new Date().toString();
                    String  msg =   "Message    Publishing  Time    -   "   +   runtime;
                    System.out.println(msg);
                    //  Creates a   KeyedMessage    instance
                    KeyedMessage<String,    String> data    =   
                            new KeyedMessage<String,    String>(topic,  msg);

                    //  Publish the message
                    producer.send(data);
            }
            //  Close   producer    connection  with    broker.
            producer.close();
    }

}

输出:

  

主题名称 - 测试   消息计数 - 10   log4j:WARN找不到记录器的appender   (kafka.utils.VerifiableProperties)。   log4j:WARN请正确初始化log4j系统。   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016   消息发布时间 - 2016年2月16日星期二02:00:56 IST 2016

从命令行我提供主题的名称为&#34; kafkatopic&#34;并计算消息&#34; 10&#34;。该程序运行良好,没有ant异常,但当我尝试从控制台看到消息时,它们不会出现。主题已创建。

  

bin / kafka-console-consumer.sh --zookeeper localhost:2181 --topic kafkatopic --from-beginning

你可以在出错的时候提供帮助!!

1 个答案:

答案 0 :(得分:0)

我想指出两件事:

1)你没有在这里指定--zookeeper - 你应该--bootstrap-server参数。

2)您应该看到server.properties文件对listenersadvertised.listener的评价。你应该正确地将它们指向经纪人。

我希望这会有所帮助。