我的docker-compose文件如下所示:
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界面,但如果我尝试从我的探索项目中连接如下:
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:Connection拒绝127.0.0.1:5672 System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
ConnectFailureException:连接失败 RabbitMQ.Client.EndpointResolverExtensions.SelectOne(IEndpointResolver解析器,Func选择器)
BrokerUnreachableException:没有指定的端点可以访问 RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver,string clientProvidedName)
答案 0 :(得分:6)
正如我在问题提出的评论中所说,这是因为Web应用程序部分中定义的“localhost”是容器localhost,而不是docker主机..
只需改变
- "ElasticUrl=http://localhost:9200"
- "RabbitMq/Host=localhost"
到
- "ElasticUrl=http://elasticsearch:9200"
- "RabbitMq/Host=rabbit"
答案 1 :(得分:0)
我在docker-compose上遇到了同样的问题。
我用主机名解决了这个问题
rabbit:
hostname: rabbit
command: sh -c "rabbitmq-plugins enable rabbitmq_management; rabbitmq-server"
image: rabbitmq
environment:
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: admin
ports:
- 5672:5672
- 15672:15672
答案 2 :(得分:0)
遵循有关custom runtimes的说明。
只是让那些偶然遇到这个问题的人受益。 --link功能现在被认为是旧功能,是docker不推荐使用的主要候选方法。 最简单的方法是使用
depends_on: 为此,建议先创建一个像这样的网络。
docker network create <network_name>
然后使用docker-compose up
生成彼此绑定的服务。在下面的示例中,我将我的spring-boot应用程序绑定到rabbit-mq。您可以从post
version: "3.1"
services:
rabbitmq-container:
image: rabbitmq:3.5.3-management
hostname: rabbitmq-container
ports:
- 5673:5673
- 5672:5672
- 15672:15672
networks:
- resolute
resolute-container:
build: .
ports:
- 8080:8080
environment:
- spring_rabbitmq_host=rabbitmq-container
- spring_rabbitmq_port=5672
- spring_rabbitmq_username=guest
- spring_rabbitmq_password=guest
- resolute_rabbitmq_publishQueueName=resolute-run-request
- resolute_rabbitmq_exchange=resolute
depends_on:
- rabbitmq-container
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- resolute
networks:
resolute:
external:
name: resolute
了解如何创建名为resolute
的网络并将应用程序绑定到同一网络。我还给了Rabbitmq容器一个hostname
。这是因为docker现在在容器名称前加上了前缀,这使得按名称绑定服务变得很困难。