集成RabbitMQ+MQ常用操作
文章目录
- 1.环境搭建
- 1.Docker安装RabbitMQ
- 1.拉取镜像
- 2.安装命令
- 3.开启5672和15672端口
- 4.登录控制台
- 2.整合Spring AMQP
- 1.sun-common模块下创建新模块
- 2.引入amqp依赖和fastjson
- 3.新建一个mq-demo的模块
- 1.在sun-frame下创建mq-demo
- 2.然后在mq-demo下创建生产者和消费者子模块
- 3.查看是否交给父模块管理了
- 4.在mq-demo模块引入sun-common-rabbitmq依赖
- 5.publisher引入sun-common-test依赖
- 6.将sun-common-rabbitmq clean-install一下
- 7.给consumer和publisher都创建主类
- 1.ConsumerApplication.java
- 2.PublisherApplication.java
- 4.测试MQ
- 1.application.yml mq的最基本配置
- 2.consumer
- 1.TestConfig.java MQ配置
- 2.TestConfigListener.java 监听队列
- 3.publisher
- 1.TestConfig.java 测试(注意指定启动类)
- 2.结果
- 2.基本交换机
- 1.Fanout
- 1.FanoutConfig.java 交换机配置
- 2.FanoutConfigListener.java 监听者
- 3.FanoutConfig.java 生产者
- 2.Direct
- 1.DirectConfig.java 交换机配置
- 2.DirectConfigListener.java 监听者
- 3.DirectConfig.java 生产者
1.环境搭建
1.Docker安装RabbitMQ
1.拉取镜像
docker pull rabbitmq:3.8-management
2.安装命令
docker run -e RABBITMQ_DEFAULT_USER=sun \-e RABBITMQ_DEFAULT_PASS=mq \-v mq-plugins:/plugins \--name mq \--hostname mq \-p 15672:15672 \-p 5672:5672 \-d 699038cb2b96 # 注意这里是镜像id,需要替换
3.开启5672和15672端口
4.登录控制台
15672端口
2.整合Spring AMQP
1.sun-common模块下创建新模块

2.引入amqp依赖和fastjson
<?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>sun-common</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>sun-common-rabbitmq</artifactId><!-- 子模块的version,如果不写就默认跟父模块的一样 --><version>${children.version}</version><!-- 自定义依赖,无需版本号 --><dependencies><!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!-- 用于传递消息时的序列化操作 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency></dependencies></project>
3.新建一个mq-demo的模块
1.在sun-frame下创建mq-demo

2.然后在mq-demo下创建生产者和消费者子模块


3.查看是否交给父模块管理了

4.在mq-demo模块引入sun-common-rabbitmq依赖
<dependencies><!-- 引入sun-common-rabbitmq --><dependency><groupId>com.sunxiansheng</groupId><artifactId>sun-common-rabbitmq</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
5.publisher引入sun-common-test依赖
<dependencies><!-- sun-common-test --><dependency><groupId>com.sunxiansheng</groupId><artifactId>sun-common-test</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
6.将sun-common-rabbitmq clean-install一下
7.给consumer和publisher都创建主类
1.ConsumerApplication.java
package com.sunxiansheng.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan("com.sunxiansheng")
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}
}
2.PublisherApplication.java
package com.sunxiansheng.publisher;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class PublisherApplication {public static void main(String[] args) {SpringApplication.run(PublisherApplication.class);}
}
4.测试MQ
1.application.yml mq的最基本配置
spring:# RabbitMQ 配置rabbitmq:# 服务器地址host: ip# 用户名username: sunxiansheng# 密码password: rabbitmq# 虚拟主机virtual-host: /# 端口port: 5672
2.consumer
1.TestConfig.java MQ配置
package com.sunxiansheng.consumer.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: 最基本的MQ测试* @Author sun* @Create 2024/8/2 14:34* @Version 1.0*/
@Configuration
public class TestConfig {/*** 创建一个fanout类型的交换机* @return*/@Beanpublic FanoutExchange fanoutExchange() {return new FanoutExchange("fanout.exchange.test");}/*** 创建一个队列* @return*/@Beanpublic Queue fanoutQueueTest() {return new Queue("fanout.queue.test");}/*** 交换机和队列绑定*/@Beanpublic Binding binding() {return BindingBuilder.bind(fanoutQueueTest()).to(fanoutExchange());}}
2.TestConfigListener.java 监听队列
package com.sunxiansheng.consumer.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** Description: 最基本的MQ测试* @Author sun* @Create 2024/8/2 14:34* @Version 1.0*/
@Component
public class TestConfigListener {@RabbitListener(queues = "fanout.queue.test")public void receive(String message) {System.out.println("接收到的消息:" + message);}}
3.publisher
1.TestConfig.java 测试(注意指定启动类)
package com.sunxiansheng.consumer.config;import com.sunxiansheng.publisher.PublisherApplication;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** Description: 最基本的MQ测试* @Author sun* @Create 2024/8/2 14:34* @Version 1.0*/
@SpringBootTest(classes = PublisherApplication.class) // 指定启动类
public class TestConfig {@Resourceprivate AmqpTemplate amqpTemplate;@Testpublic void send() {// 交换机String exchange = "fanout.exchange.test";// 路由键String routingKey = "";// 消息String message = "hello fanout";// 发送消息amqpTemplate.convertAndSend(exchange, routingKey, message);}}
2.结果

2.基本交换机
1.Fanout
1.FanoutConfig.java 交换机配置
package com.sunxiansheng.consumer.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: Fanout交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@Configuration
public class FanoutConfig {@Beanpublic FanoutExchange fanoutExchange1() {// 创建一个fanout类型的交换机return new FanoutExchange("fanout.exchange");}@Beanpublic Queue fanoutQueue1() {// 创建一个队列return new Queue("fanout.queue1");}@Beanpublic Queue fanoutQueue2() {// 创建一个队列return new Queue("fanout.queue2");}// 两个队列绑定到交换机上@Beanpublic Binding bindingFanoutQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange1) {return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange1);}@Beanpublic Binding bindingFanoutQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange1) {return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange1);}
}
2.FanoutConfigListener.java 监听者
package com.sunxiansheng.consumer.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** Description: Fanout交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@Component
public class FanoutConfigListener {@RabbitListener(queues = "fanout.queue1")public void receive1(String message) {System.out.println("fanout.queue1接收到的消息:" + message);}@RabbitListener(queues = "fanout.queue2")public void receive2(String message) {System.out.println("fanout.queue2接收到的消息:" + message);}}
3.FanoutConfig.java 生产者
package com.sunxiansheng.consumer.config;import com.sunxiansheng.publisher.PublisherApplication;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** Description: Fanout交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@SpringBootTest(classes = PublisherApplication.class) // 指定启动类
public class FanoutConfig {@Resourceprivate AmqpTemplate amqpTemplate;@Testpublic void send() {// 交换机String exchange = "fanout.exchange";// 路由键String routingKey = "";// 消息String message = "hello fanout";// 发送消息amqpTemplate.convertAndSend(exchange, routingKey, message);}}
2.Direct
1.DirectConfig.java 交换机配置
package com.sunxiansheng.consumer.config;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Description: Direct交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@Configuration
public class DirectConfig {@Beanpublic DirectExchange directExchange() {// 创建一个direct类型的交换机return new DirectExchange("direct.exchange");}@Beanpublic Queue directQueue1() {// 创建一个队列return new Queue("direct.queue1");}@Beanpublic Queue directQueue2() {// 创建一个队列return new Queue("direct.queue2");}// 两个队列绑定到交换机上,这里需要指定routingKey@Beanpublic Binding bindingDirectQueue1(Queue directQueue1, DirectExchange directExchange) {return BindingBuilder.bind(directQueue1).to(directExchange).with("black");}@Beanpublic Binding bindingDirectQueue2(Queue directQueue2, DirectExchange directExchange) {return BindingBuilder.bind(directQueue2).to(directExchange).with("green");}}
2.DirectConfigListener.java 监听者
package com.sunxiansheng.consumer.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** Description: Direct交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@Component
public class DirectConfigListener {@RabbitListener(queues = "direct.queue1")public void receive1(String message) {System.out.println("direct.queue1接收到的消息:" + message);}@RabbitListener(queues = "direct.queue2")public void receive2(String message) {System.out.println("direct.queue2接收到的消息:" + message);}}
3.DirectConfig.java 生产者
package com.sunxiansheng.consumer.config;import com.sunxiansheng.publisher.PublisherApplication;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** Description: Direct交换机* @Author sun* @Create 2024/7/29 15:06* @Version 1.0*/
@SpringBootTest(classes = PublisherApplication.class) // 指定启动类
public class DirectConfig {@Resourceprivate AmqpTemplate amqpTemplate;@Testpublic void send() {// 交换机String exchange = "direct.exchange";// 路由键String routingKey = "black";// 消息String message = "hello direct";// 发送消息amqpTemplate.convertAndSend(exchange, routingKey, message);}}
相关文章:
集成RabbitMQ+MQ常用操作
文章目录 1.环境搭建1.Docker安装RabbitMQ1.拉取镜像2.安装命令3.开启5672和15672端口4.登录控制台 2.整合Spring AMQP1.sun-common模块下创建新模块2.引入amqp依赖和fastjson 3.新建一个mq-demo的模块1.在sun-frame下创建mq-demo2.然后在mq-demo下创建生产者和消费者子模块3.查…...
PVE虚拟化平台之开启虚拟机IP显示方法
PVE虚拟化平台之开启虚拟机IP显示方法 一、PVE平台介绍1.1 PVE简介1.2 PVE特点1.3 PVE主要使用场景二、检查PVE环境2.1 环境介绍2.2 检查PVE和虚拟机状态三、虚拟机开启Qemu代理四、Linux虚拟机安装Guest-Agent4.1 进入虚拟机VNC控制台4.2 查看虚拟机IP五、Windows虚拟机安装Gu…...
子Shell及Shell嵌套模式
子Shell 概念 Shell子进程,Shell脚本是从上至下,从左至右依次执行每一行的命令及语句的,即执行完一个命令之后再执行下一个。如果在shell脚本中遇到子脚本(即脚本嵌套),就会先执行子脚本的内容,完成后再返回父脚本继…...
Onedrive精神分裂怎么办(有变更却不同步)
Onedrive有时候会分裂,你在本地删除文件,并没有同步到云端,但是本地却显示同步成功。 比如删掉了一个目录,在本地看已经删掉,onedrive显示已同步,但是别的电脑并不会同步到这个删除操作,在网页版…...
【gym】给定的强化学习环境简介(二)
文章目录 环境介绍一 box2dbipedal_walkercar_dynamicscar_racinglunar_lander 二、 classic_controlacrobotCartPolecontinuous_mountain_carmountain_carpendulum 三、toy_textblackjackcliffwalkingfrozentaxi 四、mujocoAnt:HalfCheetah:Hopper&…...
ctfhub disable_functions关卡
1.CTFHub Bypass disable_function —— LD_PRELOAD 2.CTFHub Bypass disable_function —— ShellShock 3.CTFHub Bypass disable_function —— Apache Mod CGI 4.CTFHub Bypass disable_function —— 攻击PHP-FPM 5.CTFHub Bypass disable_function —— GC UAF 6.CTFHub B…...
SpringAI人工智能开发框架006---SpringAI多模态接口_编程测试springai多模态接口支持
可以看到springai对多模态的支持. 同样去创建一个项目 也是跟之前的项目一样,修改版本1.0.0 这里 然后修改仓库地址,为springai的地址 然后开始写代码...
2025年我国网络安全发展形势展望
展望2025年,我国网络安全产业有望迎来新的快速增长阶段,零信任安全架构将在各行各业加快应用落地,数据安全技术攻关和应用进程加快,关键基础设施安全能力不断提升。同时,也应关注国家级网络对抗风险加剧、网络安全产业…...
【漏洞复现】BIG-IP Next Central Manager OData 注入漏洞(CVE-2024-21793)
🏘️个人主页: 点燃银河尽头的篝火(●’◡’●) 如果文章有帮到你的话记得点赞👍+收藏💗支持一下哦 一、漏洞概述 1.1漏洞简介 漏洞名称:BIG-IP Next Central Manager OData 注入漏洞漏洞编号:CVE-2024-21793漏洞威胁等级:超危影响范围:BIG-IP Next Central Manage…...
GitLab部署到阿里云服务器上
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。 一、安装 1.创建一…...
VTK知识学习(27)- 图像基本操作(二)
1、图像类型转换 1)vtkImageCast 图像数据类型转换在数字图像处理中会频繁用到。一些常用的图像算子(例如梯度算子)在计算时出于精度的考虑,会将结果存储为float或double类型,但在图像显示时,一般要求图像为 unsigned char 类型,…...
MyBatis如何处理延迟加载?
大家好,我是锋哥。今天分享关于【MyBatis如何处理延迟加载?】面试题。希望对大家有帮助; MyBatis如何处理延迟加载? 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 MyBatis 支持 延迟加载(Lazy Loading&am…...
前端网络之【浏览器跨域问题分析与解决方案】
文章目录 同源策略同源与异源跨域的限制场景解决方案 CORS 请求分类 简单请求预检请求 注意点一 【关于Cookie】注意点二 【关于跨域获取响应头】 JSONP 基本流程 代理服务器 如何选择对应方案 同源策略 **同源策略是一套浏览器的安全策略机制,当一个源的文档和脚…...
LeetCode:3218. 切蛋糕的最小总开销 I(贪心 Java)
目录 3218. 切蛋糕的最小总开销 I 题目描述: 实现代码与解析: 贪心 原理思路: 3218. 切蛋糕的最小总开销 I 题目描述: 有一个 m x n 大小的矩形蛋糕,需要切成 1 x 1 的小块。 给你整数 m ,n 和两个数…...
前端下载后端文件流,文件可以下载,但是打不开,显示“文件已损坏”的问题分析与解决方案
目录 场景还原 相关代码开发者工具 - 网络请求记录 问题排查 定位改bug 总结 场景还原 我在前端使用axios接收后端xlsx表格文件流并下载,xlsx文件能够下载成功,但是打开却显示文件无法打开 相关代码 请求API封装:Content–Type以及responseType经核…...
PageRank Web页面分级算法 HNUST【数据分析技术】(2025)
1.理论知识 算法原理PageRank 通过网络浩瀚的超链接关系来确定一个页面的等级。 Google 把从 A 页面到 B 页面的链接解释为A页面给B页面投票, Google 根据投票来源(甚至来源的来源, 即链接到A页面的页面)和投票目标的等级来决定新…...
数字IC前端学习笔记:脉动阵列的设计方法学(四)
相关阅读 数字IC前端https://blog.csdn.net/weixin_45791458/category_12173698.html?spm1001.2014.3001.5482 引言 脉动结构(也称为脉动阵列)表示一种有节奏地计算并通过系统传输数据的处理单元(PEs)网络。这些处理单元有规律地泵入泵出数据以保持规则…...
对话 Project Astra 研究主管:打造通用 AI 助理,主动视频交互和全双工对话是未来重点
Project Astra 愿景之一:「系统不仅能在你说话时做出回应,还能在持续的过程中帮助你。」 近期,Google DeepMind 的 YouTube 频道采访了 Google DeepMind 研究主管格雷格韦恩 (Greg Wayne)。 格雷格韦恩的研究工作为 DeepMind 的诸多突破性成…...
NetApp 存储设备巡检作业指导书
NetApp 存储设备巡检作业指导书 一、目的 本指导书旨在指导管理员通过 SSH 或 Console 登录 NetApp FAS2552 存储系统,切换节点并进行日常管理操作。 二、适用范围 适用于基于 NetApp ONTAP 操作系统的 FAS2552 存储环境。 三、前提条件 网络和权限要求࿱…...
adb无法连接到安卓设备【解决方案】报错:adb server version (40) doesn‘t match this client (41);
下载老版本Platformtoolshttps://dl.google.com/android/repository/platform-tools_r28.0.2-windows.zip?hlzh-cn 替换原来的platform-tools文件夹即可。 问题原因分析:电脑端adb client版本(41)和安卓端adb …...
【Axure高保真原型】引导弹窗
今天和大家中分享引导弹窗的原型模板,载入页面后,会显示引导弹窗,适用于引导用户使用页面,点击完成后,会显示下一个引导弹窗,直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…...
循环冗余码校验CRC码 算法步骤+详细实例计算
通信过程:(白话解释) 我们将原始待发送的消息称为 M M M,依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)(意思就是 G ( x ) G(x) G(x) 是已知的)࿰…...
家政维修平台实战20:权限设计
目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系,主要是分成几个表,用户表我们是记录用户的基础信息,包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题,不同的角色…...
[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?
论文网址:pdf 英文是纯手打的!论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误,若有发现欢迎评论指正!文章偏向于笔记,谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...
Qt Http Server模块功能及架构
Qt Http Server 是 Qt 6.0 中引入的一个新模块,它提供了一个轻量级的 HTTP 服务器实现,主要用于构建基于 HTTP 的应用程序和服务。 功能介绍: 主要功能 HTTP服务器功能: 支持 HTTP/1.1 协议 简单的请求/响应处理模型 支持 GET…...
相机从app启动流程
一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...
【AI学习】三、AI算法中的向量
在人工智能(AI)算法中,向量(Vector)是一种将现实世界中的数据(如图像、文本、音频等)转化为计算机可处理的数值型特征表示的工具。它是连接人类认知(如语义、视觉特征)与…...
Robots.txt 文件
什么是robots.txt? robots.txt 是一个位于网站根目录下的文本文件(如:https://example.com/robots.txt),它用于指导网络爬虫(如搜索引擎的蜘蛛程序)如何抓取该网站的内容。这个文件遵循 Robots…...
【服务器压力测试】本地PC电脑作为服务器运行时出现卡顿和资源紧张(Windows/Linux)
要让本地PC电脑作为服务器运行时出现卡顿和资源紧张的情况,可以通过以下几种方式模拟或触发: 1. 增加CPU负载 运行大量计算密集型任务,例如: 使用多线程循环执行复杂计算(如数学运算、加密解密等)。运行图…...
【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统
目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索(基于物理空间 广播范围)2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...
