Kafka无法使用docker创建主题

时间:2017-09-13 15:24:07

标签: docker apache-kafka docker-compose

我正在尝试使用docker来测试我们的Kafka听众

两个服务器似乎都能正常启动,因为我能够运行命令

docker exec -it bsbecpromoeventservice_broker_run_ping zookeeper
PING zookeeper (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: icmp_seq=0 ttl=64 time=0.089 ms
64 bytes from 172.20.0.2: icmp_seq=1 ttl=64 time=0.193 ms
64 bytes from 172.20.0.2: icmp_seq=2 ttl=64 time=0.201 ms
^C--- zookeeper ping statistics ---

但是当我尝试运行以下命令时

docker exec -it bsbecpromoeventservice_broker_run_4 kafka-topics --create --zookeeper zookeeper:2 -replication-factor 1 --partitions 1 --topic test

我得到了

[2017-09-13 15:22:43,967] WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)

这是我的docker-compose文件

version: '3'
services:
  zookeeper:
    hostname: zookeeper
    image: confluentinc/cp-zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-enterprise-kafka
    hostname: broker
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker:9092'
      KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:9092
      CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181
      CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
      CONFLUENT_METRICS_ENABLE: 'true'
      CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'

  test:
    image: java:8
    volumes:
        - .:/src
        - ${GRADLE_USER_HOME}:/gradle_mount
    environment:
      - GRADLE_USER_HOME=/gradle_mount
      - SPRING_KAFKA_BOOTSTRAP-SERVERS=broker:9092
    depends_on:
        - broker
        - zookeeper
    working_dir: /src
    privileged: true
    command:  "./gradlew --no-daemon clean test --info"

我如何才能使Kafka服务器能够创建主题?

2 个答案:

答案 0 :(得分:0)

容器运行命令以创建主题

使用容器命令docker run --net=host --rm。在以下示例中,zookeeper正在端口22181上运行,请使用相应的主题名称端口。

创建

docker run   --net=host   --rm   confluentinc/cp-kafka:4.0.0   kafka-topics --create --topic customer --partitions 1 --replication-factor 1 --if-not-exists --zookeeper  localhost:22181

描述

docker run   --net=host   --rm   confluentinc/cp-kafka:4.0.0   kafka-topics --zookeeper localhost:22181 --topic customer --describe

列表

docker run   --net=host   --rm   confluentinc/cp-kafka:4.0.0   kafka-topics --list --zookeeper  localhost:22181

删除

docker run   --net=host   --rm   confluentinc/cp-kafka:4.0.0   kafka-topics  --delete --topic customer --zookeeper localhost:22181

答案 1 :(得分:0)

如果使用的版本低于2.2,则应使用--zookeeper选项并将连接字符串传递给zookeeper

命令将是这样的:

docker exec -it <zookeeper_container_id> kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testLogs

docker exec -it <zookeeper_container_id> kafka-topics --list --zookeeper localhost:2181