dongzecai0684 2019-05-30 14:47
浏览 230
已采纳

容器启动时应用程序错误地解析了Docker别名

I am creating two docker-compose files (mainly because I don't want to have to keep restarting my infrastructure while developing my application.) that need to reside on the same docker network so they can use alias names to connect.

The files look similar to the following:

APP:

version: '3.5'

networks:
  default:
    name: kafka_network
    driver: bridge
services:
  client:
    build:
      context: .
      dockerfile: ./Dockerfile
    working_dir: /app/
    command: ./client
    environment:
      BADDR: kafka:9092
      CGROUP: test_group
      TOPICS: my-topic

INFRASTRUCTURE:

version: '3.5'

networks:
  default:
    name: kafka_network
    driver: bridge

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    ports:
      - 2181:2181
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  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://kafka:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

My issue is that the client doesn't resolve kafka:9092 correctly... it always resolves to 127.0.0.1:9092.

ERROR:

Broker:  kafka:9092
Consumer_Group:  my_group
Topics:  [my-topic]
Created Consumer rdkafka#consumer-1
% Error: GroupCoordinator: Connect to ipv4#127.0.0.1:9092 failed: Connection refused (after 0ms in state CONNECT)

When run locally, it appears to run fine, so I am really confused as to what the issue might be. If anyone knows anything about this I would be very grateful!

LOCAL:

[procyclinsur@P-428 client]$ ./client
Broker:  localhost:9092
Consumer_Group:  my-group
Topics:  [my-topic]
Created Consumer rdkafka#consumer-1
% AssignedPartitions: [my-topic[0]@unset]
% Message on my-topic[0]@0:
hello mate
  • 写回答

1条回答 默认 最新

  • dskyx46424 2019-05-30 14:58
    关注

    That's problem related to your Kafka's config - not to docker at all.

    Look on:

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

    It means that you setup 2 listeners for your Kafka which your clients will receive in Kafka's protocol when connecting.

    So when you connect on port 9092 your client's will try to get Kafka at "localhost "and when you connect at port 29092 your clients will try to get Kafka at "kafka" DNS name.

    It's working locally for you because your Kafka container is exposed on localhost:9092 via docker ports section.

    Here is article which is well describing that topic: https://rmoff.net/2018/08/02/kafka-listeners-explained/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置