SpringBoot整合rabbitmq-主題交換機隊列(四)

說明:Topic主題交換機它的大致流程是交換機和一個或者多個隊列綁定,這個綁定的Routingkey是包含通配符的,滿足通配符的隊列會接收到消息。

通配符規則:

#:匹配一個或多個詞

*:匹配一個詞

例如:

topic.#:能匹配 topic.xxx 或者 topic.xxx.xxx

topic.*:只能匹配 topic.xxx

工程圖:

A.總體maven依賴

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><artifactId>spring-boot-starter-parent</artifactId>  <!-- 被繼承的父項目的構件標識符 --><groupId>org.springframework.boot</groupId>  <!-- 被繼承的父項目的全球唯一標識符 --><version>2.2.2.RELEASE</version>  <!-- 被繼承的父項目的版本 --><relativePath/> <!-- lookup parent from repository --></parent><groupId>RabbitMqSpringbootDemo</groupId><artifactId>RabbitMqSpringbootDemo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>MqCustomer</module><module>MqProducer</module></modules><name>RabbitMqSpringbootDemo Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies></dependencies><build><finalName>RabbitMqSpringbootDemo</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

B.生產者

1.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"><parent><artifactId>RabbitMqSpringbootDemo</artifactId><groupId>RabbitMqSpringbootDemo</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>MqProducer</artifactId><packaging>jar</packaging><name>MqProducer Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--spring boot核心--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--spring boot 測試--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--springmvc web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--開發環境調試--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!--amqp 支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.78</version></dependency><!-- commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency></dependencies><build><finalName>MqProducer</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

2.application.yml

server:port: 8080
spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: adminvirtual-host: /

3.TopicRabbitConfig

package com.dev.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 類名稱:主題交換機的使用** @author lqw* @date 2024年02月28日 10:40*/
@Configuration
public class TopicRabbitConfig {//綁定鍵public final static String high = "topic.highSchool.one";public final static String middle = "topic.middleSchool.one";@Beanpublic Queue oneQueue() {return new Queue(TopicRabbitConfig.high);}@Beanpublic Queue twoQueue() {return new Queue(TopicRabbitConfig.middle);}@BeanTopicExchange exchange() {return new TopicExchange("topicExchange");}@BeanBinding bindingExchangeMessageA() {return BindingBuilder.bind(oneQueue()).to(exchange()).with("topic.highSchool.#");}@BeanBinding bindingExchangeMessageB() {return BindingBuilder.bind(twoQueue()).to(exchange()).with("topic.middleSchool.#");}}

4.TopicRabbitController

package com.dev.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** 類名稱:主題交換機 消息生產者** @author lqw* @date 2024年02月27日 14:47*/
@Slf4j
@RestController
@RequestMapping("topic")
public class TopicRabbitController {@AutowiredRabbitTemplate rabbitTemplate;  //使用RabbitTemplate,這提供了接收/發送等等方法/*** topic測試* @return*/@GetMapping("/sendMessageA")//匹配到 topic.highSchool.onepublic String sendMessageA() {Map<String,Object> map = new HashMap<>();map.put("name","張龍");map.put("school","高中");rabbitTemplate.convertAndSend("topicExchange", "topic.highSchool.two", map);return "ok";}@GetMapping("/sendMessageB")//匹配到 topic.middleSchool.onepublic String sendMessageB() {Map<String,Object> map = new HashMap<>();map.put("name","趙虎");map.put("school","初中");rabbitTemplate.convertAndSend("topicExchange", "topic.middleSchool.two", map);return "ok";}@GetMapping("/sendMessageBB")//匹配到 topic.middleSchool.onepublic String sendMessageBB() {Map<String,Object> map = new HashMap<>();map.put("name","趙虎B");map.put("school","初中B");rabbitTemplate.convertAndSend("topicExchange", "topic.middleSchool.two.cc", map);return "ok";}}

5.AppP

package com.dev;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 類名稱:** @author 李慶偉* @date 2024年02月27日 14:20*/
@SpringBootApplication
public class AppP {public static void main(String[] args) {SpringApplication.run(AppP.class);}
}

C.消費者

1.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"><parent><artifactId>RabbitMqSpringbootDemo</artifactId><groupId>RabbitMqSpringbootDemo</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>MqCustomer</artifactId><packaging>jar</packaging><name>MqCustomer Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><!--spring boot核心--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--spring boot 測試--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--springmvc web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--開發環境調試--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!--amqp 支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!-- commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency></dependencies><build><finalName>MqCustomer</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

2.application.yml

server:port: 8081spring:rabbitmq:port: 5672host: 192.168.18.145username: adminpassword: admin

3.TopicRabbitListenerA

package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;/*** 類名稱:主題交換機,消息消費者* 直連* @author lqw* @date 2024年02月27日 14:58*/
@Component
public class TopicRabbitListenerA {@RabbitListener(queues = "topic.highSchool.one")@RabbitHandlerpublic void process(Map msg) {System.out.println("Rabbitmq topic : " + msg);System.out.println("Rabbitmq topic : " + msg);}}

4.TopicRabbitListenerB

package cn.ct.listeners;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;/*** 類名稱:主題交換機,消息消費者* 直連* @author lqw* @date 2024年02月27日 14:58*/
@Component
public class TopicRabbitListenerB {@RabbitListener(queues = "topic.middleSchool.one")@RabbitHandlerpublic void process(Map msg) {System.out.println("Rabbitmq topic : " + msg);System.out.println("Rabbitmq topic : " + msg);}}

5.AppC

package cn.ct;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 類名稱:** @author lqw* @date 2024年02月27日 14:20*/
@SpringBootApplication
public class AppC {public static void main(String[] args) {SpringApplication.run(AppC.class);}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/714445.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/714445.shtml
英文地址,請注明出處:http://en.pswp.cn/news/714445.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2024洗地機深度測評推薦,洗地機哪款值得入手?新手入門必看洗地機選購技巧

洗地機是近年來備受矚目的智能家居產品&#xff0c;它能夠有效地幫助我們節省勞動時間&#xff0c;高效清潔地面&#xff0c;從而減輕我們的勞動負擔。洗地機實現了掃地和拖地的一體化功能&#xff0c;在掃地的同時還能順便完成地面的拖洗工作。配備水箱的設計使得不再需要頻繁…

【kubernetes VPA】記錄一次安裝 VPA 相關組件的報錯解決過程

文章目錄 1. 問題描述2. 問題原因3. 解決辦法4. 參考鏈接 1. 問題描述 在執行 ./hack/vpa-up.sh腳本命令時&#xff0c;提示有報錯。名為vpa-admission-controller的容器狀態一直停留在ContainerCreating&#xff0c;從該Pod詳細描述中得知&#xff0c;volume "tls-certs…

內核參數調優

默認的Linux內核參數考慮的是最通用場景&#xff0c;不符合用于支持高并發訪問的Web服務器的定義&#xff0c;根據業務特點來進行調整&#xff0c;當Nginx作為靜態web內容服務器、反向代理或者提供壓縮服務器的服務器時&#xff0c;內核參數的調整都是不同的&#xff0c;此處針…

【牛客】SQL131 作答試卷得分大于過80的人的用戶等級分布

描述 現有用戶信息表user_info&#xff08;uid用戶ID&#xff0c;nick_name昵稱, achievement成就值, level等級, job職業方向, register_time注冊時間&#xff09;&#xff1a; iduidnick_nameachievementleveljobregister_time11001牛客1號31007算法2020-01-01 10:00:00210…

常見外設學習以及無線通信頻率

常見外設 UART UART&#xff08;Universal Asynchronous Receiver/Transmitter&#xff0c;通用異步收發器&#xff09;是一種異步、串行、全雙工的通信總線。 UART 有3根線&#xff0c;分別是&#xff1a;發送線&#xff08;TX&#xff09;、接收線&#xff08;RX&#xff…

AT24C1024的模擬IIC驅動

AT24C1024是基于IIC的EEPROM&#xff0c;容量為1024/8128k bytes。它的引腳如下&#xff1a; 其中A1,A2為硬件地址引腳 WP為寫保護引腳&#xff0c;一般我們需要讀寫&#xff0c;需要接低電平GND&#xff0c;接高的話則僅允許讀 SDA和SCL則為IIC通信引腳 芯片通信采用IIC&…

02、MongoDB -- MongoDB 的安全配置(創建用戶、設置用戶權限、啟動安全控制、操作數據庫命令演示、mongodb 的幫助系統介紹)

目錄 MongoDB 的安全配置演示前準備&#xff1a;啟動 mongodb 服務器 和 客戶端 &#xff1a;1、啟動單機模式的 mongodb 服務器2、啟動 mongodb 的客戶端 MongoDB 的安全配置啟動演示用到的 mongodb 服務器 和 客戶端啟動單機模式的 mongodb 服務器&#xff1a;啟動 mongodb 的…

【數據結構】19 平衡二叉樹

定義 平衡二叉樹又稱為AVL樹&#xff0c;是具有以下性質的非空搜索樹&#xff1a; 任一結點的左、右子樹均為AVL樹。根節點的左、右子樹高度差的絕對值不超過1. 對于二叉樹的任一結點T&#xff0c;其平衡因子&#xff08;BF&#xff09;定義為BF(T) h L ? h R h_L- h_R hL…

acwing算法提高之搜索--雙向廣搜BFS與A星算法

目錄 1 專題說明2 訓練 1 專題說明 本專題用來記錄使用雙向廣搜BFS和A星算法求解的題目。 2 訓練 題目1&#xff1a;190字串變換 考點&#xff1a;從起點開始搜&#xff0c;從終點開始搜&#xff0c;即雙向廣搜。 C代碼如下&#xff0c; #include <iostream> #incl…

攻防世界-get_post

題目信息 相關知識 -G&#xff1a;表示GET請求&#xff0c;缺省POST -d參數用于發送 POST 請求的數據體 使用-d參數以后&#xff0c;HTTP 請求會自動加上標頭Content-Type : application/x-www-form-urlencoded。并且會自動將請求轉為 POST 方法&#xff0c;因此可以省略-X PO…

使用GPTQ進行4位LLM量化

使用GPTQ進行4位LLM量化 最佳腦量化GPTQ算法步驟1:任意順序洞察步驟2:延遲批量更新第三步:喬爾斯基重塑 用AutoGPTQ量化LLM結論References 權重量化的最新進展使我們能夠在消費級硬件上運行大量大型語言模型&#xff0c;例如在RTX 3090 GPU上運行LLaMA-30B模型。這要歸功于性能…

信息收集2.0版本

內網滲透之信息收集 whois查詢 1.1.1.1. 在線網站查詢 輸入相關的域名即可進行查詢。 &#xff08;1&#xff09;站長之家&#xff1a;whois域名查詢&#xff1a;http://whois.chinaz.com/ &#xff08;2&#xff09;愛站工具網&#xff1a;whois域名查詢&#xff1a;站長…

mysql數據庫操作小寄巧

目錄 json字段查詢時間相關只有日期只有時間又有時間又有日期時間比較時間運算 某字段同的取最新數據&#xff08;軟性的新數據覆蓋舊數據查找&#xff09;sql_modeonly_full_group_by的解決辦法優化思路 json字段查詢 查詢某個json字段&#xff08;xx&#xff09;的某個屬性下…

【考研數學】零基礎備考全年計劃

25考研數學基礎差&#xff0c;一定要重視基礎的復習&#xff01; 基礎不牢&#xff0c;地動山搖&#xff0c;這句話在如今的考研更加貼切 24考研的新形勢&#xff1a; 重基礎、計算量大、反押題 每一個變化對于基礎差的同學都不是好消息。 做過近幾年考研真題的人都會發現…

AI時代編程新寵!如何讓孩子成為未來的編程大師?

文章目錄 一、了解編程的基礎概念二、選擇適合的編程工具三、激發孩子的興趣四、注重基礎能力的培養五、提供實踐機會六、鼓勵孩子與他人合作七、持續支持與鼓勵《信息學奧賽一本通關》本書定位內容簡介作者簡介目錄 隨著科技的迅猛發展&#xff0c;編程已經從一種專業技能轉變…

Java實戰:PO、VO、DAO、BO、DTO與POJO在何處何場景下精準應用?

引言 在Java企業級應用開發中&#xff0c;良好的架構設計和清晰的數據模型劃分是保證代碼可讀性、可維護性和擴展性的基石。本文將深入剖析Java開發中常見的六大對象模型——PO&#xff08;Persistent Object&#xff09;、VO&#xff08;Value Object&#xff09;、DAO&#…

代碼隨想錄第二十五天 78.子集 90.子集II 491.非遞減子序列

LeetCode 78 子集 題目描述 給你一個整數數組 nums &#xff0c;數組中的元素 互不相同 。返回該數組所有可能的子集&#xff08;冪集&#xff09;。 解集 不能 包含重復的子集。你可以按 任意順序 返回解集。 示例 1&#xff1a; 輸入&#xff1a;nums [1,2,3] 輸出&…

24計算機考研 | 渤海大學

渤海大學丨省重點實驗室24年碩士招生&#xff08;調劑&#xff09; 考研調劑招生信息 學校:渤海大學 專業:工學->化學工程與技術->化學工藝 工學->材料科學與工程->材料學 工學->化學工程與技術->應用化學 工學->計算機科學與技術->計算機應用技…

iOS卡頓原因與優化

iOS卡頓原因與優化 1. 卡頓簡介 卡頓&#xff1a; 指用戶在使用過程中出現了一段時間的阻塞&#xff0c;使得用戶在這一段時間內無法進行操作&#xff0c;屏幕上的內容也沒有任何的變化。 卡頓作為App的重要性能指標&#xff0c;不僅影響著用戶體驗&#xff0c;更關系到用戶留…

Maven插件之 maven-dependency-plugin 分析依賴復制文件

目錄 插件簡介使用示例配置依賴&#xff1a;執行 mvn dependency:analyze輸出結果&#xff1a; 結尾 插件簡介 Apache Maven Dependency Plugin是Apache Maven構建工具的一個插件&#xff0c;用于管理項目的依賴項。 該插件提供了一系列目標&#xff08;goals&#xff09;&…