如何在Docker Compose中等待RabbitMQ接受连接

huangapple go评论118阅读模式
英文:

how to wait for rabbitmq to accept connections in docker compose

问题

如何在Docker Compose中等待RabbitMQ接受连接?

我有一个类似于以下代码片段的设置:

  1. version: "3.9"
  2. services:
  3. rabbitmq:
  4. image: rabbitmq:management
  5. service_A:
  6. depends_on:
  7. - rabbitmq
  8. service_B:
  9. depends_on:
  10. - rabbitmq

当我运行docker compose up时,它不会等待RabbitMQ完全加载,而是运行service_A和service_B。当这两个服务运行时,它尝试连接到RabbitMQ并失败,然后退出,然后过了一段时间RabbitMQ准备好接受连接。

是否有一种标准的处理此情况的方法,或者甚至是绕过此问题的技巧?

注意:这两个服务都是用Rust编写的,并使用lapin crate连接到RabbitMQ。

英文:

How to wait for rabbitmq to accept connections in docker compose ?

I have a similar setup to the following snippet

  1. version: "3.9"
  2. services:
  3. rabbitmq:
  4. image: rabbitmq:management
  5. service_A:
  6. depends_on:
  7. - rabbitmq
  8. service_B:
  9. depends_on:
  10. - rabbitmq

When I run docker compose up It doesn't wait for rabbitmq to fully load, Instead it runs service_A and service_B. When both of the services run, It tries to connect to rabbitmq and fails and exists then after a while rabbitmq is ready to accept connections.

Is there a standard way to handle this situation, or even a hack to go around this issue ?

  • note: both services are written in rust and using lapin crate to connect to rabbitmq.

答案1

得分: 1

你需要在rabbitmq上使用健康检查,并等待服务变为健康状态,而不仅仅是容器启动:

  1. version: '3.8'
  2. services:
  3. service_A:
  4. depends_on:
  5. rabbit:
  6. condition: service_healthy
  7. rabbit:
  8. image: rabbitmq:management
  9. ports:
  10. - "15672:15672" # leave management port open
  11. - "5672:5672"
  12. healthcheck:
  13. test: ["CMD", "curl", "-f", "http://localhost:15672"]
  14. interval: 30s
  15. timeout: 10s
  16. retries: 5
英文:

You'll want to use a healthcheck on rabbitmq and wait for the service to be healthy, not just for the container to start:

  1. version: '3.8'
  2. services:
  3. service_A:
  4. depends_on:
  5. rabbit:
  6. condition: service_healthy
  7. rabbit:
  8. image: rabbitmq:management
  9. ports:
  10. - "15672:15672" # leave management port open
  11. - "5672:5672"
  12. healthcheck:
  13. test: ["CMD", "curl", "-f", "http://localhost:15672"]
  14. interval: 30s
  15. timeout: 10s
  16. retries: 5
  17. </details>

huangapple
  • 本文由 发表于 2023年8月8日 21:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定