当前位置: 首页 > article >正文

项目集成RabbitMQ

文章目录

    • 1.common-rabbitmq-starter
        • 1.创建common-rabbitmq-starter
        • 2.pom.xml
        • 3.自动配置
          • 1.RabbitMQAutoConfiguration.java
          • 2.spring.factories
    • 2.测试使用
        • 1.创建common-rabbitmq-starter-demo
        • 2.目录结构
        • 3.pom.xml
        • 4.application.yml
        • 5.TestConfig.java 配置交换机和队列
        • 6.TestConfigConsumer.java 监听队列
        • 7.TestConfigPublisher.java 发布消息
        • 8.结果

1.common-rabbitmq-starter

1.创建common-rabbitmq-starter

CleanShot 2024-12-31 at 18.38.04@2x

2.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>com.sunxiansheng</groupId>
        <artifactId>sunrays-common</artifactId>
        <version>1.0.5</version>
    </parent>

    <version>1.0.5</version>

    <artifactId>common-rabbitmq-starter</artifactId>

    <dependencies>
        <!-- 工具包 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-tool-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

</project>
3.自动配置
1.RabbitMQAutoConfiguration.java
package com.sunxiansheng.rabbitmq.config;

import org.springframework.context.annotation.Configuration;

/**
 * Description: RabbitMQ自动配置类
 *
 * @Author sun
 * @Create 2024/12/31 18:44
 * @Version 1.0
 */
@Configuration
public class RabbitMQAutoConfiguration {
}
2.spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.rabbitmq.config.RabbitMQAutoConfiguration

2.测试使用

1.创建common-rabbitmq-starter-demo

CleanShot 2024-12-31 at 18.48.22@2x

2.目录结构

CleanShot 2024-12-31 at 19.13.13@2x

3.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>com.sunxiansheng</groupId>
        <artifactId>sunrays-demo</artifactId>
        <version>1.0.5</version>
    </parent>

    <artifactId>common-rabbitmq-starter-demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-rabbitmq-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- 引入springboot-web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除logging,防止日志冲突 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入自己的日志 -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-log4j2-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- 排除spring-boot-starter-logging,防止跟log4j2冲突 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>
4.application.yml
spring:
  # RabbitMQ 配置
  rabbitmq:
    # 服务器地址
    host: ip
    # 用户名
    username: guest
    # 密码
    password: guest
    # 虚拟主机
    virtual-host: /
    # 端口
    port: 6783
log:
  home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/common-rabbitmq-starter-demo
  module: common-rabbitmq-starter-demo
5.TestConfig.java 配置交换机和队列
package com.sunxiansheng.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description: 测试配置类
 *
 * @Author sun
 * @Create 2024/12/31 19:00
 * @Version 1.0
 */
@Configuration
public class TestConfig {

    /**
     * 创建一个fanout类型的交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout.exchange.test");
    }

    /**
     * 创建一个队列
     * @return
     */
    @Bean
    public Queue fanoutQueueTest() {
        return new Queue("fanout.queue.test");
    }

    /**
     * 交换机和队列绑定
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(fanoutQueueTest()).to(fanoutExchange());
    }
}
6.TestConfigConsumer.java 监听队列
package com.sunxiansheng.rabbitmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Description: 测试消费者
 *
 * @Author sun
 * @Create 2024/12/31 19:03
 * @Version 1.0
 */
@Component
@Slf4j
public class TestConfigConsumer {

    @RabbitListener(queues = "fanout.queue.test")
    public void receive(String message) {
        log.info("接收到的消息:{}", message);
    }
}
7.TestConfigPublisher.java 发布消息
package com.sunxiansheng.rabbitmq.publisher;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description: 测试发布者
 *
 * @Author sun
 * @Create 2024/12/31 19:05
 * @Version 1.0
 */
@SpringBootTest
public class TestConfigPublisher {

    @Resource
    private AmqpTemplate amqpTemplate;

    @Test
    public void send() {
        amqpTemplate.convertAndSend("fanout.exchange.test", "", "hello rabbitmq");
    }
}
8.结果

CleanShot 2024-12-31 at 19.15.06@2x


http://www.kler.cn/a/514547.html

相关文章:

  • Excel打印技巧
  • 代码随想录——串
  • 【C++】模板(进阶)
  • 机器学习(5):支持向量机
  • Java-数据结构-二叉树习题(2)
  • C语言之图像文件的属性
  • No.35 笔记 | Python学习之旅:基础语法与实践作业总结
  • webstorm git提交卡住在analyzing code ,怎么取消
  • 2024年博客之星主题创作|2024年度感想与新技术Redis学习
  • 【前端知识】简单易懂的vue前端页面元素权限控制
  • 2. CSS 中的单位
  • ToolHop: 多跳工具使用评估基准的全面解析
  • 线性表-线性存储结构
  • 从监控软件的敏感信息报警功能看企业信息安全新趋势
  • Docker 国内镜像源
  • 【VMWare Workstation 17】安装Debian 12.8DVD
  • LightRAG源码:NetworkXStorage测试(1)
  • vscode如何选用不同的python的解释器
  • Yii框架中的队列:如何实现异步操作
  • MySQL(1)概述
  • # [Unity] [游戏开发]基础协程应用与实现详解
  • 基于quartz,刷新定时器的cron表达式
  • R语言学习笔记之开发环境配置
  • Spring Boot 邂逅Netty:构建高性能网络应用的奇妙之旅
  • iOS 权限管理:同时请求相机和麦克风权限的最佳实践
  • 工业网关边缘计算:智能制造的强劲引擎