当前位置: 首页 > 知识库问答 >
问题:

RabbitMQ SpringAMPQ:经纪人不可用;在启动期间无法强制队列声明: java.net.Connect异常: 连接被拒绝: 连接

晋弘义
2023-03-14

我目前正在学习RabbitMQ和Spring AMPQ。本周我尝试了一些例子,但是我在配置的某些步骤中遇到了问题。

我有这个docker-compose文件:

app:
  build: .
  environment:
    INSERTION_QUEUE: insertion.queue
    VALIDATION_QUEUE: validation.queue
    NUMBER_OF_VALIDATION_CONSUMERS: 1
    RESPONSE_EXCHANGE: response.exchange
    RESPONSE_ROUTING_KEY: response.routing.key
    RABBITMQ_HOST: rabbitmq
    RABBITMQ_PORT: 5672
    RABBITMQ_VHOST: /
    RABBITMQ_USERNAME: guest
    RABBITMQ_PASSWORD: guest
    JDBC_URL: jdbc:mysql://mysql:3306/hided
  links:
    - mysql:mysql
    - rabbitmq:rabbitmq
mysql:
  image: mysql:5.7
  environment:
    MYSQL_DATABASE: hided
    MYSQL_ROOT_PASSWORD: secret
rabbitmq:
  image: rabbitmq:3.6-management
  ports:
    - 15672:15672

我复制了这个例子,向它添加了maven配置:https://github.com/spring-guides/gs-messaging-rabbitmq/tree/master/complete

RabbitMQ 通过 URL localhost:15672 使用用户名和密码“guest”正确访问

但是当我运行这个例子时,它给出了这个错误(我相信这个错误的重要信息是Broker不可用;无法在启动期间强制队列声明:Java . net . connect异常:连接被拒绝:connect):

2020-04-11 04:50:49.202  INFO 25576 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2020-04-11 04:50:51.222  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect
2020-04-11 04:50:51.225  INFO 25576 --- [    container-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2020-04-11 04:50:53.242 ERROR 25576 --- [    container-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2095) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2048) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1830) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1811) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1342) [spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1188) [spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_241]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_241]
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_241]
    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar:5.7.3]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar:5.7.3]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar:5.7.3]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    ... 12 common frames omitted

2020-04-11 04:50:53.244  INFO 25576 --- [    container-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2020-04-11 04:50:55.266  INFO 25576 --- [           main] c.a.A.AppNameApplication           : Started AppNameApplication in 7.328 seconds (JVM running for 8.175)
Sending message...
2020-04-11 04:50:55.268  INFO 25576 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2020-04-11 04:50:57.285  INFO 25576 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-11 04:50:57.293 ERROR 25576 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.appname.AppName.AppNameApplication.main(AppNameApplication.java:19) [classes/:na]
Caused by: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2095) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1009) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1075) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at com.appname.AppName.Runner.run(Runner.java:23) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    ... 5 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_241]
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_241]
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
    at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_241]
    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar:5.7.3]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar:5.7.3]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar:5.7.3]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    ... 14 common frames omitted

2020-04-11 04:50:57.296  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
2020-04-11 04:50:57.296  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2020-04-11 04:50:57.298  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Shutdown ignored - container is not active already

我想了解我做错了什么,我试图将下面的代码添加到“MessagingRabbitmqApplication.java”以强制连接,对端口、地址、禁用防火墙进行了一些更改,没有成功:

@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

我确实尝试了其他例子,但我一直面临同样的错误。

我的 Dockerfile 是:

# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="hidden"

# Add a volume pointing to /tmp
VOLUME /tmp

ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

我的application.properties是:

#Database properties
spring.datasource.url = jdbc:mysql://mysql:3306/app_name?user=root&password=secret
spring.datasource.username = root
spring.datasource.password = secret
spring.datasource.platform = mysql
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

#Rabbitmq properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.template.exchange=response.exchange
spring.rabbitmq.template.routing-key=response.routing.key
spring.rabbitmq.virtual-host=/

谢谢你的时间,我感谢任何帮助。

共有3个答案

阎祖鹤
2023-03-14

您可以使用这个命令运行docker来使用默认值:

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
鄢选
2023-03-14

请更正应替换为的 Bean 创建配置

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost(System.getProperty("RABBITMQ_HOST"));
    connectionFactory.setVirtualHost(System.getProperty("RABBITMQ_VHOST"));
    connectionFactory.setUsername(System.getProperty("RABBITMQ_USERNAME"));
    connectionFactory.setPassword(System.getProperty("RABBITMQ_PASSWORD"));
    return connectionFactory;
}

并修改您的docker文件的最后一行

# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="hidden"

# Add a volume pointing to /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT java -DRABBITMQ_HOST=${RABBITMQ_HOST} -DRABBITMQ_VHOST=${RABBITMQ_VHOST} -DRABBITMQ_USERNAME=${RABBITMQ_USERNAME} -DRABBITMQ_PASSWORD=${RABBITMQ_PASSWORD} -Djava.security.egd=file:/dev/./urandom  -jar app.jar
祁雪峰
2023-03-14

我今天发现了错误,我们需要在docker-compose文件中公开RabbitMQ端口,否则它将无法访问。所以我们需要在 rabbitmq 中添加一行 - 5672:5672

app:
  build: .
  environment:
    INSERTION_QUEUE: insertion.queue
    VALIDATION_QUEUE: validation.queue
    NUMBER_OF_VALIDATION_CONSUMERS: 1
    RESPONSE_EXCHANGE: response.exchange
    RESPONSE_ROUTING_KEY: response.routing.key
    RABBITMQ_HOST: rabbitmq
    RABBITMQ_PORT: 5672
    RABBITMQ_VHOST: /
    RABBITMQ_USERNAME: guest
    RABBITMQ_PASSWORD: guest
    JDBC_URL: jdbc:mysql://mysql:3306/hided
  links:
    - mysql:mysql
    - rabbitmq:rabbitmq
mysql:
  image: mysql:5.7
  environment:
    MYSQL_DATABASE: hided
    MYSQL_ROOT_PASSWORD: secret
rabbitmq:
  image: rabbitmq:3.6-management
  ports:
    - 5672:5672
    - 15672:15672

谢谢大家!

 类似资料:
  • 我正在使用Spring 5网络流量、SpringBoot 2和Reactive MongoDB构建一个POC。在使用maven插件构建项目时,我得到了如下错误MongoDB连接错误:- 以下是我的申请表。属性文件:- 波姆。xml:- 请告知这里可能存在什么问题?我是否需要在我的机器上显式安装MongoDB并在运行此项目之前运行它。我相信MongoDB实例将自行运行,因为SpringBoot将在运

  • 我正在尝试使用java邮件api,并且在servlet中有以下代码用于sen mail。我找不到解决错误的方法。 我的控制台中出现以下错误: 原因:java.net.ConnectException:连接被拒绝:java.net.DualStackPlainSocketImpl.connect0(本机方法)java.net.DualStackPlainSocketImpl.socketConnec

  • 试图在localhost中建立从app容器到mysql容器的连接,出现连接拒绝异常 我们正在采取一种docker的方法来调用rest api服务来采用微服务的方法。我们正在建立应用程序容器和mysql容器之间的连接,同时我们编写了一个docker-compose文件,创建了mysql容器和应用程序容器,为这两个容器公开了端口。下面是运行docker-compose文件docker-compose

  • 问题内容: 在Digitalocean的Ubuntu 16.4中安装了Redis。 我已经通过编辑编辑将默认的redis服务器更改为我在Digitalocean中的IP 那之后,我试着和所有回应这样的: 在127.0.0.1:6379无法连接到Redis的:连接被拒绝 但是如果 我得到: 这正常吗?或者该怎么办? 问题答案: 您应该做什么取决于您希望它如何工作… 如果您希望能够通过任何网络接口连接

  • 问题内容: 我让ELK堆栈在VM内的docker容器中运行。 我可以向ES填充,它可以在Kibana中显示。 我可以使用Logstash读取文件并将其输出到stdout。 但是Logstash无法将数据发送到ES (启用或禁用xpack似乎没有什么不同) 输出 之后,它会在“发送批量请求”,“执行运行状况检查”,“尝试复活”和“意外的池错误”之间保持循环。 随着每一次的同时,我也看到了 问题答案: