SpringBoot3整合RabbitMQ之一_消息生产者与消息消费者服务搭建

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);
    }
}
相关推荐
coderzpw1 小时前
当模板方法模式遇上工厂模式:一道优雅的烹饪架构设计
java·模板方法模式
直裾1 小时前
Mapreduce初使用
java·mapreduce
悠夏安末1 小时前
intellij Idea 和 dataGrip下载和安装教程
java·ide·intellij-idea
suimeng61 小时前
ChromeDriver的常用方法
java·selenium
Hellyc2 小时前
SpringMVC响应数据:页面跳转与回写数据
java·前端·学习
嘵奇2 小时前
深入解析 Spring Boot 测试核心注解
java·spring boot·后端
癞皮狗不赖皮2 小时前
Java安全基础-反射机制
java·反射机制·java安全基础
别惊鹊2 小时前
(三)安装和使用Maven
java·maven
兢兢业业的小白鼠2 小时前
Java高级JVM知识点记录,内存结构,垃圾回收,类文件结构,类加载器
java·开发语言·jvm·tomcat
落榜程序员2 小时前
Java 基础-29-final关键字-详解
java·开发语言