SpringBoot使用RabbitMQ自动创建Exchange和Queue
背景
小项目,使用RabbitMQ作为消息队列,发布到不同的新环境时,由于新搭建的MQ中不存在Exchange和Queue,就会出错,还得手动去创建,比较麻烦,于是想在代码中将这些定义好后,自动控制MQ去创建。
原理与步骤
- 第一步定义RabbitAdmin Bean
- 第二步定义交换机 Bean
- 第三步定义Queue Bean
- 第四步定义Binding Bean, 这一步最关键,即将Queue与交换机
完整代码如下,定义如下代码后,即便使用新的MQ,启动项目也会自动创建,但是请注意 virtual-host 是无法被自动创建的(我目前的研究是这样,有自动创建的办法请留言告诉我,谢谢),因此如果不使用默认的virtual-host,请在MQ控制台中去创建。
package com.app.config;import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author Administrator* @date 2023/8/4 16:25* @description*/
@Configuration
public class RabbitConfig {@Autowiredprivate ConnectionFactory connectionFactory;@Beanpublic RabbitAdmin rabbitAdmin() {RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);rabbitAdmin.setAutoStartup(true);return rabbitAdmin;}@Bean("addOrderExchange")public DirectExchange addOrderExchange() {return ExchangeBuilder.directExchange("addOrderExchange").durable(true).build();}@Bean("defaultExchange")public DirectExchange defaultExchange() {return ExchangeBuilder.directExchange("").durable(true).build();}@Bean("addEvtDayInvest")public Queue addEvtDayInvest() {Queue queue = QueueBuilder.durable("addEvtDayInvest").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("addEvtDayInvestBind")public Binding addEvtDayInvestBind() {return BindingBuilder.bind(addEvtDayInvest()).to(addOrderExchange()).with("");}@Bean("addMemberData")public Queue addMemberData() {Queue queue = QueueBuilder.durable("addMemberData").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("addMemberDataBind")public Binding addMemberDataBind() {return BindingBuilder.bind(addMemberData()).to(addOrderExchange()).with("");}@Bean("addRepaymentPlant")public Queue addRepaymentPlant() {Queue queue = QueueBuilder.durable("addRepaymentPlant").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("addRepaymentPlantBind")public Binding addRepaymentPlantBind() {return BindingBuilder.bind(addRepaymentPlant()).to(addOrderExchange()).with("");}@Bean("imQueue")public Queue imQueue() {Queue queue = QueueBuilder.durable("imQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("imQueueBind")public Binding imQueueBind() {return BindingBuilder.bind(imQueue()).to(defaultExchange()).withQueueName();}@Bean("settleInterestQueue")public Queue settleInterestQueue() {Queue queue = QueueBuilder.durable("settleInterestQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("settleInterestQueueBind")public Binding settleInterestQueueBind() {return BindingBuilder.bind(settleInterestQueue()).to(defaultExchange()).withQueueName();}@Bean("registerQueue")public Queue registerQueue() {Queue queue = QueueBuilder.durable("registerQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("registerQueueBind")public Binding registerQueueBind() {return BindingBuilder.bind(registerQueue()).to(defaultExchange()).withQueueName();}@Bean("memberLoginLogQueue")public Queue memberLoginLogQueue() {Queue queue = QueueBuilder.durable("memberLoginLogQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("memberLoginLogQueueBind")public Binding memberLoginLogQueueBind() {return BindingBuilder.bind(memberLoginLogQueue()).to(defaultExchange()).withQueueName();}@Bean("addParentDataQueue")public Queue addParentDataQueue() {Queue queue = QueueBuilder.durable("addParentDataQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("addParentDataQueueBind")public Binding addParentDataQueueBind() {return BindingBuilder.bind(addParentDataQueue()).to(defaultExchange()).withQueueName();}@Bean("addOrderQueue")public Queue addOrderQueue() {Queue queue = QueueBuilder.durable("addOrderQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("addOrderQueueBind")public Binding addOrderQueueBind() {return BindingBuilder.bind(addOrderQueue()).to(defaultExchange()).withQueueName();}@Bean("memberPerformanceQueue")public Queue memberPerformanceQueue() {Queue queue = QueueBuilder.durable("memberPerformanceQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("memberPerformanceQueueBind")public Binding memberPerformanceQueueBind() {return BindingBuilder.bind(memberPerformanceQueue()).to(defaultExchange()).withQueueName();}@Bean("computeExpectedIncomeQueue")public Queue computeExpectedIncomeQueue() {Queue queue = QueueBuilder.durable("computeExpectedIncomeQueue").build();rabbitAdmin().declareQueue(queue);return queue;}@Bean("computeExpectedIncomeQueueBind")public Binding computeExpectedIncomeQueueBind() {return BindingBuilder.bind(computeExpectedIncomeQueue()).to(defaultExchange()).withQueueName();}/*** 自定义 json 格式发送消息** @return*/@Beanpublic MessageConverter messageConverter() {return new Jackson2JsonMessageConverter();}}
相关文章:
SpringBoot使用RabbitMQ自动创建Exchange和Queue
背景 小项目,使用RabbitMQ作为消息队列,发布到不同的新环境时,由于新搭建的MQ中不存在Exchange和Queue,就会出错,还得手动去创建,比较麻烦,于是想在代码中将这些定义好后,自动控制M…...
【设计模式】订单状态流传中的状态机与状态模式
文章目录 1. 前言2.状态模式2.1.订单状态流转案例2.1.1.状态枚举定义2.1.2.状态接口与实现2.1.3.状态机2.1.4.测试 2.2.退款状态的拓展2.2.1.代码拓展2.2.2.测试 2.3.小结 3.总结 1. 前言 状态模式一般是用在对象内部的状态流转场景中,用来实现状态机。 什么是状态…...
2.js中attr()用来修改或者添加属性或者属性值
attr()可以用来修改或者添加属性或者属性值 例:<input type"button" class"btn btn-info" id"subbtn" style"font-size:12px" value"我也说一句"/>1.如果想获取input中value的值 $(#subbtn).attr(value);…...
【虫洞攻击检测】使用多层神经网络的移动自组织网络中的虫洞攻击检测研究(Matlab代码实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
微分流形学习之一:基本定义
微分流形学习之一:基本定义引入 引言一、微分流形的历史简介二、拓扑空间三、微分流形 引言 本文是作者在学习微分流形的时候的笔记,尽量严格完整,并带有一定理解,绝不是结论的简单罗列。如果读者知道数学分析中的 ϵ − δ \ep…...
[C++]笔记-制作自己的静态库
一.静态库的创建 在项目属性c/c里面,选用无预编译头,创建头文件与cpp文件,需要注意release模式下还是debug模式,在用库时候要与该模式相匹配,库的函数实现是外界无法看到的,最后在要使用的项目里面导入.h文件和.lib文件 二.使用一个循环给二维数组赋值 行数 : 第几个元素 / …...
优酷视频码率、爱奇艺视频码率、B站视频码率、抖音视频码率对比
优酷视频码率、爱奇艺视频码率与YouTube视频码率对比 优酷视频码率: 优酷的视频码率可以根据视频质量、分辨率和内容类型而变化。一般而言,优酷提供了不同的码率选项,包括较低的标清(SD)码率和较高的高清(…...
用pytorch实现google net
GoogleNet(也称为Inception v1)是由Google在2014年提出的一个深度卷积神经网络架构。它在ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014比赛中取得了优秀的成绩,并引起了广泛的关注。 GoogleNet的设计目标是构建一个更…...
2023-8-15差分矩阵
题目链接:差分矩阵 #include <iostream>using namespace std;const int N 1010;int n, m, q; int a[N][N], b[N][N];void insert(int x1, int y1, int x2, int y2, int c) {b[x1][y1] c;b[x1][y2 1] - c;b[x2 1][y1] - c;b[x2 1][y2 1] c; }int main…...
物理公式分类
(99 封私信 / 81 条消息) 定义式和决定式有什么区别,怎么区分? - 知乎 (zhihu.com) 1、首先,定义一个物理符号(物理量)来表征物理世界最直观/最基本的物理现象,例如,长度(米…...
vue实现登录注册
目录 一、登录页面 二、注册页面 三、配置路由 一、登录页面 <template><div class"login_container" style"background-color: rgb(243,243,243);height: 93.68vh;background-image: url(https://ts1.cn.mm.bing.net/th/id/R-C.f878c96c4179c501a6…...
SpringBoot复习:(55)在service类中的方法上加上@Transactional注解后,Spring底层是怎么生成代理对象的?
SpringBoot run方法代码如下: 可以看到它会调用refreshContext方法来刷新Spring容器,这个refreshContext方法最终会调用AbstractApplicationContext的refresh方法,代码如下 如上图,refresh方法最终会调用finisheBeanFactoryInit…...
常用的图像校正方法
在数字图像处理中,常用的校正方法包括明场均匀性校正、查找表(LUT)校正和伽玛(Gamma)校正。这些校正方法分别针对不同的图像问题,可以改善图像质量,提升图像的可读性和可分析性。下面是这三种校…...
AWS security 培训笔记
云计算的好处 Amazon S3 (Storage) Amazon EC2 (Compute) 上图aws 的几个支柱:安全是其中一个啦 其中安全有几个方面 IAMdetection基础架构保护数据保护应急响应 关于云供应商的责任 data center 原来长这样 ,据说非常之隐蔽的 如果有天退役了…...
设计模式之代理模式(Proxy)的C++实现
1、代理模式的提出 在组件的开发过程中,有些对象由于某种原因(比如对象创建的开销很大,或者对象的一些操作需要做安全控制,或者需要进程外的访问等),会使Client使用者在操作这类对象时可能会存在问题&…...
vim 配置环境变量与 JDK 编译器异常
vim 配置环境变量 使用 vim 打开系统中的配置信息(不存在将会创建): vim ~/.bash_profile 以配置两个版本 JDK 为例(前提是已安装 JDK),使用上述命令打开配置信息: 输入法调成英文,输入 i&…...
TiDB v7.1.0 跨业务系统多租户解决方案
本文介绍了 TiDB 数据库的资源管控技术,并通过业务测试验证了效果。资源管控技术旨在解决多业务共用一个集群时的资源隔离和负载问题,通过资源组概念,可以限制不同业务的计算和 I/O 资源,实现资源隔离和优先级调度,提高…...
【题解】二叉树中和为某一值的路径(一)
二叉树中和为某一值的路径(一) 题目链接:二叉树中和为某一值的路径(一) 解题思路1:递归 我们或许想记录下每一条从根节点到叶子节点的路径,计算出该条路径的和,但此种思路用递归稍麻烦,我们可以试着把和转换为差&am…...
css中变量和使用变量和运算
变量: 语法:--css变量名:值; --view-theme: #1a99fb; css使用变量: 语法:属性名:var( --css变量名 ); color: var(--view-theme); css运算: 语法:属性名…...
数据结构之线性表的类型运用Linear Lists: 数组,栈,队列,链表
线性表 定义 一个最简单,最基本的数据结构。一个线性表由多个相同类型的元素穿在一次,并且每一个元素都一个前驱(前一个元素)和后继(后一个元素)。 线性表的类型 常见的类型:数组、栈、队列…...
XML Group端口详解
在XML数据映射过程中,经常需要对数据进行分组聚合操作。例如,当处理包含多个物料明细的XML文件时,可能需要将相同物料号的明细归为一组,或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码,增加了开…...
第19节 Node.js Express 框架
Express 是一个为Node.js设计的web开发框架,它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用,和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...
rknn优化教程(二)
文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK,开始写第二篇的内容了。这篇博客主要能写一下: 如何给一些三方库按照xmake方式进行封装,供调用如何按…...
练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...
Keil 中设置 STM32 Flash 和 RAM 地址详解
文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...
【论文阅读28】-CNN-BiLSTM-Attention-(2024)
本文把滑坡位移序列拆开、筛优质因子,再用 CNN-BiLSTM-Attention 来动态预测每个子序列,最后重构出总位移,预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵(S…...
Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理
引言 Bitmap(位图)是Android应用内存占用的“头号杀手”。一张1080P(1920x1080)的图片以ARGB_8888格式加载时,内存占用高达8MB(192010804字节)。据统计,超过60%的应用OOM崩溃与Bitm…...
RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
RabbitMQ 一、RabbitMQ概述 RabbitMQ RabbitMQ最初由LShift和CohesiveFT于2007年开发,后来由Pivotal Software Inc.(现为VMware子公司)接管。RabbitMQ 是一个开源的消息代理和队列服务器,用 Erlang 语言编写。广泛应用于各种分布…...
从面试角度回答Android中ContentProvider启动原理
Android中ContentProvider原理的面试角度解析,分为已启动和未启动两种场景: 一、ContentProvider已启动的情况 1. 核心流程 触发条件:当其他组件(如Activity、Service)通过ContentR…...
【前端异常】JavaScript错误处理:分析 Uncaught (in promise) error
在前端开发中,JavaScript 异常是不可避免的。随着现代前端应用越来越多地使用异步操作(如 Promise、async/await 等),开发者常常会遇到 Uncaught (in promise) error 错误。这个错误是由于未正确处理 Promise 的拒绝(r…...
