Docker容器网络-Kafka Producer

时间:2019-05-07 13:21:26

标签: docker networking apache-kafka docker-compose dockerfile

我想从容器A到容器B中的Kafka主题生成消息,但是这些容器的联网面临一些奇怪的问题。 您对我如何正确连接这些容器有任何想法吗? 问题是收集器服务无法从另一个容器中看到kafka,也无法向其中添加消息。 更具体地说,我有以下服务:

docker-compose.yml

version: '3.5'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
      ADVERTISED_HOST: zookeeper
      ADVERTISED_PORT: 2181
    extra_hosts:
      - "moby:127.0.0.1"
    networks:
      - meetup-net

  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    extra_hosts:
      - "moby:127.0.0.1"
    networks:
      - meetup-net

collector:
    image: collector:v1
    environment:
      - kafka-bootstrap-servers=docker_kafka_1.docker_meetup-net
    restart: always
    depends_on:
      - kafka
    networks:
      - meetup-net
networks:
  meetup-net:
    driver: bridge

另一方面,我有application.conf文件

streaming {
  window-size = 50
  window-interval = 5

  kafka-bootstrap-servers = ${?kafka-bootstrap-servers}
  kafka-bootstrap-servers = "localhost:9092"

  sink-topic = ${?source-topic}
  sink-topic = "meetup"

  key-value-json-path = ${key-value-json-path}
  key-value-json-path = "./data/keyvalue"

  source-topic-checkpoint-location = ${source-topic-checkpoint-location}
  source-topic-checkpoint-location = "./target/source-topic"

  sink-topic-checkpoint-location = ${sink-topic-checkpoint-location}
  sink-topic-checkpoint-location = "./target/sink-topic"
}

zookeeper.server = ${?zookeeper-server}
zookeeper.server = "localhost:2181"

1 个答案:

答案 0 :(得分:2)

您需要正确设置KAFKA_ADVERTISED_LISTENERS

目前,KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092意味着连接到代理的任何客户端都将获得localhost作为代理地址,在该地址上可以进行后续请求。 除非客户端在代理上运行(不在此处),否则您需要更改此配置。对于在独立的Docker env中运行,这很容易:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092

现在,任何客户端连接都应连接到kafka:29092。这也意味着您可以将在Docker主机上运行的客户端 连接到Kafka代理,这可能很有用,例如在笔记本电脑上运行并在本地运行客户端时。

Here是一个示例Docker Compose,显示了这一操作。

有关更多详细信息和背景,请参见https://rmoff.net/2018/08/02/kafka-listeners-explained/