有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么卡夫卡消费者表现缓慢?

我有一个简单的主题,一个简单的卡夫卡消费者和生产者,使用默认配置

这个程序非常简单,我有两个线程

在producer中,它不断发送16字节的数据

在消费者方面,它一直在接受

我发现生产商的吞吐量大约是10MB/s,这很好

但是消费者的吞吐量只有0.2MB/s。我已经禁用了所有调试日志,但这并没有使它变得更好。测试正在本地计算机上运行。有人知道出了什么问题吗?谢谢

我使用的代码如下: 制片人:

KafkaProducer producer = new KafkaProducer(props);
int size = 16;
byte[] payload = new byte[size];
String key = "key";
Arrays.fill(payload, (byte) 1);
ProducerRecord record = new ProducerRecord("test",0,key.getBytes(),payload);
while(true){
producer.send(record);
}

消费者:

Properties consumerProps = new Properties();
consumerProps.put("zookeeper.connect", "localhost:2181");
consumerProps.put("group.id", "test");
ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(consumerProps));
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put("test", 1);
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get("test");
ConsumerIterator<byte[], byte[]> it = streams.get(0).iterator();
while(it.hasNext()){
    it.next().message();
}

共 (1) 个答案

  1. # 1 楼答案

    尝试使用以下属性配置使用者

    1. fetch.min.bytes
    2. fetch.max.wait.ms
    3. max.partition.fetch.bytes

    此外,还可以为吞吐量调整poll()方法的超时参数

    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));