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

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

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

问题

docker-compose:

  1. version: '3'
  2. services:
  3. server:
  4. build: ./mlogger
  5. ports:
  6. - "8000:8080"
  7. depends_on:
  8. - database
  9. restart: always
  10. web:
  11. build: ./web
  12. ports:
  13. - "4000:3000"
  14. tty: true
  15. depends_on:
  16. - server
  17. database:
  18. container_name: 'mysql'
  19. image: mysql
  20. environment:
  21. MYSQL_ROOT_PASSWORD: jitsi1234
  22. MYSQL_USER: jitsiteam
  23. MYSQL_DATABASE: testmlogger
  24. ports:
  25. - "3306:3306"
  26. restart: always
  27. rabbitmq:
  28. image: rabbitmq:3-management
  29. container_name: rabbitmq
  30. command: sh -c "rabbitmq-plugins enable rabbitmq_auth_backend_ldap"
  31. ports:
  32. - "5672:5672"
  33. - "15672:15672"
  34. restart: always

error:

  1. 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]
  2. 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).
  3. server_1 |
  4. server_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
  5. ...

Spring boot properties:

  1. spring.rabbitmq.address=localhost
  2. spring.rabbitmq.port=5672
  3. spring.rabbitmq.username=guest
  4. spring.rabbitmq.password=guest

Spring boot ldap config:

  1. @Configuration
  2. @EnableLdapRepositories
  3. public class LdapConfig {
  4. @Bean
  5. public LdapContextSource contextSource() {
  6. LdapContextSource contextSource = new LdapContextSource();
  7. contextSource.setUrl("ldap://localhost:18889");
  8. contextSource.setBase("dc=example,dc=com");
  9. return contextSource;
  10. }
  11. @Bean
  12. public LdapTemplate ldapTemplate() {
  13. return new LdapTemplate(contextSource());
  14. }
  15. }
英文:

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:

  1. version: '3'
  2. services:
  3. server:
  4. build: ./mlogger
  5. ports:
  6. - "8000:8080"
  7. depends_on:
  8. - database
  9. restart: always
  10. web:
  11. build: ./web
  12. ports:
  13. - "4000:3000"
  14. tty: true
  15. depends_on:
  16. - server
  17. database:
  18. container_name: 'mysql'
  19. image: mysql
  20. environment:
  21. MYSQL_ROOT_PASSWORD: jitsi1234
  22. MYSQL_USER: jitsiteam
  23. MYSQL_DATABASE: testmlogger
  24. ports:
  25. - "3306:3306"
  26. restart: always
  27. rabbitmq:
  28. image: rabbitmq:3-management
  29. container_name: rabbitmq
  30. command: sh -c "rabbitmq-plugins enable rabbitmq_auth_backend_ldap"
  31. ports:
  32. - "5672:5672"
  33. - "15672:15672"
  34. restart: always

error:

  1. 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]
  2. 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).
  3. server_1 |
  4. server_1 | org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
  5. server_1 | at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  6. server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:524) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  7. server_1 | at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  8. server_1 | at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  9. server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2089) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  10. server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2062) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  11. server_1 | at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2042) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  12. server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  13. server_1 | at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  14. server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1836) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  15. server_1 | at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1817) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  16. 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]
  17. 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]
  18. server_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]
  19. server_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
  20. server_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_265]
  21. server_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_265]
  22. server_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_265]
  23. server_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_265]
  24. server_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_265]
  25. server_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_265]
  26. server_1 | at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.9.0.jar!/:5.9.0]
  27. server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1137) ~[amqp-client-5.9.0.jar!/:5.9.0]
  28. server_1 | at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1087) ~[amqp-client-5.9.0.jar!/:5.9.0]
  29. server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:560) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  30. server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:533) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  31. server_1 | at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:487) ~[spring-rabbit-2.2.7.RELEASE.jar!/:2.2.7.RELEASE]
  32. server_1 | ... 12 common frames omitted

Spring boot properties:

  1. spring.rabbitmq.address=localhost
  2. spring.rabbitmq.port=5672
  3. spring.rabbitmq.username=guest
  4. 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:

  1. @Configuration
  2. @EnableLdapRepositories
  3. public class LdapConfig {
  4. @Bean
  5. public LdapContextSource contextSource() {
  6. LdapContextSource contextSource = new LdapContextSource();
  7. contextSource.setUrl("ldap://localhost:18889");
  8. contextSource.setBase("dc=example,dc=com");
  9. return contextSource;
  10. }
  11. @Bean
  12. public LdapTemplate ldapTemplate() {
  13. return new LdapTemplate(contextSource());
  14. }
  15. }

答案1

得分: 1

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

  1. server:
  2. build: ./mlogger
  3. ports:
  4. - "8000:8080"
  5. depends_on:
  6. - database
  7. - rabbitmq
  8. network_mode: "host"
英文:

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

  1. server:
  2. build: ./mlogger
  3. ports:
  4. - "8000:8080"
  5. depends_on:
  6. - database
  7. - rabbitmq
  8. 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:

确定