当spring boot应用程序试图连接到在docker中运行的rabbitMQ时,出现连接相关异常

xqk2d5yq  于 2023-10-20  发布在  RabbitMQ
关注(0)|答案(1)|浏览(205)

我在localhost:8081上运行的spring-boot中创建了一个生产者和消费者,它试图将消息推送到rabbitMQ。我在Docker中运行rabbitMQ。我正在使用此镜像-> rabbitmq:3.12.2-management当我发送请求时,我得到了一个连接拒绝异常。我试着寻找一些类似于我的问题,如以下,但我不能解决这个问题。Spring Boot not connecting to RabbitMQspring boot cannot connect to rabbitmq
我正在添加代码,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": {}
    }
]

我已经尝试了spring.rabbitmq.host =localhost也在application.properties文件尽管所有这些我得到异常时,我打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)
cdmah0mi

cdmah0mi1#

@我改变的事情
1.我使用的是Docker Image 3.12.2管理。我使用的是3.10.25-管理映像。
1.我通过点击image的run选项从docker桌面启动容器/运行image。而不是这样做,我使用以下命令docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.10.25-management现在我的发布者能够将消息推送到队列,消费者可以从同一队列读取/获取消息。

相关问题