Kafka 使用

最近更新时间:2024-08-23 14:52:21

我的收藏

生成数据

Java 代码方式

@Component
@Slf4j
public class KafkaProducer {

@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;

//自定义topic
public static final String TOPIC_TEST = "topic.test";

//
public static final String TOPIC_GROUP1 = "topic.group1";

//
public static final String TOPIC_GROUP2 = "topic.group2";

public void send(Object obj) {
String obj2String = JSONObject.toJSONString(obj);
log.info("准备发送消息为:{}", obj2String);
//发送消息
ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(TOPIC_TEST, obj);
future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
@Override
public void onFailure(Throwable throwable) {
//发送失败的处理
log.info(TOPIC_TEST + " - 生产者 发送消息失败:" + throwable.getMessage());
}

@Override
public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
//成功的处理
log.info(TOPIC_TEST + " - 生产者 发送消息成功:" + stringObjectSendResult.toString());
}
});
}
}

命令方式

bin/kafka-console-producer.sh --broker-list node86:9092 --topic t_cdr

消费数据

Java 代码方式

@Component
@Slf4j
public class KafkaConsumer {

@KafkaListener(topics = KafkaProducer.TOPIC_TEST, groupId = KafkaProducer.TOPIC_GROUP1)
public void topic_test(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {

Optional message = Optional.ofNullable(record.value());
if (message.isPresent()) {
Object msg = message.get();
log.info("topic_test 消费了: Topic:" + topic + ",Message:" + msg);
ack.acknowledge();
}
}

@KafkaListener(topics = KafkaProducer.TOPIC_TEST, groupId = KafkaProducer.TOPIC_GROUP2)
public void topic_test1(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {

Optional message = Optional.ofNullable(record.value());
if (message.isPresent()) {
Object msg = message.get();
log.info("topic_test1 消费了: Topic:" + topic + ",Message:" + msg);
ack.acknowledge();
}
}
}

命令方式

bin/kafka-console-consumer.sh --zookeeper node01:2181 --topic t_cdr --from-beginning

新增 topic(命令方式)

bin/kafka-topics.sh --zookeeper node01:2181 --create --topic t_cdr --partitions 30 --replication-factor 2
详细使用可参考 kafka 官方文档