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);
    }
}
相关推荐
陈王卜10 分钟前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、10 分钟前
Spring Boot 注解
java·spring boot
java亮小白199715 分钟前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF21 分钟前
java Queue 详解
java·队列
武子康43 分钟前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs
武子康1 小时前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本
java·大数据·数据仓库·hive·hadoop·mysql
苏-言1 小时前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
界面开发小八哥1 小时前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
草莓base1 小时前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright1 小时前
maven概述
java·maven