【Kakfa五】Kafka Producer和Consumer基本使用

0.Kafka服务器的配置

一个Broker,

一个Topic

Topic中只有一个Partition()

 

1. Producer:

package kafka.examples.producers;


import kafka.producer.KeyedMessage;
import kafka.javaapi.producer.Producer;
import kafka.producer.ProducerConfig;

import java.util.Properties;

public class SimpleProducer {
    private static Producer<Integer, String> producer;
    private static final Properties props = new Properties();
    ///ProducerConfig没有关于Zookeeper的配置信息
    static {
        props.put("broker.list", "192.168.26.140:9092");

        /*metadata.broker.list is for bootstrapping and the producer will only use it for getting 
        metadata (topics, partitions and replicas). The socket connections for 
        sending the actual data will be established based on the broker 
        information returned in the metadata. The format is 
        host1:port1,host2:port2, and the list can be a subset of brokers or a 
        VIP pointing to a subset of brokers.*/
        props.put("metadata.broker.list", "192.168.26.140:9092");

        /*The serializer class for messages. The default encoder(kafka.serializer.DefaultEncoder) takes a byte[] and returns the same byte[].*/
        props.put("serializer.class", "kafka.serializer.StringEncoder");

        /**/
        props.put("request.required.acks", "1");
        producer = new Producer<Integer, String>(new ProducerConfig(props));
    }

    public static void main(String[] args) {
        String topic = "learn.topic";
        String messageStr = "This is a simple message from JavaAPI Producer2";
        ///Key如何生成的?
        KeyedMessage<Integer, String> data = new KeyedMessage<Integer,String>(topic, messageStr);
        producer.send(data);
        producer.close();
    }
}

 

 

关于request.required.acks:

 

This value controls when a produce request is considered completed. Specifically, how many other brokers must have committed the data to their log and acknowledged this to the leader? Typical values are

  • 0, which means that the producer never waits for an acknowledgement from the broker (the same behavior as 0.7). This option provides the lowest latency but the weakest durability guarantees (some data will be lost when a server fails).
  • 1, which means that the producer gets an acknowledgement after the leader replica has received the data. This option provides better durability as the client waits until the server acknowledges the request as successful (only messages that were written to the now-dead leader but not yet replicated will be lost).
  • -1, The producer gets an acknowledgement after all in-sync replicas have received the data. This option provides the greatest level of durability. However, it does not completely eliminate the risk of message loss because the number of in sync replicas may, in rare cases, shrink to 1. If you want to ensure that some minimum number of replicas (typically a majority) receive a write, then you must set the topic-level min.insync.replicas setting. Please read the Replication section of the design documentation for a more in-depth discussion.

关于KeyedMessage:

/**
 * A topic, key, and value.
 * If a partition key is provided it will override the key for the purpose of partitioning but will not be stored.
 */
case class KeyedMessage[K, V](val topic: String, val key: K, val partKey: Any, val message: V) {
  if(topic == null)
    throw new IllegalArgumentException("Topic cannot be null.")
  
  def this(topic: String, message: V) = this(topic, null.asInstanceOf[K], null, message)
  
  def this(topic: String, key: K, message: V) = this(topic, key, key, message)
  //分区键,如果没有,是什么行为
  def partitionKey = {
    if(partKey != null)
      partKey
    else if(hasKey)
      key
    else
      null  
  }
  
  def hasKey = key != null
}

 

 

 

2. Consumer

package kafka.examples.consumers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

public class SimpleHLConsumer {
    private final ConsumerConnector consumer;
    private final String topic;

    public SimpleHLConsumer(String zookeeper, String groupId, String
            topic) {
        ///Consumer的属性配置
        Properties props = new Properties();
        props.put("zookeeper.connect", zookeeper);
        //consumer group id
        props.put("group.id", groupId);
        /*
        ZooKeeper session timeout. If the server fails to heartbeat to ZooKeeper
        within this period of time it is considered dead. If you set this too 
        low the server may be falsely considered dead; if you set it too high it
        may take too long to recognize a truly dead server.
        */
        props.put("zookeeper.session.timeout.ms", "500"); //默认6秒
        ///How far a ZK follower can be behind a ZK leader.默认两秒
        props.put("zookeeper.sync.time.ms", "250");
        ///offset自动提交的时间间隔
        props.put("auto.commit.interval.ms", "1000");
        consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(props));
        this.topic = topic;
    }

    public void doConsume() {
        Map<String, Integer> topicCount = new HashMap<String, Integer>();
        // Define single thread for topic
        topicCount.put(topic, new Integer(1));
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = consumer.createMessageStreams(topicCount);
        //KafkaStream是一个BlockingQueue
        List<KafkaStream<byte[], byte[]>> streams = consumerStreams.get(topic);
        ///有几个线程,就会有几个Kafka Stream
        for (final KafkaStream stream : streams) {
            /**
             * An iterator that blocks until a value can be read from the supplied queue.
             * The iterator takes a shutdownCommand object which can be added to the queue to trigger a shutdown
             *
            */
            ConsumerIterator<byte[], byte[]> consumerIte = stream.iterator();
            ///阻塞在hasNext等待消息到来
            while (consumerIte.hasNext()) {
                System.out.println("Message from Single Topic :: " + new String(consumerIte.next().message()));
            }
        }
        if (consumer != null) {
            consumer.shutdown();
        }
    }

    public static void main(String[] args) {
        String topic = "learn.topic";
        ////learn.topic.consumers.group是消费者群组,不需要预先定义,但是会记录到Zookeeper中
        SimpleHLConsumer simpleHLConsumer = new SimpleHLConsumer("192.168.26.140:2181", "learn.topic.consumers.group", topic);
        simpleHLConsumer.doConsume();
    }
}

 

3. 注意的问题:

因为Kafka服务器和Producer、Consumer不在同一个机器上,因此在配置Kafka中的Zookeeper连接信息以及server.properties中的host.name时,需要指定具体的IP,不能使用localhost

 

 

你可能感兴趣的:(kafka)