首页 > 解决方案 > Spring Boot AMQP - 启动时的连接创建/队列声明

问题描述

Spring Boot 2.3.1 spring-boot-starter-amqp

如果我通过运行 main 方法(Intellij - 创建Application 类型的运行配置并提供主类 DemoApplication)将 Spring Boot 应用程序作为 Java 应用程序运行,则不会在启动时创建 RabbitMQ 连接。但是,当我作为 Spring Boot 应用程序运行(Intellij - 创建Spring Boot 类型的运行配置并提供主类 DemoApplication)时,会在启动期间创建 RabbitMQ 连接。

两者有什么区别?当 main 方法作为 java 应用程序运行时,为什么 RabbitMQ 不能在启动时创建连接?该行不出现。

INFO 32385 --- [*.*.*.*] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory.publisher#3f499c2f:0/SimpleConnection@4e5ef6a0 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 62061]

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

应用程序.yaml

spring:
  rabbitmq:
    host: ${RABBITMQ_HOST:localhost}
    port: ${RABBITMQ_PORT:5672}
    username: ${RABBITMQ_USERNAME:guest}
    password: ${RABBITMQ_PASSWORD:guest}

主班

package com.example.demo;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    DirectExchange demoExchange() {
        return new DirectExchange("demo-exchange");
    }

    @Bean
    Queue demoQueue() {
        return new Queue("demo-queue");
    }

    @Bean
    Binding demoBinding() {
        return BindingBuilder.bind(demoQueue()).to(demoExchange()).with("demo-routing");
    }

}

Intellij - 创建 Application 类型的运行配置并提供主类 (DemoApplication)

标签: spring-bootspring-amqpspring-rabbit

解决方案


您的代码中没有任何内容可以打开连接(例如@RabbitListener)。

也许执行器 ( RabbitHealthIndicator) 正在打开一个连接 - 不知道为什么它会影响你如何启动它。

我没有看到任何一种连接方式(使用 STS)。

如果您将 a 添加@RabbitListener到您的应用中;无论哪种方式,您都会看到连接。


推荐阅读