docker - 在 Windows 中使用 Docker Compose 运行 Kafka 时出现问题

标签 docker apache-kafka docker-compose kafka-rest

我正在尝试在 Windows 10 Pro 和 Docker Desktop(不是工具箱)上本地运行 Kafka。一切似乎都很完美,但我无法使用我的应用程序访问 Kafka,也无法使用 kafka rest ( http://localhost:8082/topics | http://127.0.0.1:8082/topics | http://192.168.1.103:8082/topics - 最后一个是我在主机中的 docker ip)

我的 docker-compose 文件是:

version: '2'

services:

  # https://hub.docker.com/r/confluentinc/cp-zookeeper/tags
  zookeeper:
    image: confluentinc/cp-zookeeper:5.3.1
    container_name: zookeeper
    hostname: zookeeper
    network_mode: host
    ports:
      - "2181:2181"
      - "32181:32181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181

  # https://hub.docker.com/r/confluentinc/cp-kafka/tags
  kafka:
    image: confluentinc/cp-kafka:5.3.1
    container_name: kafka
    hostname: kafka
    network_mode: host
    ports:
      - "9092:9092"
      - "29092:29092"
    restart: always
    environment:
      KAFKA_ADVERTISED_HOST_NAME: localhost
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_ZOOKEEPER_CONNECT: localhost:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_BROKER_ID: 2
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    depends_on:
      - zookeeper

  schema-registry:
    image: confluentinc/cp-schema-registry:5.3.1
    hostname: schema-registry
    container_name: schema-registry
    network_mode: host
    depends_on:
      - zookeeper
      - kafka
    ports:
      - "8081:8081"
    environment:
      SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: localhost:2181
      SCHEMA_REGISTRY_HOST_NAME: localhost
      SCHEMA_REGISTRY_LISTENERS: http://localhost:8081

  kafka-rest:
    image: confluentinc/cp-kafka-rest:5.3.1
    hostname: kafka-rest
    container_name: kafka-rest
    network_mode: host
    depends_on:
      - zookeeper
      - kafka
    ports:
      - "8082:8082"
    environment:
      KAFKA_REST_HOST_NAME: localhost
      KAFKA_REST_ZOOKEEPER_CONNECT: localhost:2181
      KAFKA_REST_LISTENERS: http://localhost:8082
      KAFKA_REST_SCHEMA_REGISTRY_URL: http://localhost:8081

我的主机文件是:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
192.168.1.103 host.docker.internal
192.168.1.103 gateway.docker.internal
# Added by Docker Desktop
192.168.2.236 host.docker.internal
192.168.2.236 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
127.0.0.1 kafka

在日志中我收到了类似的消息 “kafka-rest | [2019-10-21 11:40:57,903] INFO 服务器已启动,正在监听请求...(io.confluence.kafkarest.KafkaRestMain)”

我不知道我做错了什么 我尝试遵循其他帖子上的一些说明:

最佳答案

考虑到 Docker 涉及的网络,您需要正确配置 Kafka 监听器。这篇文章解释了如何:https://rmoff.net/2018/08/02/kafka-listeners-explained/

您可以在此处找到包含主机访问的工作 Docker Compose:https://github.com/confluentinc/examples/blob/5.3.1-post/cp-all-in-one/docker-compose.yml

关于docker - 在 Windows 中使用 Docker Compose 运行 Kafka 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58485835/

相关文章:

apache-kafka - Kafka : How to Display Offsets

docker - Docker-compose + Nginx + Certbot +简单的Django Rest Framework应用

Maven:找不到前缀 'dockerfile' 的插件

windows - Docker Desktop 2.2+ 在主机卷挂载时性能极其缓慢

javascript - 使用kafka消费者进行数据库操作或http调用

java.lang.ClassCastException :xx cannot be cast to org. apache.avro.generic.IndexedRecord

docker-compose - 为什么 docker-compose 'down' 不采用可选的 [SERVICE...] 参数?

python - 如何将 Flask 应用程序连接到在 Docker 中运行的 SQLite DB?

docker - 通过 Dockerfile 在 Ubuntu 上安装 Google Chrome 并点击地理区域

docker - 如何在 Windows 的 Docker 上增加交换大小