其他分享
首页 > 其他分享> > Kafka生产者与消费者

Kafka生产者与消费者

作者:互联网

Kafka生产者与消费者

1. kafka客户端——生产者

1. pom配置

    <properties>
        <lombok.version>1.16.18</lombok.version>
        <fastjson.version>1.2.66</fastjson.version>
        <kafka.version>2.4.1</kafka.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
             <version>${kafka.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>

    </dependencies>

2. 生产者发送消息的基本实现

package hcx.kafka.core;

import com.alibaba.fastjson.JSON;
import hcx.kafka.entities.Order;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class MyProducer {
 private final static String TOPIC_NAME="my-replicated-topic";//主题名称
 public static void main(String[] args) throws ExecutionException, InterruptedException {
     Properties props = new Properties();
     props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"10.1.48.214:9092,10.1.48.214:9093,10.1.48.214:9094");//设置集群

     //把发送的key从字符串序列化为字节数组
     props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

     //把发送的value从字符串序列化为字节数组
     props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

     //发消息的客户端初始化
     Producer<String,String> producer = new KafkaProducer<String, String>(props);

     //要发送5条消息
     final int msgNum=5;
     final CountDownLatch countDownLatch=new CountDownLatch(msgNum);

     for (int i=1;i<=5;i++){
         Order order = new Order((long)i,i);

//            //未指定发送分区,具体发送的分区计算公式:hash(key)%partitionNum
//            ProducerRecord<String,String> producerRecord=new ProducerRecord<String, String>(TOPIC_NAME,String.valueOf(order.getOrderId()), JSON.toJSONString(order));
//
//            //3.3 同步方式发送消息
//            Future<RecordMetadata> future = producer.send(producerRecord);
//            RecordMetadata metadata = future.get();
//            System.out.println("同步方式发送消息结果:" + "topic-" +metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" +metadata.offset());

         //指定发送分区
         ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(TOPIC_NAME, 0 , String.valueOf(order.getOrderId()),JSON.toJSONString(order));
         //异步回调方式发送消息
         producer.send(producerRecord, new Callback() {
             public void onCompletion(RecordMetadata metadata, Exception exception) {
                 if (exception != null) {
                     System.err.println("发送消息失败:" +
                             exception.getStackTrace());
                 }
                 if (metadata != null) {
                     System.out.println("异步方式发送消息结果:" + "topic-" +metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" + metadata.offset());
                 }
                 countDownLatch.countDown();
             }
         });
     }
     countDownLatch.await(5,TimeUnit.SECONDS);
//        TimeUnit.SECONDS.sleep(10);//方便异步测试
     //4.关闭资源
     producer.close();
 }
}

3.发送消息到指定分区上

 ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(TOPIC_NAME, 0 , String.valueOf(order.getOrderId()),JSON.toJSONString(order));

4. 未指定分区

//未指定发送分区,具体发送的分区计算公式:hash(key)%partitionNum
ProducerRecord<String,String> producerRecord=new ProducerRecord<String, String>(TOPIC_NAME,String.valueOf(order.getOrderId()), JSON.toJSONString(order));

5. 同步发送

           //3.3 同步方式发送消息
           Future<RecordMetadata> future = producer.send(producerRecord);
           RecordMetadata metadata = future.get();
           System.out.println("同步方式发送消息结果:" + "topic-" +metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" +metadata.offset());

6. 异步发送消息

			//要发送5条消息
            final int msgNum=5;
            final CountDownLatch countDownLatch=new CountDownLatch(msgNum);
			for (int i=1;i<=5;i++){
            Order order = new Order((long)i,i);
			//指定发送分区
            ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(TOPIC_NAME, 0 , String.valueOf(order.getOrderId()),JSON.toJSONString(order));
            //异步回调方式发送消息
            producer.send(producerRecord, new Callback() {
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    if (exception != null) {
                        System.err.println("发送消息失败:" +
                                exception.getStackTrace());
                    }
                    if (metadata != null) {
                        System.out.println("异步方式发送消息结果:" + "topic-" +metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" + metadata.offset());
                    }
                    countDownLatch.countDown();
                }
            });
        }

7. 关于生产者的ack设置

props.put(ProducerConfig.ACKS_CONFIG, "1"); //设置ack

8. 细节部分

2. kafaka客户端——消费者

1. 消费者的基本实现

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class MyConsumer {
    private final static String TOPIC_NAME = "my-replicated-topic";
    private final static String CONSUMER_GROUP_NAME = "testGroup";

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"10.1.48.214:9092,10.1.48.214:9093,10.1.48.214:9094");
        // 消费分组名
        props.put(ConsumerConfig.GROUP_ID_CONFIG, CONSUMER_GROUP_NAME);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        //创建一个消费者的客户端
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String,String>(props);
        // 消费者订阅主题列表
        consumer.subscribe(Arrays.asList(TOPIC_NAME));

        while (true) {
            /*
             * poll() API 是拉取消息的⻓轮询
             */
            ConsumerRecords<String, String> records =consumer.poll(Duration.ofMillis( 1000 ));
            for (ConsumerRecord<String, String> record : records) {
                System.out.printf("收到消息:partition = %d,offset = %d, key =%s, value = %s%n", record.partition(),record.offset(), record.key(), record.value());
            }
        }
    }
}

2. 自动提交offset

// 是否自动提交offset,默认就是true
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
// 自动提交offset的间隔时间
props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");

3. 手动提交offset

props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");

4. 消费者的pool消息过程

5 .指定分区消费

consumer.assign(Arrays.asList(new TopicPartition(TOPIC_NAME, 0 )));

6 .指定回溯消费

consumer.assign(Arrays.asList(new TopicPartition(TOPIC_NAME, 0 )));
consumer.seekToBeginning(Arrays.asList(new TopicPartition(TOPIC_NAME,0 )));

7. 指定offset消费

consumer.assign(Arrays.asList(new TopicPartition(TOPIC_NAME, 0 )));
consumer.seek(new TopicPartition(TOPIC_NAME, 0 ), 10 );

8. 指定时间点消费

List<PartitionInfo> topicPartitions =consumer.partitionsFor(TOPIC_NAME);
//从 1 小时前开始消费
long fetchDataTime = new Date().getTime() - 1000 * 60 * 60 ;
Map<TopicPartition, Long> map = new HashMap<>();
for (PartitionInfo par : topicPartitions) {
    map.put(new TopicPartition(TOPIC_NAME, par.partition()),fetchDataTime);
}
Map<TopicPartition, OffsetAndTimestamp> parMap =consumer.offsetsForTimes(map);
for (Map.Entry<TopicPartition, OffsetAndTimestamp> entry :parMap.entrySet()) {
    TopicPartition key = entry.getKey();
    OffsetAndTimestamp value = entry.getValue();
    if (key == null || value == null) continue;
    Long offset = value.offset();
    System.out.println("partition-" + key.partition() +"|offset-" + offset);
    System.out.println();
    //根据消费里的timestamp确定offset
    if (value != null) {
        consumer.assign(Arrays.asList(key));
        consumer.seek(key, offset);
    }
}

9. 新消费组的消费偏移量

当消费主题的是一个新的消费组,或者指定offset的消费方式,offset不存在,那么应该如何消费?

存在两种情况:

props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

10. 学习来源

哔哩哔哩:https://www.bilibili.com/video/BV1Xy4y1G7zA

标签:消费者,生产者,props,Kafka,发送,offset,new,consumer,metadata
来源: https://www.cnblogs.com/hcxss/p/16601423.html