Docker環境下的kafka部署

1.下載兩個官方imagesdocker

docker pull confluentinc/cp-zookeeperbootstrap

docker pull confluentinc/cp-kafkabash

2.而後建立一個docker-compose.yml 配置以下:server

version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper
    container_name: zookeeper
    mem_limit: 1024M
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
  kafka:
    image: confluentinc/cp-kafka
    container_name: kafka
    mem_limit: 1024M
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_NO: 1
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_HEAP_OPTS: "-Xmx512M -Xms16M"kafka

3.啓動docker-compose:it

docker-compose up -dio

4. 打開兩個新的終端窗口,分別用如下命令登陸docker:console

docker exec -it kafka /bin/bashdocker-compose

5. 在其中一個窗口裏建立topic並運行producertest

kafka-topics --zookeeper zookeeper:2181 --create --replication-factor 1 --partitions 1 --topic kafkatest
kafka-console-producer --broker-list localhost:9092 --topic kafkatest


6.在另外一個窗口裏運行consumer:

kafka-console-consumer --bootstrap-server localhost:9092 --topic kafkatest --from-beginning
 

如今,在producer裏輸入任何內容,都會在consumer裏收到。

相關文章
相關標籤/搜索