与容器上RabbitMQ的连接问题

时间:2019-01-02 06:25:23

标签: .net docker .net-core docker-compose rabbitmq

更新:事实证明,rabbitmq使用的端口不是15672。我在ConnectionFactory块中将端口号从15672更改为5672并成功连接。

我一直在尝试设计一个简单的微服务架构,以尝试和学习docker&rabbitmq。所以我写了这些docker-compose.yml文件,如下所示:

version: '3.4'

networks:
 customqueue:

services:
  feed.api:
    image: feed.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Feed/Feed.Api/Dockerfile
    depends_on:
     - sqldata
     - rabbitmq
    ports:
     - "8000:80"
    networks:
     - customqueue

  like.api:
    image: like.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Like/Like.Api/Dockerfile
    depends_on:
     - rabbitmq
    ports:
     - "7000:70"
    networks:
     - customqueue

  rabbitmq:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: "admin"
      RABBITMQ_DEFAULT_PASS: "password"
    ports:
      - "15672:15672"
      - "5672:5672" 
    networks:
     - customqueue 

feed.api 被设计为订阅者, like.api 被设计为发布者。但是,当我尝试运行feed.api的.net核心代码时,RabbitMQ出现此“没有端点可达”错误。容器上的RabbitMQ正常工作。我正在尝试在 Feed.Api 项目的 Startup.cs 中定义如下的ConnectionFactory。

var factory = new ConnectionFactory()
{
    HostName = "rabbitmq",
    UserName = "admin",
    Password = "password",
    Port = 15672,
    Protocol = Protocols.DefaultProtocol,
    RequestedConnectionTimeout = 2000,
    VirtualHost = "/",
};

注意:

  • “管理员”用户是管理员。

  • rabbitmq-management插件已启用。

编辑:rabbimq:3-managament-alpine显然是旧图像。将其更新为最新版本可能会有所帮助,但我不确定。有人知道吗?

1 个答案:

答案 0 :(得分:0)

从apis向Rabbitmq容器添加links:部分,否则它们不了解“ rabbitmq”主机名。

泊坞窗命令中不赞成使用链接,但是not in docker-compose

  feed.api:
    image: feed.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Feed/Feed.Api/Dockerfile
    depends_on:
     - sqldata
     - rabbitmq
    links:
     - rabbitmq
    ports:
     - "8000:80"
    networks:
     - customqueue

  like.api:
    image: like.api:${TAG:-latest}
    build:
      context: .
      dockerfile: src/Services/Like/Like.Api/Dockerfile
    depends_on:
     - rabbitmq
    ports:
     - "7000:70"
    links:
     - rabbitmq
    networks:
     - customqueue

  rabbitmq:
    image: rabbitmq:3-management-alpine
    environment:
      RABBITMQ_DEFAULT_USER: "admin"
      RABBITMQ_DEFAULT_PASS: "password"
    ports:
      - "15672:15672"
      - "5672:5672" 
    networks:
     - customqueue