简体   繁体   English

Docker容器网络-Kafka Producer

[英]Docker container networking - Kafka Producer

I would like to produce messages from a container A to a Kafka topic in a container B, but I am facing some weird issues with the networking of these containers. 我想从容器A到容器B中的Kafka主题生成消息,但是这些容器的联网面临一些奇怪的问题。 Do you have any idea on how I can connect those containers in a proper way? 您对我如何正确连接这些容器有任何想法吗? The problem is that the collector service cannot see the kafka from the other container and cannot add messages to it. 问题是收集器服务无法从另一个容器中看到kafka,也无法向其中添加消息。 More specifically I have the services below: 更具体地说,我有以下服务:

docker-compose.yml 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

and on the other side I have the application.conf file 另一方面,我有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"

You need to set KAFKA_ADVERTISED_LISTENERS correctly. 您需要正确设置KAFKA_ADVERTISED_LISTENERS

At the moment KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092 means that any client connecting to the broker will get localhost as the broker address on which to connect for subsequent requests. 目前, KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092表示,连接到代理的任何客户端都将获得localhost作为代理地址,在该代理地址上进行后续请求。 Unless the client is running on the broker (which it isn't here) then you need to change this configuration. 除非客户端在代理上运行 (不在此处),否则您需要更改此配置。 For running in a self-contained Docker env this is easy enough: 对于在独立的Docker env中运行,这很容易:

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

Now any client connection should be to kafka:29092 . 现在,任何客户端连接都应该是kafka:29092 This also means that you can connect a client running on your Docker host to the Kafka broker which can be useful eg when running on a laptop and running a client locally. 这也意味着您可以将在Docker主机上运行的客户端连接到Kafka代理,这很有用,例如在笔记本电脑上运行并在本地运行客户端时。

Here is a sample Docker Compose showing this in action. 是一个示例Docker Compose,显示了实际操作。

For more details and background, see https://rmoff.net/2018/08/02/kafka-listeners-explained/ 有关更多详细信息和背景,请参阅https://rmoff.net/2018/08/02/kafka-listeners-explained/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM