生成数据
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 --broker-list 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 --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