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

编程技术  /  houtizong 发布于 3年前   115

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

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客