消息队列:Kafka

Posted by     "zengchengjie" on Wednesday, February 24, 2021

相关链接

kafka常见面试题

docker-compose部署kafka集群_修理男爵的博客-程序员资料_docker-compose部署kafka集群

部署

docker部署kafka

干啥都不如官方文档清晰和权威,所以,我们直接上dockerhub:https://hub.docker.com/r/bitnami/kafka

$ curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-kafka/master/docker-compose.yml > docker-compose.yml
$ docker-compose up -d

源文件如下:

version: "2"

services:
  zookeeper:
    image: docker.io/bitnami/zookeeper:3.7
    ports:
      - "2181:2181"
    volumes:
      - "zookeeper_data:/bitnami"
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: docker.io/bitnami/kafka:3
    ports:
      - "9092:9092"
    volumes:
      - "kafka_data:/bitnami"
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper

volumes:
  zookeeper_data:
    driver: local
  kafka_data:
    driver: local

不过使用kafka客户端工具连接时,会出现报错:kafka tool unnable to connect broker 1001

这时候需要配置KAFKA_CFG_ADVERTISED_LISTENERS以支持外部客户端访问:

version: "2"

services:
  zookeeper:
    image: docker.io/bitnami/zookeeper:3.7
    ports:
      - "2181:2181"
    volumes:
      - "zookeeper_data:/bitnami"
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: docker.io/bitnami/kafka:3
    ports:
      - "9092:9092"
    volumes:
      - "kafka_data:/bitnami"
      - "/home/kafka/kafka-persistence:/bitnami/kafka"
    environment:
      #- KAFKA_BROKER_ID=1
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.10.49:9092
    depends_on:
      - zookeeper

volumes:
  zookeeper_data:
    driver: local
  kafka_data:
    driver: local

如上配置文件所示:配置了 属性:- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092 - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.10.49:9092

以后,kafka-tool能够正常连接了

消费kafka数据到MongoDB

docker部署kafka-connector

docker pull hebertrujillo/kafka-connectors