如果没有安装kafka,可以参考我的另一篇文章
https://blog.csdn.net/zs319428/article/details/83859229
先来个生产者的
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 kafkaProducer {
public static String topic = "test";
public static Producer<String, String> producer;
public static void main(String[] args) throws InterruptedException {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:9092");
// 0不需要等待任何确认收到的信息(性能最佳) 1至少要等待leader已经成功将数据写入本地log(中等) all所有备份都成功写入日志(最强保住不丢)
props.put("acks", "1");
// 消息发送最大尝试次数
props.put("retries", 0);
// 批量处理的消息的大小 小于此size会进行批量打包发送 设置过大会占用太多内存 太小降低吞吐
props.put("batch.size", 16384);
// 批量发送消息的延迟为5ms
props.put("linger.ms", 5);
// 缓存数据的内存大小 如果产生数据速度大于发送速度 并且缓存超过这个大小 则抛出异常
props.put("buffer.memory", 33554432);
// key和value的编码序列化方法
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(props);
for (int i = 0; i < 10000; i++) {
Thread.sleep(1);
ProducerRecord<String, String> producerRecord =
new ProducerRecord<String, String>(topic, "", "topic向你发来慰问" + i);
producer.send(producerRecord);
}
}
}
下面是消费者的
package com.kafka.mykafka;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class kafkaConsumer extends Thread {
public static String topic = "test";
/** 消息保存队列 */
public static ArrayBlockingQueue<ConsumerRecord<String, String>> recordQueue =
new ArrayBlockingQueue<ConsumerRecord<String, String>>(10000);
public static KafkaConsumer<String, String> consumer;
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:9092");
// 分组。一条消息只会被同组的一个consume消费
props.put("group.id", "test");
// 自动提交offset; false的话还需要手动调用consumer.commitSync()
props.put("enable.auto.commit", "true");
// 自动提交offset的时间间隔 太短性能会差,太长如果crash重启后可能会重复消费的多(前提是消息在kafka服务器上还在)
props.put("auto.commit.interval.ms", "1000");
// session 过期时间 一旦心跳连接不上,超过这个时间就会链接断开
props.put("session.timeout.ms", "30000");
// # 重复消费,设置为latest,如游戏中的聊天等
props.put("auto.offset.reset", "latest");
// key和value的解码序列化方法
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
consumer = new KafkaConsumer<String, String>(props);
// 绑定topic
consumer.subscribe(Arrays.asList(topic));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1000);
System.out.println("收到一次kafka消息,size:" + records);
for (ConsumerRecord<String, String> record : records) {
recordQueue.offer(record);
System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(),
record.key(), record.value());
}
System.out.println("recordQueue" + recordQueue.size());
}
}
}
然后再运行即可搞定