Connection related exception while a spring-boot application tries to connect to rabbitMQ running in docker

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

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

  1. package com.akshay.springbootrabbitmq.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.core.TopicExchange;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. public class RabbitMQConfig {
  11. @Value("${rabbitmq.queue.name}")
  12. private String queue;
  13. @Value("${rabbitmq.exchange.name}")
  14. private String exchange;
  15. @Value("${rabbitmq.routing.key}")
  16. private String routingKey;
  17. @Bean
  18. public Queue queue() {
  19. return new Queue(queue);
  20. }
  21. @Bean
  22. public TopicExchange exchange() {
  23. return new TopicExchange(exchange);
  24. }
  25. @Bean
  26. public Binding bind() {
  27. return BindingBuilder.bind(queue())
  28. .to(exchange())
  29. .with(routingKey);
  30. }
  31. }

RabbitMQProducer.java

  1. package com.akshay.springbootrabbitmq.publisher;
  2. import org.springframework.amqp.AmqpException;
  3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class RabbitMQProducer {
  9. @Value("${rabbitmq.exchange.name}")
  10. private String exchange;
  11. @Value("${rabbitmq.routing.key}")
  12. private String routingKey;
  13. private RabbitTemplate rabbitTemplate;
  14. @Autowired
  15. public RabbitMQProducer(RabbitTemplate rabbitTemplate) {
  16. this.rabbitTemplate = rabbitTemplate;
  17. }
  18. public void sendMessage(String message) {
  19. System.out.println("Sending message - " + message + " to the exchange named - " + exchange);
  20. try {
  21. rabbitTemplate.convertAndSend(exchange, routingKey, message);
  22. } catch (AmqpException ex) {
  23. ex.printStackTrace();
  24. }
  25. }
  26. }

MessageController.java

  1. package com.akshay.springbootrabbitmq.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.akshay.springbootrabbitmq.publisher.RabbitMQProducer;
  8. @RestController
  9. public class MessageController {
  10. private RabbitMQProducer producer;
  11. @Autowired
  12. public MessageController(RabbitMQProducer producer) {
  13. this.producer = producer;
  14. }
  15. @GetMapping("/api/publish")
  16. public ResponseEntity<String> sendMessage(@RequestParam("msg") String message){
  17. producer.sendMessage(message);
  18. return ResponseEntity.ok("Message sent to RabbitMQ");
  19. }
  20. }

application.properties文件

  1. server.port=8081
  2. spring.rabbitmq.host=172.17.0.1
  3. spring.rabbitmq.port=5672
  4. rabbitmq.queue.name=JavaTest
  5. rabbitmq.exchange.name=JavaExchange
  6. rabbitmq.routing.key=Java_routing_key

以下是我从docker ps中获取的信息:

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 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网络检查桥接的信息:

  1. [
  2. {
  3. "Name": "bridge",
  4. "Id": "21397db4b92db2ecc181c502b4db27d673614a1c1ae07fbf0d0751ab1e70cd85",
  5. "Created": "2023-08-08T12:18:21.6715506Z",
  6. "Scope": "local",
  7. "Driver": "bridge",
  8. "EnableIPv6": false,
  9. "IPAM": {
  10. "Driver": "default",
  11. "Options": null,
  12. "Config": [
  13. {
  14. "Subnet": "172.17.0.0/16",
  15. "Gateway": "172.17.0.1"
  16. }
  17. ]
  18. },
  19. "Internal": false,
  20. "Attachable": false,
  21. "Ingress": false,
  22. "ConfigFrom": {
  23. "Network": ""
  24. },
  25. "ConfigOnly": false,
  26. "Containers": {
  27. "5fa2a5be9ed9aff148d2318d4dfc5e10cc4581c0a6aac702ff477f6268e65c8a": {
  28. "Name": "suspicious_chatelet",
  29. "EndpointID": "54b34aa3f809b91d4e53de47c743ef1cad4d31e30a6e758fab31aed0bae11839",
  30. "MacAddress": "02:42:ac:11:00:02",
  31. "IPv4Address": "172.17.0.2/16",
  32. "IPv6Address": ""
  33. }
  34. },
  35. "Options": {
  36. "com.docker.network.bridge.default_bridge": "true",
  37. "com.docker.network.bridge.enable_icc": "true",
  38. "com.docker.network.bridge.enable_ip_masquerade": "true",
  39. "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
  40. "com.docker.network.bridge.name": "docker0",
  41. "com.docker.network.driver.mtu": "1500"
  42. },
  43. "Labels": {}
  44. }
  45. ]

我已经尝试在application.properties文件中使用spring.rabbitmq.host=localhost,但仍然出现异常,当我访问localhost:8081/api/publish?msg="This is from Postman"时。

  1. 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'
  2. 2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  3. 2023-08-08T18:53:40.397+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  4. Sending message - "This is from Postman" to the exchange named - JavaExchange
  5. 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]
  6. org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: no further information
  7. at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61)
  8. at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:594)
  9. at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:687)
  10. at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:257)
  11. at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2225)
  12. at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2198)
  13. at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1119)
  14. at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1182)
  15. at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1175)
  16. at com.akshay.springbootrabbitmq.publisher.RabbitMQProducer.sendMessage(RabbitMQProducer.java:28)
  17. 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

  1. package com.akshay.springbootrabbitmq.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.core.TopicExchange;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. @Configuration
  10. public class RabbitMQConfig {
  11. @Value(&quot;${rabbitmq.queue.name}&quot;)
  12. private String queue;
  13. @Value(&quot;${rabbitmq.exchange.name}&quot;)
  14. private String exchange;
  15. @Value(&quot;${rabbitmq.routing.key}&quot;)
  16. private String routingKey;
  17. @Bean
  18. public Queue queue() {
  19. return new Queue(queue);
  20. }
  21. @Bean
  22. public TopicExchange exchange() {
  23. return new TopicExchange(exchange);
  24. }
  25. @Bean
  26. public Binding bind() {
  27. return BindingBuilder.bind(queue())
  28. .to(exchange())
  29. .with(routingKey);
  30. }
  31. }

RabbitMQProducer.java

  1. package com.akshay.springbootrabbitmq.publisher;
  2. import org.springframework.amqp.AmqpException;
  3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class RabbitMQProducer {
  9. @Value(&quot;${rabbitmq.exchange.name}&quot;)
  10. private String exchange;
  11. @Value(&quot;${rabbitmq.routing.key}&quot;)
  12. private String routingKey;
  13. private RabbitTemplate rabbitTemplate;
  14. @Autowired
  15. public RabbitMQProducer(RabbitTemplate rabbitTemplate) {
  16. this.rabbitTemplate = rabbitTemplate;
  17. }
  18. public void sendMessage(String message) {
  19. System.out.println(&quot;Sending message - &quot;+message+&quot; to the exchange named - &quot;+exchange);
  20. try {
  21. rabbitTemplate.convertAndSend(exchange, routingKey, message);
  22. }catch(AmqpException ex) {
  23. ex.printStackTrace();
  24. }
  25. }
  26. }

MessageController.java

  1. package com.akshay.springbootrabbitmq.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.akshay.springbootrabbitmq.publisher.RabbitMQProducer;
  8. @RestController
  9. public class MessageController {
  10. private RabbitMQProducer producer;
  11. @Autowired
  12. public MessageController(RabbitMQProducer producer) {
  13. this.producer = producer;
  14. }
  15. @GetMapping(&quot;/api/publish&quot;)
  16. public ResponseEntity&lt;String&gt; sendMessage(@RequestParam(&quot;msg&quot;) String message){
  17. producer.sendMessage(message);
  18. return ResponseEntity.ok(&quot;Message sent to RabbitMQ&quot;);
  19. }
  20. }

application.properties file

  1. server.port=8081
  2. spring.rabbitmq.host=172.17.0.1
  3. spring.rabbitmq.port=5672
  4. rabbitmq.queue.name=JavaTest
  5. rabbitmq.exchange.name=JavaExchange
  6. rabbitmq.routing.key=Java_routing_key

Following I have got from docker ps

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 5fa2a5be9ed9 rabbitmq:3.12.2-management &quot;docker-entrypoint.s…&quot; 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

  1. [
  2. {
  3. &quot;Name&quot;: &quot;bridge&quot;,
  4. &quot;Id&quot;: &quot;21397db4b92db2ecc181c502b4db27d673614a1c1ae07fbf0d0751ab1e70cd85&quot;,
  5. &quot;Created&quot;: &quot;2023-08-08T12:18:21.6715506Z&quot;,
  6. &quot;Scope&quot;: &quot;local&quot;,
  7. &quot;Driver&quot;: &quot;bridge&quot;,
  8. &quot;EnableIPv6&quot;: false,
  9. &quot;IPAM&quot;: {
  10. &quot;Driver&quot;: &quot;default&quot;,
  11. &quot;Options&quot;: null,
  12. &quot;Config&quot;: [
  13. {
  14. &quot;Subnet&quot;: &quot;172.17.0.0/16&quot;,
  15. &quot;Gateway&quot;: &quot;172.17.0.1&quot;
  16. }
  17. ]
  18. },
  19. &quot;Internal&quot;: false,
  20. &quot;Attachable&quot;: false,
  21. &quot;Ingress&quot;: false,
  22. &quot;ConfigFrom&quot;: {
  23. &quot;Network&quot;: &quot;&quot;
  24. },
  25. &quot;ConfigOnly&quot;: false,
  26. &quot;Containers&quot;: {
  27. &quot;5fa2a5be9ed9aff148d2318d4dfc5e10cc4581c0a6aac702ff477f6268e65c8a&quot;: {
  28. &quot;Name&quot;: &quot;suspicious_chatelet&quot;,
  29. &quot;EndpointID&quot;: &quot;54b34aa3f809b91d4e53de47c743ef1cad4d31e30a6e758fab31aed0bae11839&quot;,
  30. &quot;MacAddress&quot;: &quot;02:42:ac:11:00:02&quot;,
  31. &quot;IPv4Address&quot;: &quot;172.17.0.2/16&quot;,
  32. &quot;IPv6Address&quot;: &quot;&quot;
  33. }
  34. },
  35. &quot;Options&quot;: {
  36. &quot;com.docker.network.bridge.default_bridge&quot;: &quot;true&quot;,
  37. &quot;com.docker.network.bridge.enable_icc&quot;: &quot;true&quot;,
  38. &quot;com.docker.network.bridge.enable_ip_masquerade&quot;: &quot;true&quot;,
  39. &quot;com.docker.network.bridge.host_binding_ipv4&quot;: &quot;0.0.0.0&quot;,
  40. &quot;com.docker.network.bridge.name&quot;: &quot;docker0&quot;,
  41. &quot;com.docker.network.driver.mtu&quot;: &quot;1500&quot;
  42. },
  43. &quot;Labels&quot;: {}
  44. }
  45. ]

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"

  1. 2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet &#39;dispatcherServlet&#39;
  2. 2023-08-08T18:53:40.395+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet &#39;dispatcherServlet&#39;
  3. 2023-08-08T18:53:40.397+05:30 INFO 11108 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
  4. Sending message - &quot;This is from Postman&quot; to the exchange named - JavaExchange
  5. 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]
  6. org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: no further information
  7. at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61)
  8. at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:594)
  9. at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:687)
  10. at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:257)
  11. at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2225)
  12. at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2198)
  13. at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1119)
  14. at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1182)
  15. at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1175)
  16. at com.akshay.springbootrabbitmq.publisher.RabbitMQProducer.sendMessage(RabbitMQProducer.java:28)
  17. 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

  1. I was using docker image 3.12.2-management. Instead I used 3.10.25-management image.
  2. 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.

huangapple
  • 本文由 发表于 2023年8月8日 21:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860040.html
匿名

发表评论

匿名网友

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

确定