java 访问kafka

1 添加maven依赖

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.12</artifactId>
            <version>2.1.1</version>
        </dependency>

2 创建消息生产者

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class MyKafkaClient {

    private static MyKafkaClient instance;
    private static final Properties properties = new Properties();
    KafkaProducer<String, String> producer;
    static {
        properties.put("bootstrap.servers","localhost:9092");
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("max.request.size","10485760");
        properties.put("batch.size","163840");
        properties.put("buffer.memory","163840");
        properties.put("max.block.ms","500");
        properties.put("retries","3");
        properties.put("acks","1");
    }

    private MyKafkaClient(){
         producer = new KafkaProducer<String, String>(properties);

    }

    public static MyKafkaClient getInstance(){
        if(null == instance){
            instance = new MyKafkaClient();
        }
        return instance;
    }

    public void sendMessage(Map<String,String> msg) throws Exception{
        for (Map.Entry<String, String> entry : msg.entrySet()) {
            String topic = entry.getKey();
            String message = entry.getValue();
            ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic,"myTest",message);
            // 发送
            try {
                producer.send(record);
                System.err.println("====================发送消息成功..................");

            }catch (Exception e){
                System.err.println("====================发送消息失败..................");
                e.printStackTrace();
            }
        }
        producer.flush();
        producer.close();

    }



    public String getMessageByTopic(String topic){
       return null;
    }
    public static void main(String[] args) throws Exception{
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("test","this is my message 13");
        MyKafkaClient instance = MyKafkaClient.getInstance();

        instance.sendMessage(hashMap);
    }

}

3、创建消息消费者

import java.util.Arrays;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;


public class MyKafkaConsumer {
    private static  MyKafkaConsumer instance;

    private KafkaConsumer<String, String> consumer;
    private static final Properties properties = new Properties();
    static {
        properties.put("bootstrap.servers","localhost:9092");
        properties.put("group.id","test12");
        properties.put("session.timeout.ms","30000");
        properties.put("enable.auto.commit", "true");
        properties.put("auto.commit.interval.ms", "1000");
        properties.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");

    }
    private MyKafkaConsumer(){
         consumer = new KafkaConsumer<>(properties);
         consumer.subscribe(Arrays.asList("test"));

    }

    public static MyKafkaConsumer getInstance(){
        if(null == instance){
            instance = new MyKafkaConsumer();
        }
        return instance;
    }


    public String getMessage(){
            ConsumerRecords<String, String> poll = consumer.poll(10000);
            for (ConsumerRecord<String, String> record:poll) {
                System.out.println(String.format("===================>>>>>getMessage key:%s, value:%s",record.key(),record.value()));
            }
            return "";
    }

    public static void main(String[] args) {
        MyKafkaConsumer instance = MyKafkaConsumer.getInstance();
        instance.getMessage();
    }
}

4、结果验证

===================>>>>>getMessage key:null, value:this is my message hahahahaha
===================>>>>>getMessage key:null, value:this is my message 12
===================>>>>>getMessage key:null, value:this is my message 13
===================>>>>>getMessage key:myTest, value:this is my message 13
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值