生成数据
Java 代码方式
@Component@Slf4jpublic class KafkaProducer {@Autowiredprivate KafkaTemplate<String, Object> kafkaTemplate;//自定义topicpublic 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>>() {@Overridepublic void onFailure(Throwable throwable) {//发送失败的处理log.info(TOPIC_TEST + " - 生产者 发送消息失败:" + throwable.getMessage());}@Overridepublic void onSuccess(SendResult<String, Object> stringObjectSendResult) {//成功的处理log.info(TOPIC_TEST + " - 生产者 发送消息成功:" + stringObjectSendResult.toString());}});}}
命令方式
bin/kafka-console-producer.sh --bootstrap-server node86:9092 --topic t_cdr
消费数据
Java 代码方式
@Component@Slf4jpublic 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 --bootstrap-server node86:9092 --topic t_cdr --from-beginning
新增 topic
命令方式
bin/kafka-topics.sh --bootstrap-server node86:9092 --create --topic t_cdr --partitions 30 --replication-factor 2
Kafka3.x 连接方式
Kafka 3.x 已移除命令行工具的 --zookeeper 参数,所有脚本统一使用 --bootstrap-server 连接 Broker,端口默认为 9092。--bootstrap-server 后填写 Kafka Broker 节点的内网 IP 或主机名(例如 node86:9092),多个 Broker 之间用英文逗号分隔。
场景 | 命令 |
生产消息 | bin/kafka-console-producer.sh --bootstrap-server node86:9092 --topic t_cdr |
消费消息 | bin/kafka-console-consumer.sh --bootstrap-server node86:9092 --topic t_cdr --from-beginning |
创建 topic | bin/kafka-topics.sh --bootstrap-server node86:9092 --create --topic t_cdr --partitions 30 --replication-factor 2 |
查看 topic 列表 | bin/kafka-topics.sh --bootstrap-server node86:9092 --list |
查看消费组 | bin/kafka-consumer-groups.sh --bootstrap-server node86:9092 --list |
客户端代码只需将 bootstrap.servers 配置为 Kafka Broker 地址(例如 node86:9092),无需再配置 ZooKeeper 地址。