跳转至

🚡 Kafka

参考



MQ

  • 消息队列,程序间通信的一种方式

why

  • 解耦

  • 异步

  • 削峰

how

waht



安装

  1. 使用 docker 启动

    Bash
    1
    sudo docker run -d --rm -p 9092:9092 apache/kafka
    
  2. 创建一个主题

    Bash
    1
    sudo docker exec -it <container_id> /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test-topic
    
  3. 生产者

    Bash
    1
    sudo docker exec -it <container_id> /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
    

    输入消息后按 Ctrl+C 结束

  4. 消费者

    Bash
    1
    sudo docker exec -it <container_id> /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning
    



C++

Docker
1
2
3
4
5
FROM ubuntu:latest AS builder

RUN apt update && apt install -y \
    build-essential cmake pkg-config \
    librdkafka-dev libglib2.0-dev

生成镜像:

Bash
1
sudo docker build -t dxlcq/kafka_builder_cpp .

编译:

Bash
1
2
sudo docker run --rm -v /kafka-c-getting-started:/kafka-c-getting-started -w /kafka-c-getting-started/build dxlcq/kafka_builder_cpp cmake ..
sudo docker run --rm -v /kafka-c-getting-started:/kafka-c-getting-started -w /kafka-c-getting-started/build dxlcq/kafka_builder_cpp cmake --build .

消费者:

Bash
1
sudo docker run --rm --net=host -v /kafka-c-getting-started:/kafka-c-getting-started -w /kafka-c-getting-started/build dxlcq/kafka_builder_cpp ./consumer

生产者:

Bash
1
sudo docker run --rm --net=host -v /kafka-c-getting-started:/kafka-c-getting-started -w /kafka-c-getting-started/build dxlcq/kafka_builder_cpp ./producer