maven依赖
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>0.11.0.0</version></dependency>
不带回调函数的API
需要用到的类:
KafkaProducer:需要创建一个生产者对象,用来发送数据
ProducerConfig:获取所需的一系列配置参数
ProducerRecord:每条数据都要封装成一个ProducerRecord对象
package com.producer;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;import java.util.concurrent.ExecutionException;public class CustomProducer {public static void main(String[] args) throws ExecutionException, InterruptedException {Properties props = new Properties();//kafka集群,broker-listprops.put("bootstrap.servers", "zjj101:9092");props.put("acks", "all");//重试次数props.put("retries", 1);//批次大小props.put("batch.size", 16384);//等待时间props.put("linger.ms", 1);//RecordAccumulator缓冲区大小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++) {//往topic为false发送数据producer.send(new ProducerRecord<String, String>("first", Integer.toString(i), Integer.toString(i)));}producer.close();}}
带回调函数的API
回调函数会在producer收到ack时调用,为异步调用,该方法有两个参数,分别是RecordMetadata和Exception,如果Exception为null,说明消息发送成功,如果Exception不为null,说明消息发送失败。
注意:消息发送失败会自动重试,不需要我们在回调函数中手动重试。
因为是异步调用,Kafka发送的进度和接收回调的进度完全是不一致的,
package com.producer;import org.apache.kafka.clients.producer.*;import java.util.Properties;import java.util.concurrent.ExecutionException;public class CustomProducer2 {public static void main(String[] args) throws ExecutionException, InterruptedException {Properties props = new Properties();props.put("bootstrap.servers", "zjj101:9092");//kafka集群,broker-listprops.put("acks", "all");props.put("retries", 1);//重试次数props.put("batch.size", 16384);//批次大小props.put("linger.ms", 1);//等待时间props.put("buffer.memory", 33554432);//RecordAccumulator缓冲区大小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<String, String>("first", Integer.toString(i), Integer.toString(i)), new Callback() {//回调函数,该方法会在Producer收到ack时调用,为异步调用@Overridepublic void onCompletion(RecordMetadata metadata, Exception exception) {if (exception == null) {System.out.println("success->" + metadata.offset());} else {exception.printStackTrace();}}});}producer.close();}}
