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

问题描述 投票:0回答:1

我正在 spring-boot 中创建一个生产者和消费者,运行在 localhost:8081 上,它试图将消息推送到rabbitMQ。 我在 docker 中运行rabbitMQ。 我正在使用这个图像->rabbitmq:3.12.2-management 当我发送请求时,我收到连接拒绝异常。 我试图寻找一些与我类似的问题,例如以下问题,但我无法解决问题。 Spring Boot 未连接到 RabbitMQ spring boot无法连接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();
        }
    }
}

消息控制器.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)
java spring spring-boot docker rabbitmq
1个回答
0
投票

@我做出改变的事情

  1. 我使用的是 docker image 3.12.2-management。相反,我使用了 3.10.25-管理映像。
  2. 我通过单击图像的运行选项来启动容器/从 docker 桌面运行图像。我没有这样做,而是使用了以下命令 docker run --rm -it -p 15672:15672 -p 5672:5672rabbitmq:3.10.25-管理 现在我的发布者能够将消息推送到队列,消费者可以从同一队列读取/获取消息。
© www.soinside.com 2019 - 2024. All rights reserved.