连接拒绝:无法从Nodejs Docker容器访问RabbitMQ Docker容器

发布于 2025-02-01 17:57:56 字数 3022 浏览 0 评论 0原文

我在连接运行nodejs的Docker容器上的另一个Docker容器运行RabbitMQ时很难。

我谷歌搜索并环顾四周,但没有看到任何专门针对我遇到的问题。我不确定这是我缺少的东西还是其他。

要从nodejs连接,首先我尝试使用 ampqlib 库。这将使我从nodejs拒绝了连接。因此,我尝试寻找其他替代方案,并找到 rascal 。它是围绕ampqlib的包装器。我看到两者都相同的错误:

Error: connect ECONNREFUSED 192.XXX.XXX.X:5672
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.XXX.XXX.X',
  port: 5672,
  broker: Symbol()
}

用于连接的NODEJS的连接字符串:

amqp:// $ {process.env.rabbitmq_default_user}:$ {process. env.rabbitmq_default_pass.rabbitmq_default_pass}美元

​/代码>下面。

这些是从我检查的.env文件中拉入的,并且Nodejs应用程序可以读取它们。

到目前为止,我已经测试过的事情:

  1. 我可以看到RabbitMQ容器正在运行,并准备在端口5672
2022-05-27 15:16:06.422648+00:00 [info] <0.680.0> Ready to start client connection listeners
2022-05-27 15:16:06.429403+00:00 [info] <0.825.0> started TCP listener on [::]:5672
 completed with 4 plugins.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> Server startup complete; 4 plugins started.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_prometheus
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_management
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_web_dispatch
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_management_agent
  1. 进行连接:

    我可以通过port 15672 使用通过ENV变量设置的自定义用户/通过I。我必须配置nginx反向代理,但起作用。

  2. docker-compose相关作品看起来像这样:

  #################
  ### rabbit mq ###
  #################
  rabbitmq:
    hostname: rabbitmq
    container_name: rabbitmq
    image: rabbitmq:management
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
      RABBITMQ_DEFAULT_HOST: ${RABBITMQ_DEFAULT_HOST}
    ports:
      # AMQP protocol port
      - '5672:5672'
      # HTTP management UI
      - '15672:15672'

  ######################
  ### nodejs-client ####
  ######################
  api-client:
    container_name: api-client
    restart: unless-stopped
    build: ./packages/market/api-client
    env_file:
      - ./packages/market/api-client/.env
    depends_on:
      - rabbitmq
      - api
    links:
      - rabbitmq
      - api
  1. 确保用户名/密码没有任何奇怪的字符可能会破坏字符串,因此只需使用test:test:test:test:bot test:bot test:nock and nock and nock of coss。

如果有人以前遇到过这个问题或有任何想法或建议,那么我很感激我很感激!谢谢,

  1. 我没有配置除默认网络以外的任何码头网络。当我做Docker网络检查App_default时,我可以看到Rabbitmqapi-client该网络列出的容器。

I am having trouble connecting a Docker container running NodeJS to another docker container running RabbitMQ.

I googled and looked around but did not see anything specifically for issue I am running into. I am not sure if this something silly I am missing or something else.

To connect from NodeJS, first I tried just using the ampqlib library. And that would give me a connection refused from NodeJS. So then I tried looking for other alternatives and found Rascal. It is a wrapper around ampqlib. I see same error for both:

Error: connect ECONNREFUSED 192.XXX.XXX.X:5672
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.XXX.XXX.X',
  port: 5672,
  broker: Symbol()
}

The connection string from NodeJS used to connect:

amqp://${process.env.RABBITMQ_DEFAULT_USER}:${process.env.RABBITMQ_DEFAULT_PASS}@${process.env.RABBITMQ_DEFAULT_HOST}:${process.env.RABBITMQ_DEFAULT_PORT};`

The HOST for this is RABBITMQ_DEFAULT_HOST=rabbitmq as what I have set in the docker-compose below.

These are pulled in from a .env file that I checked and the NodeJS app can read them fine.

Things I have tested so far:

  1. I can see the rabbitmq container running and ready for connections on port 5672:
2022-05-27 15:16:06.422648+00:00 [info] <0.680.0> Ready to start client connection listeners
2022-05-27 15:16:06.429403+00:00 [info] <0.825.0> started TCP listener on [::]:5672
 completed with 4 plugins.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> Server startup complete; 4 plugins started.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_prometheus
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_management
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_web_dispatch
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0>  * rabbitmq_management_agent
  1. I can access the RabbitMQ management UI through web browser on port 15672 using the custom user/pass I set through env variables. I had to configure the NGINX reverse proxy but it worked.

  2. The docker-compose for relevant pieces looks like this:

  #################
  ### rabbit mq ###
  #################
  rabbitmq:
    hostname: rabbitmq
    container_name: rabbitmq
    image: rabbitmq:management
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
      RABBITMQ_DEFAULT_HOST: ${RABBITMQ_DEFAULT_HOST}
    ports:
      # AMQP protocol port
      - '5672:5672'
      # HTTP management UI
      - '15672:15672'

  ######################
  ### nodejs-client ####
  ######################
  api-client:
    container_name: api-client
    restart: unless-stopped
    build: ./packages/market/api-client
    env_file:
      - ./packages/market/api-client/.env
    depends_on:
      - rabbitmq
      - api
    links:
      - rabbitmq
      - api
  1. Making sure the username/password did not have any strange characters that could break the string, so just using things like test:test but still no luck.

If anyone has run into this before or has any ideas or suggestions I would greatly appreciate as I am stumped! Thank you

  1. I am not configuring any Docker network other than the default one. When I do docker network inspect app_default I can see the rabbitmq and api-client containers listed for this network.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2025-02-08 17:57:56

我弄清楚了这个问题:我需要让Nodejs应用程序等待一些时间,然后再尝试连接到RabbitMQ容器。我将settimeout等待几分钟,并且能够成功连接。

我不知道有更好的等待方式。我之前曾尝试过Wait-for-it,也许我会编写一个特定于我的设置的自定义bash脚本,以便在一段时间之后仅启动nodejs容器。

I figured out the issue: I needed to have the NodeJS app wait some time before trying to connect to RabbitMQ container. I put a setTimeout to wait for a couple of minutes and it was able to successfully connect.

I do not know of a better way to wait. I have tried wait-for-it before, maybe I will write a custom bash script that is specific to my setup to only start the NodeJS container after some time has passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文