[英]Why I have a connection refused error when starting rabbit by docker-compose?
I run sprinboot app, front (in react), database and rabbitmq in docker-compose.我在 docker-compose 中运行 sprinboot 应用程序、前端(在反应中)、数据库和rabbitmq。 When I run it everything works except rabbitmq.
当我运行它时,除了rabbitmq 之外,一切正常。 I had an error - "Connection refused".
我有一个错误 - “连接被拒绝”。
docker-compose:码头工人组成:
version: '3'
services:
server:
build: ./mlogger
ports:
- "8000:8080"
depends_on:
- database
restart: always
web:
build: ./web
ports:
- "4000:3000"
tty: true
depends_on:
- server
database:
container_name: 'mysql'
image: mysql
environment:
MYSQL_ROOT_PASSWORD: jitsi1234
MYSQL_USER: jitsiteam
MYSQL_DATABASE: testmlogger
ports:
- "3306:3306"
restart: always
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
command: sh -c "rabbitmq-plugins enable rabbitmq_auth_backend_ldap"
ports:
- "5672:5672"
- "15672:15672"
restart: always
error:错误:
server_1 | 2020-09-05 16:14:42.110 INFO 6 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
server_1 | 2020-09-05 16:14:42.117 ERROR 6 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
server_1 |
server_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
server_1 | at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:524) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2089) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2062) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2042) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1836) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1817) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1349) [spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1195) [spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
server_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
server_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_265]
server_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_265]
server_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_265]
server_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_265]
server_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1137) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1087) ~[amqp-client-5.9.0.jar!/:5.9.0]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:560) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:533) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:487) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
server_1 | ... 12 common frames omitted
Spring boot properties:弹簧靴属性:
spring.rabbitmq.address=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
I'm using ldap also, and I want to add users to rabbitmq on start.我也在使用 ldap,我想在开始时将用户添加到 rabbitmq。 I can do it by adding commands to docker-compose?
我可以通过向 docker-compose 添加命令来实现吗?
Spring boot ldap config: Spring 启动 LDAP 配置:
@Configuration
@EnableLdapRepositories
public class LdapConfig {
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://localhost:18889");
contextSource.setBase("dc=example,dc=com");
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
}
I figured it out, in the docker compose part responsible for the spring application, I had to specify the network mode :我想通了,在负责spring应用程序的docker compose部分,我必须指定网络模式:
server:
build: ./mlogger
ports:
- "8000:8080"
depends_on:
- database
- rabbitmq
network_mode: "host"
Inside a container, localhost
is the container itself, not the host it is running on.在容器内,
localhost
是容器本身,而不是运行它的主机。 Components are visible to each other with their service names.组件通过其服务名称彼此可见。 Insead of connecting to localhost:5672, connect to rabbitmq:5672, and it should work.
Insead 连接到 localhost:5672,连接到 rabbitmq:5672,它应该可以工作。 Do the same for the database as well.
对数据库也做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.