英文:
Connection related exception while a spring-boot application tries to connect to rabbitMQ running in docker
问题
我正在创建一个在本地主机上运行的Spring Boot生产者和消费者,它试图将消息推送到RabbitMQ。我正在使用Docker运行RabbitMQ。我正在使用以下镜像-> rabbitmq:3.12.2-management。当我发送请求时,我收到连接被拒绝的异常。我尝试查找一些类似于我的问题的问题,例如以下问题,但我无法解决这个问题。
我正在添加来自我的Spring Boot应用程序的代码、日志以及我在Docker桌面上看到的日志。
我的应用程序详细信息如下:
RabbitMQConfig.java
package com.akshay.springbootrabbitmq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Value("${rabbitmq.queue.name}")
private String queue;
@Value("${rabbitmq.exchange.name}")
private String exchange;
@Value("${rabbitmq.routing.key}")
private String routingKey;
@Bean
public Queue queue() {
return new Queue(queue);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange(exchange);
}
@Bean
public Binding bind() {
return BindingBuilder.bind(queue())
.to(exchange())
.with(routingKey);
}
}
RabbitMQProducer.java
package com.akshay.springbootrabbitmq.publisher;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQProducer {
@Value("${rabbitmq.exchange.name}")
private String exchange;
@Value("${rabbitmq.routing.key}")
private String routingKey;
private RabbitTemplate rabbitTemplate;
@Autowired
public RabbitMQProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessage(String message) {
System.out.println("Sending message - " + message + " to the exchange named - " + exchange);
try {
rabbitTemplate.convertAndSend(exchange, routingKey, message);
} catch (AmqpException ex) {
ex.printStackTrace();
}
}
}
MessageController.java
package com.akshay.springbootrabbitmq.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.akshay.springbootrabbitmq.publisher.RabbitMQProducer;
@RestController
public class MessageController {
private RabbitMQProducer producer;
@Autowired
public MessageController(RabbitMQProducer producer) {
this.producer = producer;
}
@GetMapping("/api/publish")
public ResponseEntity<String> sendMessage(@RequestParam("msg") String message){
producer.sendMessage(message);
return ResponseEntity.ok("Message sent to RabbitMQ");
}
}
application.properties文件
server.port=8081
spring.rabbitmq.host=172.17.0.1
spring.rabbitmq.port=5672
rabbitmq.queue.name=JavaTest
rabbitmq.exchange.name=JavaExchange
rabbitmq.routing.key=Java_routing_key
以下是我从docker ps中获取的信息:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fa2a5be9ed9 rabbitmq:3.12.2-management "docker-entrypoint.s…" 13 seconds ago Up 12 seconds 4369/tcp, 5671-5672/tcp, 15671-15672/tcp, 15691-15692/tcp, 25672/tcp suspicious_chatelet
这是从docker网络检查桥接的信息:
[
{
"Name": "bridge",
"Id": "21397db4b92db2ecc181c502b4db27d673614a1c1ae07fbf0d0751ab1e70cd85",
"Created": "2023-08-08T12:18:21.6715506Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"5fa2a5be9ed9aff148d2318d4dfc5e10cc4581c0a6aac702ff477f6268e65c8a": {
"Name": "suspicious_chatelet",
"EndpointID": "54b34aa3f809b91d4e53de47c743ef1cad4d31e30a6e758fab31aed0bae11839",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
我已经尝试在application.properties文件中使用spring.rabbitmq.host=localhost,但仍然出现异常,当我访问localhost:8081/api/publish?msg="This is from Postman"时。
2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-08-08T18:53:40.397+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Sending message - "This is from Postman" to the exchange named - JavaExchange
2023-08-08T18:53:40.453+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: no further information
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:594)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:687)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:257)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2225)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2198)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1119)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1182)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1175)
at com.akshay.springbootrabbitmq.publisher.RabbitMQProducer.sendMessage(RabbitMQProducer.java:28)
at com.akshay.springbootrabbitmq.controller.MessageController.sendMessage(MessageController.java:23)
英文:
I am creating a producer and consumer in spring-boot running on localhost:8081, which is trying to push the message to the rabbitMQ.
I am running rabbitMQ in docker.
I am using this image -> rabbitmq:3.12.2-management
When I send request I am getting a connection refused exception.
I tried to look for some of the questions similar to mine such as following but I could not solve the issue.
https://stackoverflow.com/questions/47873835/spring-boot-not-connecting-to-rabbitmq
https://stackoverflow.com/questions/33880410/spring-boot-cannot-connect-to-rabbitmq
I am adding code, logs from my spring-boot application as well as the logs that I can see on docker desktop.
My application details are as follows
RabbitMQConfig.java
package com.akshay.springbootrabbitmq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Value("${rabbitmq.queue.name}")
private String queue;
@Value("${rabbitmq.exchange.name}")
private String exchange;
@Value("${rabbitmq.routing.key}")
private String routingKey;
@Bean
public Queue queue() {
return new Queue(queue);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange(exchange);
}
@Bean
public Binding bind() {
return BindingBuilder.bind(queue())
.to(exchange())
.with(routingKey);
}
}
RabbitMQProducer.java
package com.akshay.springbootrabbitmq.publisher;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQProducer {
@Value("${rabbitmq.exchange.name}")
private String exchange;
@Value("${rabbitmq.routing.key}")
private String routingKey;
private RabbitTemplate rabbitTemplate;
@Autowired
public RabbitMQProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessage(String message) {
System.out.println("Sending message - "+message+" to the exchange named - "+exchange);
try {
rabbitTemplate.convertAndSend(exchange, routingKey, message);
}catch(AmqpException ex) {
ex.printStackTrace();
}
}
}
MessageController.java
package com.akshay.springbootrabbitmq.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.akshay.springbootrabbitmq.publisher.RabbitMQProducer;
@RestController
public class MessageController {
private RabbitMQProducer producer;
@Autowired
public MessageController(RabbitMQProducer producer) {
this.producer = producer;
}
@GetMapping("/api/publish")
public ResponseEntity<String> sendMessage(@RequestParam("msg") String message){
producer.sendMessage(message);
return ResponseEntity.ok("Message sent to RabbitMQ");
}
}
application.properties file
server.port=8081
spring.rabbitmq.host=172.17.0.1
spring.rabbitmq.port=5672
rabbitmq.queue.name=JavaTest
rabbitmq.exchange.name=JavaExchange
rabbitmq.routing.key=Java_routing_key
Following I have got from docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fa2a5be9ed9 rabbitmq:3.12.2-management "docker-entrypoint.s…" 13 seconds ago Up 12 seconds 4369/tcp, 5671-5672/tcp, 15671-15672/tcp, 15691-15692/tcp, 25672/tcp suspicious_chatelet
And this is from docker networ inspect bridge
[
{
"Name": "bridge",
"Id": "21397db4b92db2ecc181c502b4db27d673614a1c1ae07fbf0d0751ab1e70cd85",
"Created": "2023-08-08T12:18:21.6715506Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"5fa2a5be9ed9aff148d2318d4dfc5e10cc4581c0a6aac702ff477f6268e65c8a": {
"Name": "suspicious_chatelet",
"EndpointID": "54b34aa3f809b91d4e53de47c743ef1cad4d31e30a6e758fab31aed0bae11839",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
I have tried spring.rabbitmq.host=localhost also in application.properties file
Inspite of all these I am getting exception when I hit localhost:8081/api/publish?msg="This is from Postman"
2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-08-08T18:53:40.397+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Sending message - "This is from Postman" to the exchange named - JavaExchange
2023-08-08T18:53:40.453+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: no further information
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61)
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:594)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:687)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:257)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2225)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2198)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1119)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1182)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1175)
at com.akshay.springbootrabbitmq.publisher.RabbitMQProducer.sendMessage(RabbitMQProducer.java:28)
at com.akshay.springbootrabbitmq.controller.MessageController.sendMessage(MessageController.java:23)
答案1
得分: 1
@ 我做出的更改事项
1)我之前使用的是docker镜像3.12.2-management,现在改为使用3.10.25-management镜像。
2)我之前是通过在docker桌面上点击运行选项来启动容器/运行镜像。现在改为使用以下命令:
docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.10.25-management
现在我的发布者能够将消息推送到队列中,消费者可以从同一队列中读取/获取消息。
英文:
@ things where I made change
- I was using docker image 3.12.2-management. Instead I used 3.10.25-management image.
- I was starting the container/ running the image from docker desktop by clicking on the run option of image. Instead of doing that I used following command
docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.10.25-management
And now my publisher is able to push the message to the queue and cosumer can read/get the message from the same queue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论