【Kafka】Java整合Kafka

1.引入依赖

XML 复制代码
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>2.3.1</version>
        </dependency>

2.搭建生产者

java 复制代码
package com.wen.kafka;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class MyProducer {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //配置信息
        Properties prop = new Properties();
        prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.117.80:9092");
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        //创建生产者
        Producer<String,String> producer = new KafkaProducer<String, String>(prop);

        //创建消息
        ProducerRecord<String,String> record = new ProducerRecord<>("test", "hello kafka-client");

        //同步发送消息
//        RecordMetadata metadata = producer.send(record).get();
//        System.out.println("同步消息------topic:"+metadata.topic()+"partition"+metadata.partition()+"offset"+metadata.offset());

        //异步发送消息
        producer.send(record, new Callback() {
            @Override
            public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                if (e != null) {
                    System.out.println(e.getMessage());
                }
                if (recordMetadata != null) {
                    System.out.println("异步消息------topic:"+recordMetadata.topic()+"partition"+recordMetadata.partition()+"offset"+recordMetadata.offset());
                }
            }
        });
        Thread.sleep(1000);
    }
}

3.搭建消费者

java 复制代码
package com.wen.kafka;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class MyConsumer {

    public static void main(String[] args) {
        //参数信息
        Properties prop = new Properties();
        prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.117.80:9092");
        prop.put(ConsumerConfig.GROUP_ID_CONFIG,"testGroup");
        prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());

        //创建消费者
        Consumer<String,String> consumer = new KafkaConsumer<String, String>(prop);

        //订阅主题
        consumer.subscribe(Arrays.asList("test"));

        //拉取消息
        while (true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.value());
            }
        }
    }
}
相关推荐
Yan.love2 分钟前
开发场景中Java 集合的最佳选择
java·数据结构·链表
椰椰椰耶5 分钟前
【文档搜索引擎】搜索模块的完整实现
java·搜索引擎
大G哥6 分钟前
java提高正则处理效率
java·开发语言
智慧老师36 分钟前
Spring基础分析13-Spring Security框架
java·后端·spring
lxyzcm38 分钟前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
V+zmm101341 小时前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
Oneforlove_twoforjob2 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
xmh-sxh-13142 小时前
常用的缓存技术都有哪些
java
AiFlutter2 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
J不A秃V头A3 小时前
IntelliJ IDEA中设置激活的profile
java·intellij-idea