Maven
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>0.11.0.0</version>
</dependency>
生產者Producer
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class ProducerDemo {
private final KafkaProducer<String, String> producer;
public final static String TOPIC = "test5";
private ProducerDemo() {
Properties props = new Properties();
props.put("bootstrap.servers", "xxx:9092,1xxx:9092,xxx:9092");//xxx服務器ip
props.put("acks", "all");//所有follower都響應了才認為消息提交成功,即"committed"
props.put("retries", 0);//retries = MAX 無限重試,直到你意識到出現了問題:)
props.put("batch.size", 16384);//producer將試圖批處理消息記錄,以減少請求次數.默認的批量處理消息字節數
//batch.size當批量的數據大小達到設定值后,就會立即發送,不顧下面的linger.ms
props.put("linger.ms", 1);//延遲1ms發送,這項設置將通過增加小的延遲來完成--即,不是立即發送一條記錄,producer將會等待給定的延遲時間以允許其他消息記錄發送,這些消息記錄可以批量處理
props.put("buffer.memory", 33554432);//producer可以用來緩存數據的內存大小。
props.put("key.serializer",
"org.apache.kafka.common.serialization.IntegerSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(props);
}
public void produce() {
int messageNo = 1;
final int COUNT = 5;
while(messageNo < COUNT) {
String key = String.valueOf(messageNo);
String data = String.format("hello KafkaProducer message %s from hubo 06291018 ", key);
try {
producer.send(new ProducerRecord<String, String>(TOPIC, data));
} catch (Exception e) {
e.printStackTrace();
}
messageNo++;
}
producer.close();
}
public static void main(String[] args) {
new ProducerDemo().produce();
}
}
消費者Consumer
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 UserKafkaConsumer extends Thread {
public static void main(String[] args){
Properties properties = new Properties();
properties.put("bootstrap.servers", "xxx:9092,xxx:9092,xxx:9092");//xxx是服務器集群的ip
properties.put("group.id", "jd-group");
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "1000");
properties.put("auto.offset.reset", "latest");
properties.put("session.timeout.ms", "30000");
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);
kafkaConsumer.subscribe(Arrays.asList("test5"));
while (true) {
ConsumerRecords<String, String> records = kafkaConsumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.println("-----------------");
System.out.printf("offset = %d, value = %s", record.offset(), record.value());
System.out.println();
}
}
}
}
