【问题标题】:Spring Boot AMQP - Connection Creation / Queue Declaration at StartupSpring Boot AMQP - 启动时的连接创建/队列声明
【发布时间】:2020-06-19 10:39:18
【问题描述】:

春季启动 2.3.1 spring-boot-starter-amqp

如果我通过运行 main 方法(Intellij - 创建 type Application 的运行配置并提供主类 DemoApplication)将 Spring Boot 应用程序作为 java 应用程序运行,则不会在以下位置创建 RabbitMQ 连接启动。但是,当我作为 Spring Boot 应用程序运行时(Intellij - 创建 type 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)

【问题讨论】:

  • 我看不出有什么不同;无论我如何启动它,我总是能获得连接;显示您的代码。
  • 添加示例代码
  • 另外 mvn spring-boot:run 不会在启动时创建连接。我看到 Spring Boot Run Configuration 有 Enable JMX 复选框,如果未选中,即使 Spring Boot Run 在启动期间也不会创建连接。我尝试将这些 JMX 选项添加到 Application Run 类型(主)但没有运气: -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true

标签: spring-boot spring-amqp spring-rabbit


【解决方案1】:

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

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

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

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

【讨论】:

  • 看起来执行器正在打开它,因为如果我删除该依赖关系连接在启动时不会打开。现在,我在想为什么在主要的 java 风格运行期间没有触发执行器,可能是我遗漏了一些东西。刚买了古玩,因为看到了不同的行为,没有这样的问题
  • 据我所知;在您使用 HTTP 访问执行器端点之前,健康检查不会运行。就我而言,即使使用执行器,我也没有任何连接。
  • 非常感谢您的 cmets,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
相关资源
最近更新 更多