Java使用kafka发送和生产消息

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012129558/article/details/80065817

1. maven依赖包

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka-clients</artifactId>
  <version>0.9.0.1</version>
</dependency>

2. 生产者代码

package com.lnho.example.kafka;
 
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
 
import java.util.Properties;
 
public class KafkaProducerExample {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("bootstrap.servers", "master: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<>(props);
    for(int i = 0; i < 100; i++)
      producer.send(new ProducerRecord<>("topic1", Integer.toString(i), Integer.toString(i)));
 
    producer.close();
  }
}

3. 消费者代码

package com.lnho.example.kafka;
 
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
 
import java.util.Arrays;
import java.util.Properties;
 
public class KafkaConsumerExample {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("bootstrap.servers", "master: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<>(props);
    consumer.subscribe(Arrays.asList("topic1"));
    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());
    }
  }
}

展开阅读全文

kafka producer生产消息时报错

12-16

16/12/16 11:30:51 INFO client.ClientUtils$: Fetching metadata from broker BrokerEndPoint(0,act201,9092) with correlation id 5 for 1 topic(s) Set(ncytest1)rn16/12/16 11:30:51 INFO producer.SyncProducer: Connected to act201:9092 for producingrn16/12/16 11:30:51 INFO producer.SyncProducer: Disconnecting from act201:9092rn16/12/16 11:30:51 WARN producer.BrokerPartitionInfo: Error while fetching metadata [TopicMetadata for topic ncytest1 -> rnNo partition metadata for topic ncytest1 due to org.apache.kafka.common.errors.LeaderNotAvailableException] for topic [ncytest1]: class org.apache.kafka.common.errors.LeaderNotAvailableException rn16/12/16 11:30:51 ERROR async.DefaultEventHandler: Failed to collate messages by topic, partition due to: Failed to fetch topic metadata for topic: ncytest1rn16/12/16 11:30:51 INFO async.DefaultEventHandler: Back off for 100 ms before retrying send. Remaining retries = 1rn16/12/16 11:30:51 INFO client.ClientUtils$: Fetching metadata from broker BrokerEndPoint(1,act202,9092) with correlation id 6 for 1 topic(s) Set(ncytest1)rn16/12/16 11:30:51 INFO producer.SyncProducer: Connected to act202:9092 for producingrn16/12/16 11:30:51 INFO producer.SyncProducer: Disconnecting from act202:9092rn16/12/16 11:30:51 WARN producer.BrokerPartitionInfo: Error while fetching metadata [TopicMetadata for topic ncytest1 -> rnNo partition metadata for topic ncytest1 due to org.apache.kafka.common.errors.LeaderNotAvailableException] for topic [ncytest1]: class org.apache.kafka.common.errors.LeaderNotAvailableException rn16/12/16 11:30:51 INFO client.ClientUtils$: Fetching metadata from broker BrokerEndPoint(0,act201,9092) with correlation id 7 for 1 topic(s) Set(ncytest1)rn16/12/16 11:30:51 INFO producer.SyncProducer: Connected to act201:9092 for producingrn16/12/16 11:30:51 INFO producer.SyncProducer: Disconnecting from act201:9092rn16/12/16 11:30:51 WARN producer.BrokerPartitionInfo: Error while fetching metadata [TopicMetadata for topic ncytest1 -> rnNo partition metadata for topic ncytest1 due to org.apache.kafka.common.errors.LeaderNotAvailableException] for topic [ncytest1]: class org.apache.kafka.common.errors.LeaderNotAvailableException rn16/12/16 11:30:51 ERROR async.DefaultEventHandler: Failed to collate messages by topic, partition due to: Failed to fetch topic metadata for topic: ncytest1rn16/12/16 11:30:51 INFO async.DefaultEventHandler: Back off for 100 ms before retrying send. Remaining retries = 0rn16/12/16 11:30:51 INFO client.ClientUtils$: Fetching metadata from broker BrokerEndPoint(3,act204,9092) with correlation id 8 for 1 topic(s) Set(ncytest1)rn16/12/16 11:30:51 INFO producer.SyncProducer: Connected to act204:9092 for producingrn16/12/16 11:30:51 INFO producer.SyncProducer: Disconnecting from act204:9092rn16/12/16 11:30:51 ERROR async.DefaultEventHandler: Failed to send requests for topics ncytest1 with correlation ids in [0,8]rnException in thread "Thread-10" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.rn at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:93)rn at kafka.producer.Producer.send(Producer.scala:77)rn at kafka.javaapi.producer.Producer.send(Producer.scala:33)rn at com.surfilter.ip.kafka.producer.KafkaSend.send(KafkaSend.java:51)rn at com.surfilter.ip.kafka.producer.KafkaProducer.run(KafkaProducer.java:32)rnrnrn用java写了个kafka producer程序,启动的时候报错 ,但是topic已经创建成功了,消息发送不了rn 论坛

没有更多推荐了,返回首页