Use the kafka-clients
operation kafka
is always unsuccessful, the reasons are unclear, the following posted related code and configuration, please know how to guide, thank you!
Environment and dependence
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.10.2.0</version></dependency>
JDK
Version 1.8, Kafka
version 2.12-0.10.2.0
, server use CentOS-7
build.
Test code
public class TestBase { protected Logger log = LoggerFactory.getLogger(this.getClass()); protected String kafka_server = "192.168.60.160:9092" ; protected String topic = "zlikun_topic";}
public class Producertest extends Testbase {protected Properties props = new properties (); @Before public void init () {props.put (producerconfig.bootstrap_servers_config, kafka_server); Props.put (Producerconfig.acks_config, "all"); Props.put (producerconfig.retries_config, 0); Props.put (Producerconfig.batch_size_config, 16384); Props.put (Producerconfig.linger_ms_config, 1); Props.put (Producerconfig.buffer_memory_config, 33554432); Props.put (Producerconfig.key_serializer_class_config, Stringserializer.class); Props.put (Producerconfig.value_serializer_class_config, Stringserializer.class); Props.put (Producerconfig.partitioner_class_config, Mypartitioner.class); } @Test public void Test () throws Interruptedexception {kafkaproducer<string, string> producer = new K Afkaproducer<> (props); Send message for (int i = 0; i < i++) {producer.send (new producerrecord<stRing, string> (topic, integer.tostring (i), integer.tostring (i)), new Callback () {@Override public void Oncompletion (Recordmetadata recordmetadata, Exception e) {if (E = = null) { System.out.printf ("offset =%d, partition =%d \ n", Recordmetadata.offset (), recordmetadata.partition ()); } else {log.error ("Send Error!", e); } } }); } TimeUnit.SECONDS.sleep (3); Producer.close (); }}
public class Consumertest extends Testbase {private Properties props = new properties (); @Before public void init () {props.put (consumerconfig.bootstrap_servers_config, kafka_server); Props.put (Consumerconfig.group_id_config, "Zlikun"); Props.put (Consumerconfig.enable_auto_commit_config, "true"); Props.put (Consumerconfig.auto_commit_interval_ms_config, "1000"); Props.put (Consumerconfig.key_deserializer_class_config, Stringdeserializer.class); Props.put (Consumerconfig.value_deserializer_class_config, Stringdeserializer.class); } @Test public void Test () {consumer<string, string> Consumer = new kafkaconsumer<> (props); Consumer.subscribe (arrays.aslist (topic));//Consumer.assign (Arrays.aslist (New Topicpartition (topic, 1)); while (true) {consumerrecords<string, string> records = Consumer.poll (100); For (consumerrecord<string, string> record:records) { System.out.printf ("offset =%d, key =%s, value =%s%n", Record.offset (), Record.key (), Record.value ()); } } }}
Problem
# 测试topic为手动创建$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 4 --topic zlikun_topic
Console output Information
[Kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 2 record (s) For zlikun_topic-3:30042 Ms have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 2 record (s) For zlikun_topic-3:30042 Ms have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 2 record (s) For zlikun_topic-2:30042 Ms have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 2 record (s) For zlikun_topic-2:30042 Ms have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 3 record (s) For zlikun_topic-1:30043 MS have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 3 record (s) For zlikun_topic-1:30043 MS have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 3 record (s) For zlikun_topic-1:30043 MS have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] Error Com.zlikun.mq.producertest-send error!org.apache.kafka.common.errors.timeoutexception:expiring 3 record (s) For zlikun_topic-0:30046 Ms have passed since batch creation plus linger Time[kafka-producer-network-thread | producer-1] ERROR Com.zlikun.mq.producertest-send Error!org.apache.kafka.common.errors.timeoutexception:expiring 3 record (s) for zlikun_topic-0:30046 Ms have passed since BA TCH creation plus Linger Time[kafka-producer-network-thread | producer-1] Error com.zlikun.mq.producertest-send error!o Rg.apache.kafka.common.errors.TimeoutException:Expiring 3 record (s) for zlikun_topic-0:30046 Ms have passed since batch C Reation Plus linger time
Java Operation Kafka execution unsuccessful >> Java
The answer is quite clear:
Http://www.goodpm.net/postreply/java/1010000008863969/Java Operation Kafka Execution is unsuccessful. html
Java Operation Kafka execution is unsuccessful