0

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. Spring Boot not connecting to RabbitMQ 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)
Akshay
  • 23
  • 5

1 Answers1

1

@ 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.
Akshay
  • 23
  • 5