SpringBoot3整合RabbitMQ之一_消息生产者与消息消费者服务搭建
文章目录
- SpringBoot3整合RabbitMQ之一_消息生产者与消息消费者服务搭建
- [1. 消费发布者服务](#1. 消费发布者服务)
-
- [1. 新建工程](#1. 新建工程)
- [2. pom.xml](#2. pom.xml)
- [3. application.yml](#3. application.yml)
- [4. 主启动类](#4. 主启动类)
- [2. 消息消费者服务](#2. 消息消费者服务)
-
- [1. 新建工程](#1. 新建工程)
- [2. pom.xml](#2. pom.xml)
- [3. application.yml](#3. application.yml)
- [4. 主启动类](#4. 主启动类)
1. 消费发布者服务
1. 新建工程
新建名称为
happy-cloud-message
的工程
2. pom.xml
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>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.happy</groupId>
<artifactId>happy-cloud-message</artifactId>
<version>1.0.0</version>
<name>happy-cloud-message</name>
<description>happy-cloud-message</description>
<properties>
<java.version>17</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-configuration-processor</artifactId>
<optional>true</optional>
</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>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml
application.yml
配置如下
yaml
server:
port: 2001
spring:
application:
name: happy-cloud-message
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
listener:
# 默认 simple,三种类型 simple | direct | stream
type: simple
4. 主启动类
java
package com.happy.msg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HappyCloudMessageApplication {
public static void main(String[] args) {
SpringApplication.run(HappyCloudMessageApplication.class, args);
}
}
2. 消息消费者服务
1. 新建工程
新建名称为
happy-cloud-message-consumer
的工程
2. pom.xml
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>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.happy</groupId>
<artifactId>happy-cloud-message-consumer</artifactId>
<version>1.0.0</version>
<name>happy-cloud-message-consumer</name>
<description>happy-cloud-message-consumer</description>
<properties>
<java.version>17</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.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml
yaml
server:
port: 2002
spring:
application:
name: happy-cloud-message-consumer
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
listener:
# 默认 simple,三种类型 simple | direct | stream
type: simple
4. 主启动类
java
package com.happy.msg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HappyCloudMessageConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HappyCloudMessageConsumerApplication.class, args);
}
}