首页 > 解决方案 > 未使用 Spring-Cloud-Stream 创建 RabbitMQ Exchange

问题描述

我是 Java Spring Cloud 的新手。我正在尝试使用 RabbitMQ 将消息发布到交换。但是一旦我运行我的 java 应用程序,我就看不到任何正在创建的 Exchange。我也没有收到任何错误。我错过了什么?

我尝试将磁盘空闲限制设置为 200MB,最初设置为 50MB,但仍然没有发现任何变化。

下面是我的控制器类代码片段::

public class AppointmentController {

private static final Logger 
   logger=LoggerFactory.getLogger(AppointmentController.class);
    @Autowired
    AppointmentSender sender;

    @PostMapping("/appointment-management-service/appointments")
    public void bookAppointment(@RequestBody AppointmentEvent 
              appointmentEvent) 
    {
       logger.info("Appointment request received {}",appointmentEvent);
       appointmentEvent.setStatus(AppointmentStatus.INITIATED);
       boolean isSent = sender.send(appointmentEvent);
       logger.info("Appointment booking initiated {}",isSent);

     }

}

下面是我的 Message Sender 类的代码片段::

@EnableBinding(Source.class)
public class AppointmentSender {

@Autowired
private Source source;

public boolean send(AppointmentEvent appointmentEvent) {
      return this.source.output().
      send(MessageBuilder.withPayload(appointmentEvent).build());
}

}

下面是 application.properties 文件

spring.application.name=appointment-management-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.broker-url=tcp://127.0.0.1:5672
spring.rabbitmq.virtual-host= /
spring.cloud.stream.bindings.output.destination=appointments-exchange

下面是 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 
  http://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.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.microservices</groupId>
<artifactId>appointmentmanagementsystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>appointmentmanagementsystem</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
     <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>5.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-test-support</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<dependencyManagement>
      <dependencies>
          <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                 <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

</project>

未创建名为“appointments-exchange”的交换。

标签: javaspring-cloud-streamspring-rabbit

解决方案


我刚刚将您的属性复制到一个新的引导项目中,它工作正常。

在此处输入图像描述

您的来宾用户是否具有管理员权限?

尝试启用 DEBUG 日志记录以查看是否有任何线索。


推荐阅读