我无法从Kafka Producer发送消息。我的配置无法正常工作,看起来像这样:
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "127.0.0.1:9092");
properties.setProperty("key.serializer", StringSerializer.class.getName());
properties.setProperty("value.serializer", StringSerializer.class.getName());
properties.setProperty("acks", "1");
properties.setProperty("retries", "3");
properties.setProperty("linger.ms", "1");
Producer<String, String> producer =
new org.apache.kafka.clients.producer.KafkaProducer<String, String>(properties);
ProducerRecord<String, String> producerRecord =
new ProducerRecord<String, String>("second_topic", "3", "messagtest");
Future<RecordMetadata> s = producer.send(producerRecord);
producer.flush();
producer.close();
在我执行s.get()之后出现了错误;
java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for second_topic-0: 30021 ms has passed since batch creation plus linger time
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:64)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:29)
at ai.sys.producer.Test.main(Test.java:33)
Caused by: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for second_topic-0: 30021 ms has passed since batch creation plus linger time
答案 0 :(得分:0)
默认情况下,Kafka Producer中已启用批处理,大小为16K字节。但是,在您的代码中,您只发送了一条可能不满足批处理大小的记录。
因此,为了使您的代码正常工作,请尝试将以下“ batch.size”属性添加为Kafka Producer属性的值“ 0”。
properties.setProperty("batch.size", "0");
这将禁用批处理机制,并允许您的生产者将记录写入Kafka Broker。
注意:实时,禁用批处理将增加对代理的写请求数量,并降低I / O吞吐量以及生产者和服务器的性能。