跳转到主要内容

kafka集群搭建

docker-compose.yaml

version: '3.9'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    restart: always
    hostname: zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    volumes:
      - ./zookeeper/data:/data
      - ./zookeeper/datalog:/datalog

  kafka1:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka1
    container_name: kafka1
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.18.32.61:9092 # 消费者需要用此服务码链接,需要做服务发现或者修改为主机ip
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_CREATE_TOPICS: "topic.kafkapartition:compact"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_BROKER_ID: 1
    volumes:
      - ./kafka1/logs:/kafka/logs

  kafka2:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka2
    container_name: kafka2
    ports:
      - "9093:9093"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.18.32.61:9093 # 消费者需要用此服务码链接,需要做服务发现或者修改为主机ip
      KAFKA_LISTENERS: PLAINTEXT://:9093
      KAFKA_CREATE_TOPICS: "topic.kafkapartition:compact"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_BROKER_ID: 2
    volumes:
      - ./kafka2/logs:/kafka/logs


测试运行状态


# 进入到kafka工具集
/opt/kafka_2.12-2.3.0

# broker能够响应请求,通常意味着它正在运行
bin/kafka-broker-api-versions.sh --bootstrap-server kafka1:9092,kafka2:9092

# 查询消费组信息,能够响应请求并返回,通常意味着他正在运行
bin/kafka-consumer-groups.sh --bootstrap-server 172.18.32.61:9093 --list