Kafka - 分布式发布订阅消息系统

在这里插入图片描述

Kafka是一个开源的分布式发布订阅消息系统,可以实现高吞吐量的消息处理。下面是一个使用Kafka的Java代码示例:

  1. 生产者代码:
import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class KafkaProducerExample {
    
    

    public static void main(String[] args) {
    
    

        // 设置Kafka生产者的配置
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

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

        // 发送消息
        String topic = "my-topic";
        String key = "my-key";
        String value = "Hello, Kafka!";
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, key, value);
        producer.send(record, new Callback() {
    
    
            @Override
            public void onCompletion(RecordMetadata metadata, Exception exception) {
    
    
                if (exception != null) {
    
    
                    System.out.println("Failed to send message: " + exception.getMessage());
                } else {
    
    
                    System.out.println("Message sent successfully. Offset: " + metadata.offset());
                }
            }
        });

        // 关闭生产者
        producer.close();
    }
}
  1. 消费者代码:
import org.apache.kafka.clients.consumer.*;

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

public class KafkaConsumerExample {
    
    

    public static void main(String[] args) {
    
    

        // 设置Kafka消费者的配置
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("group.id", "my-group");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

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

        // 订阅主题
        String topic = "my-topic";
        consumer.subscribe(Arrays.asList(topic));

        // 拉取和处理消息
        while (true) {
    
    
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
    
    
                System.out.println("Received message: key=" + record.key() + ", value=" + record.value() +
                        ", partition=" + record.partition() + ", offset=" + record.offset());
            }
        }

        // 关闭消费者
        consumer.close();
    }
}

生产者使用KafkaProducer类发送消息,消费者使用KafkaConsumer类订阅主题并拉取和处理消息。你需要将示例中的localhost:9092替换为你的Kafka集群的地址。另外,你还需要确保Kafka服务器已启动,并且创建了名为my-topic的主题。

猜你喜欢

转载自blog.csdn.net/IT_WEH_coder/article/details/142065416