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

SpringBoot使用RabbitMQ自动创建Exchange和Queue

背景

小项目,使用RabbitMQ作为消息队列,发布到不同的新环境时,由于新搭建的MQ中不存在Exchange和Queue,就会出错,还得手动去创建,比较麻烦,于是想在代码中将这些定义好后,自动控制MQ去创建。

原理与步骤

  1. 第一步定义RabbitAdmin Bean
  2. 第二步定义交换机 Bean
  3. 第三步定义Queue Bean
  4. 第四步定义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()可以用来修改或者添加属性或者属性值 例&#xff1a;<input type"button" class"btn btn-info" id"subbtn" style"font-size:12px" value"我也说一句"/>1.如果想获取input中value的值 $(#subbtn).attr(value);…...

【虫洞攻击检测】使用多层神经网络的移动自组织网络中的虫洞攻击检测研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

微分流形学习之一:基本定义

微分流形学习之一&#xff1a;基本定义引入 引言一、微分流形的历史简介二、拓扑空间三、微分流形 引言 本文是作者在学习微分流形的时候的笔记&#xff0c;尽量严格完整&#xff0c;并带有一定理解&#xff0c;绝不是结论的简单罗列。如果读者知道数学分析中的 ϵ − δ \ep…...

[C++]笔记-制作自己的静态库

一.静态库的创建 在项目属性c/c里面,选用无预编译头,创建头文件与cpp文件,需要注意release模式下还是debug模式,在用库时候要与该模式相匹配,库的函数实现是外界无法看到的,最后在要使用的项目里面导入.h文件和.lib文件 二.使用一个循环给二维数组赋值 行数 : 第几个元素 / …...

优酷视频码率、爱奇艺视频码率、B站视频码率、抖音视频码率对比

优酷视频码率、爱奇艺视频码率与YouTube视频码率对比 优酷视频码率&#xff1a; 优酷的视频码率可以根据视频质量、分辨率和内容类型而变化。一般而言&#xff0c;优酷提供了不同的码率选项&#xff0c;包括较低的标清&#xff08;SD&#xff09;码率和较高的高清&#xff08;…...

用pytorch实现google net

GoogleNet&#xff08;也称为Inception v1&#xff09;是由Google在2014年提出的一个深度卷积神经网络架构。它在ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014比赛中取得了优秀的成绩&#xff0c;并引起了广泛的关注。 GoogleNet的设计目标是构建一个更…...

2023-8-15差分矩阵

题目链接&#xff1a;差分矩阵 #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 条消息) 定义式和决定式有什么区别&#xff0c;怎么区分&#xff1f; - 知乎 (zhihu.com) 1、首先&#xff0c;定义一个物理符号&#xff08;物理量&#xff09;来表征物理世界最直观/最基本的物理现象&#xff0c;例如&#xff0c;长度&#xff08;米&#xf…...

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方法代码如下&#xff1a; 可以看到它会调用refreshContext方法来刷新Spring容器&#xff0c;这个refreshContext方法最终会调用AbstractApplicationContext的refresh方法&#xff0c;代码如下 如上图&#xff0c;refresh方法最终会调用finisheBeanFactoryInit…...

常用的图像校正方法

在数字图像处理中&#xff0c;常用的校正方法包括明场均匀性校正、查找表&#xff08;LUT&#xff09;校正和伽玛&#xff08;Gamma&#xff09;校正。这些校正方法分别针对不同的图像问题&#xff0c;可以改善图像质量&#xff0c;提升图像的可读性和可分析性。下面是这三种校…...

AWS security 培训笔记

云计算的好处 Amazon S3 (Storage) Amazon EC2 (Compute) 上图aws 的几个支柱&#xff1a;安全是其中一个啦 其中安全有几个方面 IAMdetection基础架构保护数据保护应急响应 关于云供应商的责任 data center 原来长这样 &#xff0c;据说非常之隐蔽的 如果有天退役了&#xf…...

设计模式之代理模式(Proxy)的C++实现

1、代理模式的提出 在组件的开发过程中&#xff0c;有些对象由于某种原因&#xff08;比如对象创建的开销很大&#xff0c;或者对象的一些操作需要做安全控制&#xff0c;或者需要进程外的访问等&#xff09;&#xff0c;会使Client使用者在操作这类对象时可能会存在问题&…...

vim 配置环境变量与 JDK 编译器异常

vim 配置环境变量 使用 vim 打开系统中的配置信息&#xff08;不存在将会创建&#xff09;&#xff1a; vim ~/.bash_profile 以配置两个版本 JDK 为例&#xff08;前提是已安装 JDK&#xff09;,使用上述命令打开配置信息&#xff1a; 输入法调成英文&#xff0c;输入 i&…...

TiDB v7.1.0 跨业务系统多租户解决方案

本文介绍了 TiDB 数据库的资源管控技术&#xff0c;并通过业务测试验证了效果。资源管控技术旨在解决多业务共用一个集群时的资源隔离和负载问题&#xff0c;通过资源组概念&#xff0c;可以限制不同业务的计算和 I/O 资源&#xff0c;实现资源隔离和优先级调度&#xff0c;提高…...

【题解】二叉树中和为某一值的路径(一)

二叉树中和为某一值的路径(一) 题目链接&#xff1a;二叉树中和为某一值的路径(一) 解题思路1&#xff1a;递归 我们或许想记录下每一条从根节点到叶子节点的路径&#xff0c;计算出该条路径的和&#xff0c;但此种思路用递归稍麻烦&#xff0c;我们可以试着把和转换为差&am…...

css中变量和使用变量和运算

变量&#xff1a; 语法&#xff1a;--css变量名&#xff1a;值&#xff1b; --view-theme: #1a99fb; css使用变量&#xff1a; 语法&#xff1a;属性名&#xff1a;var( --css变量名 )&#xff1b; color: var(--view-theme); css运算&#xff1a; 语法&#xff1a;属性名…...

数据结构之线性表的类型运用Linear Lists: 数组,栈,队列,链表

线性表 定义 一个最简单&#xff0c;最基本的数据结构。一个线性表由多个相同类型的元素穿在一次&#xff0c;并且每一个元素都一个前驱&#xff08;前一个元素&#xff09;和后继&#xff08;后一个元素&#xff09;。 线性表的类型 常见的类型&#xff1a;数组、栈、队列…...

DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径

目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

《用户共鸣指数(E)驱动品牌大模型种草:如何抢占大模型搜索结果情感高地》

在注意力分散、内容高度同质化的时代&#xff0c;情感连接已成为品牌破圈的关键通道。我们在服务大量品牌客户的过程中发现&#xff0c;消费者对内容的“有感”程度&#xff0c;正日益成为影响品牌传播效率与转化率的核心变量。在生成式AI驱动的内容生成与推荐环境中&#xff0…...

【ROS】Nav2源码之nav2_behavior_tree-行为树节点列表

1、行为树节点分类 在 Nav2(Navigation2)的行为树框架中,行为树节点插件按照功能分为 Action(动作节点)、Condition(条件节点)、Control(控制节点) 和 Decorator(装饰节点) 四类。 1.1 动作节点 Action 执行具体的机器人操作或任务,直接与硬件、传感器或外部系统…...

《通信之道——从微积分到 5G》读书总结

第1章 绪 论 1.1 这是一本什么样的书 通信技术&#xff0c;说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号&#xff08;调制&#xff09; 把信息从信号中抽取出来&am…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...

基于Java+VUE+MariaDB实现(Web)仿小米商城

仿小米商城 环境安装 nodejs maven JDK11 运行 mvn clean install -DskipTestscd adminmvn spring-boot:runcd ../webmvn spring-boot:runcd ../xiaomi-store-admin-vuenpm installnpm run servecd ../xiaomi-store-vuenpm installnpm run serve 注意&#xff1a;运行前…...

PHP 8.5 即将发布:管道操作符、强力调试

前不久&#xff0c;PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5&#xff01;作为 PHP 语言的又一次重要迭代&#xff0c;PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是&#xff0c;借助强大的本地开发环境 ServBay&am…...

「全栈技术解析」推客小程序系统开发:从架构设计到裂变增长的完整解决方案

在移动互联网营销竞争白热化的当下&#xff0c;推客小程序系统凭借其裂变传播、精准营销等特性&#xff0c;成为企业抢占市场的利器。本文将深度解析推客小程序系统开发的核心技术与实现路径&#xff0c;助力开发者打造具有市场竞争力的营销工具。​ 一、系统核心功能架构&…...

springboot 日志类切面,接口成功记录日志,失败不记录

springboot 日志类切面&#xff0c;接口成功记录日志&#xff0c;失败不记录 自定义一个注解方法 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/***…...