docker-compose.yml文件内容如下
version : '3'
services :
docker-mysql :
image: twocold/mysql:latest
ports:
- "3306:3306"
expose:
- 3306
rabbitmq:
image: rabbitmq:management
environment:
- RABBITMQ_DEFAULT_USER=rabbit
- RABBITMQ_DEFAULT_PASS=badrabbit
ports:
- "5673:5672"
- "15673:15672"
web:
image: twocold/springboot
depends_on:
- docker-mysql
- rabbitmq
ports:
- "8080:8080"
links:
- docker-mysql
- rabbitmq
项目中配置如下
# rabbitmq相关配置
spring.rabbitmq.host=rabbitmq
spring.rabbitmq.port=5673
spring.rabbitmq.username=rabbit
spring.rabbitmq.password=badrabbit
本地启动后mysql服务正常使用,客户端和项目都可以连接。但是rabbitmq服务浏览器可以正常访问,项目报错
Attempting to connect to: [rabbitmq:5673]
Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
Restarting Consumer@4fb62711: tags=[[]], channel=null, acknowledgeMode=AUTO local queue size=0
Attempting to connect to: [rabbitmq:5673]
Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62)
进入springboot项目的容器中,可以ping通rabbitmq服务
root@e55f9aed612c:/# ping rabbitmq
PING rabbitmq (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: icmp_seq=0 ttl=64 time=0.127 ms
64 bytes from 172.19.0.3: icmp_seq=1 ttl=64 time=0.152 ms
64 bytes from 172.19.0.3: icmp_seq=2 ttl=64 time=0.216 ms
64 bytes from 172.19.0.3: icmp_seq=3 ttl=64 time=0.072 ms
找到问题所在了
springboot的配置文件中应该写
spring.rabbitmq.port=5672 而不是 5673