简体   繁体   English

Kafka - 生产者致谢

[英]Kafka - Producer Acknowledgement

I saw in a video tutorial that Kafka Broker supports 3 types of acknowledgement when producer posts a message.我在视频教程中看到,Kafka Broker 在生产者发布消息时支持 3 种类型的确认。

0 - Fire and Forget 0 - 火而忘记
1 - Leader Ack 1 - 领导确认
2 - Ack of all the brokers 2 - 确认所有经纪人

I am using Kafka's Java API to post message.我正在使用 Kafka 的 Java API 来发布消息。 Is this something that has to be set for each broker using server.properties specific to each broker or is it something that has to be set by producer?这是必须使用特定于每个代理的 server.properties 为每个代理设置的内容还是必须由生产者设置的内容? If it has to be set by the producer , please explain how it can be set using Java API.如果必须由生产者设置,请说明如何使用 Java API 进行设置。

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;

public class KafkaProducerApp {

    public static void main(String[] args){
        Properties properties = new Properties();
        properties.put("bootstrap.servers","localhost:9092,localhost:9093,localhost:9094");
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String,String> kafkaProducer = new KafkaProducer<String, String>(properties);

        try{
            for(int i=0;i<150;i++) {
                RecordMetadata ack = kafkaProducer.send(new ProducerRecord<String, String>("replicated_topic", Integer.toString(i), "MyMessage" + Integer.toString(i))).get();
                System.out.println(" Offset = " + ack.offset());
                System.out.println(" Partition = " + ack.partition());
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            kafkaProducer.close();
        }



    }

}

It's a producer property and is set similar to other properties you have in your code:它是一个生产者属性,设置类似于您在代码中拥有的其他属性:

properties.put("acks","all");

The list of all configurable producer properties can be found here .可以在此处找到所有可配置生产者属性的列表。

You might want to also look at the broker (or topic) property min.insync.replicas that is related to this producer config.您可能还想查看与此生产者配置相关的代理(或主题)属性min.insync.replicas

I think you should understand the acks property what has actually done and look at the also behind scenes.我认为您应该了解acks属性实际做了什么,并查看幕后情况。 If that is ok, you will see this property is configured by the producer .如果没问题,你会看到这个属性是由 producer配置

For example, you mustn't lose any message like an audit log.例如,您不能丢失任何消息,例如审核日志。 The following code how we would start our producer config:以下代码我们将如何启动我们的生产者配置:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092,localhost:9093");
props.put("acks", "all"); //We are using acks=all in order to get the strongest guarantee we can.
props.put("retries", "3");
props.put("max.in.flight.requests.per.connection", "5");

This is a small but powerful change that has a major impact on if a message is going to arrive or not.这是一个小而强大的变化,对消息是否会到达有重大影响。

This images that from Kafka In Action book which represents more clear for acks property:这张来自Kafka In Action书中的图片更清楚地表示了acks属性:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM