在docker中运行时RabbitMq拒绝连接 [英] RabbitMq refuses connection when run in docker

查看:797
本文介绍了在docker中运行时RabbitMq拒绝连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的docker-compose文件如下所示:

My docker-compose file looks like this:

version: '2'

services:
  explore:
    image: explore
    build:
      context: ./Explore
      dockerfile: VsDockerfile
    environment:
      - "ElasticUrl=http://localhost:9200"
      - "RabbitMq/Host=localhost"
      - "RabbitMq/Username=guest"
      - "RabbitMq/Password=guest"
    networks:
      - localnet

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
    container_name: elasticsearch
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - 9200:9200
    volumes:
      - ./esdata:/usr/share/elasticsearch/data
    networks:
      - localnet

  rabbit:
    image: rabbitmq:3.6.7-management
    hostname: rabbit
    ports:
      - 15672:15672
      - 5672:5672
    networks:
      - localnet

networks:
  localnet:
    external:
      name: localnet

如果在浏览器中输入 http:// localhost:15672 ,我将获得Rabbitmq界面,但是如果我尝试通过这样的探索项目连接:

If I type http://localhost:15672 in the browser, I get the rabbitmq interface, but if I tries to connect from my Explore project like this:

public SqlToRabbitProcessor(SqlToRabbitRepository sqlToRabbitRepository)
{
    _sqlToRabbitRepository = sqlToRabbitRepository;

    var factory = new ConnectionFactory
    {
        HostName = Environment.GetEnvironmentVariable("RabbitMq/Host"),
        UserName = Environment.GetEnvironmentVariable("RabbitMq/Username"),
        Password = Environment.GetEnvironmentVariable("RabbitMq/Password")
    };

    var rabbit = factory.CreateConnection();
    channel = rabbit.CreateModel();
}

然后在行中中断

var rabbit = factory.CreateConnection();

,错误提示

ExtendedSocketException:连接被拒绝127.0.0.1:5672
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

ExtendedSocketException: Connection refused 127.0.0.1:5672 System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

ConnectFailureException:连接失败
RabbitMQ.Client .EndpointResolverExtensions.SelectOne(IEndpointResolver解析器,Func选择器)

ConnectFailureException: Connection failed RabbitMQ.Client.EndpointResolverExtensions.SelectOne(IEndpointResolver resolver, Func selector)

BrokerUnreachableException:无法访问任何指定的端点
RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver,字符串clientProvidedName)

BrokerUnreachableException: None of the specified endpoints were reachable RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, string clientProvidedName)

推荐答案

正如我对这个问题的评论所建议的,这是因为在Web应用程序部分中定义的 localhost是

As my comment under the question suggested, it's because the "localhost" defined in the web application part is it's containers localhost, and not the docker host..

只需要更改

- "ElasticUrl=http://localhost:9200"
- "RabbitMq/Host=localhost"

- "ElasticUrl=http://elasticsearch:9200"
- "RabbitMq/Host=rabbit"

这篇关于在docker中运行时RabbitMq拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆