本文主要是介绍kafka-0.10.0.1版本在java客户端发送数据接收不到,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误日志:
org.apache.kafka.common.errors.TimeoutException: Batch containing 100 record
kafka版本:0.10.0.1
zookeeper版本: 3.4.5
产生的现象:
编写好java客户端后,给kafka发送数据,可是数据会一直发送不过去,而且显示上面那种TimeoutException错误.
生产者配置:
import java.util.Properties;import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;public class KafkaProducerTest{public static void main(String[] args) {Properties props = new Properties();props.put("bootstrap.servers", "192.168.72.18:9092");props.put("acks", "all");props.put("retries", 0);props.put("batch.size", 16384);props.put("linger.ms", 1);props.put("buffer.memory", 33554432);props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");Producer<String, String> producer = new KafkaProducer<String, String>(props);for(int i = 0; i < 100; i++){System.out.println(Integer.toString(i));producer.send(new ProducerRecord<String, String>("test1001", Integer.toString(i), Integer.toString(i)));}producer.close();}
}
消费者代码:
import java.util.Arrays;
import java.util.Properties;import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;public class KafkaConsumerTest {public static void main(String[] args) {Properties props = new Properties();props.put("bootstrap.servers", "192.168.72.18:9092");props.put("group.id", "test");props.put("enable.auto.commit", "true");props.put("auto.commit.interval.ms", "1000");props.put("session.timeout.ms", "30000");props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);consumer.subscribe(Arrays.asList("test1001"));while (true) {ConsumerRecords<String, String> records = consumer.poll(100);for (ConsumerRecord<String, String> record : records)System.out.printf("offset = %d, key = %s, value = %s", record.offset(), record.key(), record.value());}}
}
解决:
最终是发送配置应该不正确,原来的配置:
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0############################# Socket Server Settings ############################## The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = security_protocol://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
listeners=PLAINTEXT://name1:9092# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://name1:9092# The number of threads handling network requests
num.network.threads=3
修改后的配置:
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0############################# Socket Server Settings ############################## The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = security_protocol://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
listeners=PLAINTEXT://192.168.72.18:9092# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.72.18:9092# The number of threads handling network requests
num.network.threads=3
这样把ip写好,不使用名字映射,就正常了
这篇关于kafka-0.10.0.1版本在java客户端发送数据接收不到的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!