为什么我在使用docker-compose启动Rabbit时会出现连接被拒绝的错误?

huangapple go评论70阅读模式
英文:

Why I have a connection refused error when starting rabbit by docker-compose?

问题

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)
...

Spring boot properties:

spring.rabbitmq.address=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

Spring boot ldap config:

@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 run sprinboot app, front (in react), database and rabbitmq in docker-compose. When I run it everything works except 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. I can do it by adding commands to docker-compose?

Spring boot ldap config:

@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());
    }
}

答案1

得分: 1

我弄清楚了,在负责Spring应用程序的Docker Compose部分,我必须指定网络模式

server:
 build: ./mlogger
 ports:
  - "8000:8080"
 depends_on:
  - database
  - rabbitmq
 network_mode: "host"
英文:

I figured it out, in the docker compose part responsible for the spring application, I had to specify the network mode:

server:
 build: ./mlogger
 ports:
  - "8000:8080"
 depends_on:
  - database
  - rabbitmq
 network_mode: "host"

答案2

得分: 0

在容器内部,localhost 表示的是容器本身,而不是它运行在的主机。各组件可以通过其服务名称相互访问。所以,不要连接到 localhost:5672,而是连接到 rabbitmq:5672,这样应该就可以正常工作。对于数据库也是同样的方式。

英文:

Inside a container, localhost is the container itself, not the host it is running on. Components are visible to each other with their service names. Insead of connecting to localhost:5672, connect to rabbitmq:5672, and it should work. Do the same for the database as well.

huangapple
  • 本文由 发表于 2020年9月6日 00:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定