Spring集成RabbitMQ

devtools/2024/11/27 23:30:05/

Spring集成RabbitMQ

官网:https://spring.io/projects/spring-amqp

创建聚合项目

在这里插入图片描述

父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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>provider</module><module>consumer</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.yanghuisen</groupId><artifactId>spring-rabbitmq</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-rabbitmq</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

生产者

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>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>provider</artifactId><name>provider</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></project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author admin* @version 1.0* @date 2020/5/29 22:28* @Description TODO*/
@Configuration
public class RabbitMQConfig {/*** 申明队列* @return*/@Beanpublic Queue queue(){return new Queue("topic");}/*** 申明交换机* @return*/@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}/*** 将队列绑定到交换机上* @return*/@Beanpublic Binding binding(){return BindingBuilder.bind(queue()).to(topicExchange()).with("*.msg.#");}}
package cn.yanghuisen;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:33* @Description TODO*/
@Component
public class Send {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(){String message = "Hello World";/*1、交换机参数2、路由key3、消息内容*/rabbitTemplate.convertAndSend("topicExchange","topic.msg",message);System.out.println("发送消息:"+message);}
}

消费者

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>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>consumer</artifactId><name>consumer</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></build>
</project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App 
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** @author admin* @version 1.0* @date 2020/5/29 22:36* @Description TODO*/
@Component
@RabbitListener(queues = "topic")   // 监听队列
public class Consumer {// 接收消息的处理方法@RabbitHandlerpublic void recv(String message){System.out.println("接受消息:"+message);}
}

测试

package cn.yanghuisen;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:54* @Description TODO*/
@SpringBootTest
public class TestSend {@Resourceprivate Send send;@Testpublic void testSend(){send.send();}}

http://www.ppmy.cn/devtools/137511.html

相关文章

BERT简单理解;双向编码器优势

目录 BERT简单理解 一、BERT模型简单理解 二、BERT模型使用举例 三、BERT模型的优势 双向编码器优势 BERT简单理解 (Bidirectional Encoder Representations from Transformers)模型是一种预训练的自然语言处理(NLP)模型,由Google于2018年推出。以下是对BERT模型的简…

Java爬虫:数据采集的强大工具

引言 在信息爆炸的今天&#xff0c;数据已成为企业决策的重要依据。无论是市场趋势分析、用户行为研究还是竞争对手监控&#xff0c;都离不开对海量数据的收集和分析。Java作为一种成熟且功能强大的编程语言&#xff0c;其在数据采集领域——尤其是爬虫技术的应用——展现出了…

JVM相关知识

Java中的JVM&#xff08;Java Virtual Machine&#xff0c;Java虚拟机&#xff09;是一个抽象的计算机&#xff0c;它提供了一个运行时环境来执行Java字节码。JVM的结构清晰且复杂&#xff0c;主要包括以下几个关键组件&#xff1a; 1. 类加载器&#xff08;Class Loader&…

Redis主从架构

Redis&#xff08;Remote Dictionary Server&#xff09;是一个开源的、高性能的键值对存储系统&#xff0c;广泛应用于缓存、消息队列、实时分析等场景。为了提高系统的可用性、可靠性和读写性能&#xff0c;Redis提供了主从复制&#xff08;Master-Slave Replication&#xf…

tomcat 文件上传 (CVE-2017-12615)

目录 1、漏洞描述 2、访问ip&#xff1a;port 3、漏洞利用 4、Exploit 5、修复建议 1、漏洞描述 Tomcat 是一个小型的轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多的场合下被普遍使用&#xff0c;是开发和调试JSP 程序的首选。 攻击者将有可能可通过…

Java设计模式笔记(一)

Java设计模式笔记&#xff08;一&#xff09; &#xff08;23种设计模式由于篇幅较大分为两篇展示&#xff09; 一、设计模式介绍 1、设计模式的目的 让程序具有更好的&#xff1a; 代码重用性可读性可扩展性可靠性高内聚&#xff0c;低耦合 2、设计模式的七大原则 单一职…

视频变音软件哪个好用?5款视频变音软件推荐

在视频制作过程中&#xff0c;变音功能可以为视频增添趣味性和多样性。无论你是想制作搞笑视频、配音动画&#xff0c;还是进行声音伪装&#xff0c;一款好用的视频变音软件都是必不可少的工具。那么&#xff0c;视频变音软件哪个好用呢&#xff1f;本文将为你介绍几款热门的视…

我们来学mysql -- EXPLAIN之ref(原理篇)

EXPLAIN之ref 题记**ref** 题记 书接上文《 EXPLAIN之type》2024美国大选已定&#xff0c;川普剑登上铁王座&#xff0c;在此过程中出谋划策的幕僚很重要&#xff0c;是他们决定了最终的执行计划在《查询成本之索引选择》中提到&#xff0c;explain的输出&#xff0c;就是优化…