Docker-compose 如何重复启动一个服务,以支持多消费者模型

图片.png

上图描述了我的工作流程,我希望可以有多个消费者来快速消化 RabbitMQ queue 中的消息

👇下面是我的 docker-compose.yml 文件:

version: "3"
services:
  http_api_service:
    container_name: http_api_service
    image: vo/service
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    ports:
      - "63000:63000"
    command: uvicorn api:app --host '0.0.0.0' --port 63000

  console_service:
    container_name: console_service
    image: vo/service
    network_mode: "host"
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    command: nameko run services:ConsoleService --config ./config.yaml
  • http_api_service 即为 MQ 生产者角色,接受 HTTP 请求,并把消息放到 MQ 中
  • console_service 即为 MQ 消费者角色,由 MQ 主动 push 消息,或者主动去 MQ 中获取消息

我发现 console_service 在一个进程下的消费能力比较欠缺,希望通过多进程,多开启一些消费者进程来提高消费速度

此时我有以下两种方案:

  • 方案一:在同一个容器中运行多个消费者进程,通过一个容器内的多进程技术来增加消费者的数量
  • 方案二:一个容器一个消费者进程,通过多开容器来增加消费者的数量

如果采用前一种方案,存在一个问题,就是无法动态增减消费者的数量,需要通过 restart 容器的方式来增减消费者的数量,而且这貌似也违反了官方一个容器一个进程的使用建议

如果采用第二种方案,如何在 docker-compose 中优雅的实现呢?

假设用下面的方式:

version: "3"
services:
  http_api_service:
    container_name: http_api_service
    image: vo/service
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    ports:
      - "63000:63000"
    command: uvicorn api:app --host '0.0.0.0' --port 63000

  console_service00:
    container_name: console_service00
    image: vo/service
    network_mode: "host"
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    command: nameko run services:ConsoleService --config ./config.yaml
    
  console_service01:
    container_name: console_service01
    image: vo/service
    network_mode: "host"
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    command: nameko run services:ConsoleService --config ./config.yaml
    
  console_service02:
    container_name: console_service02
    image: vo/service
    network_mode: "host"
    env_file:
      - .env
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "1"
    command: nameko run services:ConsoleService --config ./config.yaml

👆 手工在 console_service 后面加上 000102 这样子的后缀,但是这样子非常的不优雅!!!!🤯 请问如何结果这个问题呢?

貌似没有查询到 docker-compose 对这方面的支持

鄙人目前没有使用过 Docker SwarmKubernetes,不知道这个问题是否可以用这两个容器编排工具解决呢?
阅读 4.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题