HelloKafka入门 helloKafka

该博客介绍了如何启动Zookeeper和Kafka,并提供了HelloKafkaProducer和HelloKafkaConsumer的Java代码示例,展示了如何使用Apache Kafka进行消息生产和消费。
摘要由CSDN通过智能技术生成

一、启动好zookeeper

二、启动好kafka

三、代码类

HelloKafkaProducer.java

import cn.enjoyedu.config.BusiConst;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;


/**
 * 类说明:kafka生产者
 */
public class HelloKafkaProducer {
    
    public static void main(String[] args) {
        Properties properties=new Properties();
        properties.put("bootstrap.servers","127.0.0.1:9092");
        properties.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String,String> producer=new KafkaProducer<String, String>(properties);
        ProducerRecord<String,String> record;
        //TODO 发送4条消息
        try {
            for (int i = 0; i < 4; i++) {
                record=new ProducerRecord<>(BusiConst.HELLO_TOPIC,"1","lison");
                producer.send(record);//发送并忘记
                System.out.println(i+"message is send");;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }


}

HelloKafkaConsumer.java

import cn.enjoyedu.config.BusiConst;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.utils.Time;


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

/**
 * 类说明:kafka消费者
 */
public class HelloKafkaConsumer {

    public static void main(String[] args) {
        // 消费者三个属性必须制定(broker 地址清单、key和value的反序列化器)
        Properties properties=new Properties();
        properties.put("bootstrap.servers","127.0.0.1:9092");
        properties.put("key.deserializer",StringDeserializer.class);
        properties.put("value.deserializer",StringDeserializer.class);
        //TODO 群组并非完全必须
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"test1");
        KafkaConsumer<String,String> consumer=new KafkaConsumer<String, String>(properties);
        try {
            // 消费者订阅主题(可以多个)
            consumer.subscribe(Collections.singletonList(BusiConst.HELLO_TOPIC));
            while (true){
                //拉取
                ConsumerRecords<String,String> records=consumer.poll(Duration.ofMillis(500));
                for (ConsumerRecord<String,String> record:records
                     ) {
                    System.out.println(String.format("topic:%s,分区:%d,偏移量:%d,"+"key:%s,value:%s",record.topic(),record.partition(),
                            record.offset(),record.key(),record.value()));
                }
            }
        } finally {
            consumer.close();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值