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#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配