1

我正在尝试在 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.confluent.kafkarest.KafkaRestMain)”之类的消息

我不知道我做错了什么我尝试按照其他帖子上的一些说明进行操作:

4

1 回答 1

3

鉴于 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

于 2019-10-21T12:02:53.603 回答