转载自https://blog.csdn.net/lyhkmm/article/details/78772919

RabbitMq的介绍

RabbitMq的基本原理可以自行上网查阅,或者点击传送门:RabbitMQ的基本原理。

使用配置

1、老规矩,先在pom.xml中添加相关依赖:

 
  1. <!--消息队列模块-->

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-amqp</artifactId>

  5. </dependency>

2、在application.properties添加rabbitmq的相关信息:

 
  1. spring.application.name=spirng-boot-rabbitmq

  2. spring.rabbitmq.host=127.0.0.1

  3. spring.rabbitmq.port=5672

  4. spring.rabbitmq.username=guest

  5. spring.rabbitmq.password=guest

端口、用户名和密码都是默认的,根据自己的实际情况配置,rabbitmq的安装教程网上很多了,这里暂时不介绍,以后有时间补上。

3、配置队列:

 
  1. package com.lyh.demo;

  2. import org.springframework.amqp.core.Queue;

  3. import org.springframework.context.annotation.Bean;

  4. import org.springframework.context.annotation.Configuration;

  5. /**

  6. * @Author:linyuanhuang

  7. * @Description:队列配置,队列的名称,发送者和接受者的名称必须一致,否则接收不到消息

  8. * @Date:2017/12/11 14:50

  9. */

  10. @Configuration

  11. public class RabbitMqConfig {

  12. @Bean

  13. public Queue Queue1() {

  14. return new Queue("lyhTest1");

  15. }

  16. }

4、发送者通过Controller类发送消息:

 
  1. package com.lyh.demo.controller;

  2. import org.springframework.amqp.core.AmqpTemplate;

  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RestController;

  6. import java.util.Date;

  7. @RestController

  8. public class SendController {

  9. @Autowired

  10. private AmqpTemplate amqpTemplate;

  11. @RequestMapping("/send")

  12. public String send(){

  13. String content="Date:"+new Date();

  14. amqpTemplate.convertAndSend("lyhTest1",content);

  15. return content;

  16. }

  17. }

5、创建接受者Receiver1,新建类:

 
  1. package com.lyh.demo.Receiver;

  2. import org.springframework.amqp.rabbit.annotation.RabbitHandler;

  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;

  4. import org.springframework.stereotype.Component;

  5. @Component

  6. @RabbitListener(queues = "lyhTest1")

  7. public class Receiver1 {

  8. @RabbitHandler

  9. public void receiver(String msg){

  10. System.out.println("Test1 receiver1:"+msg);

  11. }

  12. }

6、测试

浏览器访问地址:http://localhost:8080/send,如下图:

终端输出接受的内容:

查看RabbitMQ的Web客户端http://localhost:15672,需要自己安装RabbitMQ的客户端,可以自己上网查阅相关教程。帐号密码和配置文件一样,如下图:可以在列表里看到之前创建的队列。

一对多的使用配置

1、一对多,一个发送者发送消息,多个接受者接受同一个消息,添加新的接收者Receiver2:

 
  1. package com.lyh.demo.Receiver;

  2. import org.springframework.amqp.rabbit.annotation.RabbitHandler;

  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;

  4. import org.springframework.stereotype.Component;

  5. @Component

  6. @RabbitListener(queues = "lyhTest1")

  7. public class Receiver2 {

  8. @RabbitHandler

  9. public void receiver(String msg){

  10. System.out.println("Test1 receiver2:"+msg);

  11. }

  12. }

2、发送者循环发送10个消息,在SendController添加一对多发送方法:

 
  1. @RequestMapping("/multiSend")

  2. public String multiSend(){

  3. StringBuilder times=new StringBuilder();

  4. for(int i=0;i<10;i++){

  5. long time=System.nanoTime();

  6. amqpTemplate.convertAndSend("lyhTest1","第"+i+"次发送的时间:"+time);

  7. times.append(time+"<br>");

  8. }

  9. return times.toString();

  10. }

3、测试,浏览器访问http://localhost:8080/multiSend,如下图:

4、终端输出接收数据:

Test1 receiver2:第1次发送的时间:25953655163399
Test1 receiver1:第0次发送的时间:25953641137213
Test1 receiver2:第2次发送的时间:25953655403734
Test1 receiver1:第3次发送的时间:25953655591967
Test1 receiver1:第5次发送的时间:25953655949458
Test1 receiver2:第4次发送的时间:25953655772971
Test1 receiver1:第6次发送的时间:25953656111790
Test1 receiver1:第8次发送的时间:25953656492471
Test1 receiver1:第9次发送的时间:25953656687330
Test1 receiver2:第7次发送的时间:25953656277133

可以看到发送者发送一个消息被多个接收者接收,注意这里的消息只能被消费一次

多对多的使用配置

1、在配置类RabbbitMqConfig添加新的队列名lyhTest2:

 
  1. @Configuration

  2. public class RabbitMqConfig {

  3. @Bean

  4. public Queue Queue1() {

  5. return new Queue("lyhTest1");

  6. }

  7. @Bean

  8. public Queue Queue2() {

  9. return new Queue("lyhTest2");

  10. }

  11. }

2、修改Receiver2接收队列名为lyhTest2:

 
  1. @Component

  2. @RabbitListener(queues = "lyhTest2")

  3. //这里的lyhTest2是多对多,如果要测试一对多改成lyhTest1

  4. public class Receiver2 {

  5. @RabbitHandler

  6. public void receiver(String msg){

  7. System.out.println("Test2 receiver2:"+msg);

  8. }

  9. }

3、在SendController添加多对多发送消息的方法:

 
  1. @RequestMapping("/multi2MultiSend")

  2. public String mutil2MutilSend(){

  3. StringBuilder times=new StringBuilder();

  4. for(int i=0;i<10;i++){

  5. long time=System.nanoTime();

  6. amqpTemplate.convertAndSend("lyhTest1","第"+i+"次发送的时间:"+time);

  7. amqpTemplate.convertAndSend("lyhTest2","第"+i+"次发送的时间:"+time);

  8. times.append(time+"<br>");

  9. }

  10. return times.toString();

  11. }

4、测试,浏览器访问:http://localhost:8080/multi2MultiSend,如下图:

5、终端输出接收数据:

Test1 receiver1:第0次发送的时间:27607875773748
Test2 receiver2:第0次发送的时间:27607875773748
Test2 receiver2:第1次发送的时间:27607882272138
Test2 receiver2:第2次发送的时间:27607882429049
Test1 receiver1:第1次发送的时间:27607882272138
Test2 receiver2:第3次发送的时间:27607882594693
Test1 receiver1:第2次发送的时间:27607882429049
Test2 receiver2:第4次发送的时间:27607882897371
Test1 receiver1:第3次发送的时间:27607882594693
Test2 receiver2:第5次发送的时间:27607883163005
Test1 receiver1:第4次发送的时间:27607882897371
Test2 receiver2:第6次发送的时间:27607883319916
Test2 receiver2:第7次发送的时间:27607883489777
Test1 receiver1:第5次发送的时间:27607883163005
Test1 receiver1:第6次发送的时间:27607883319916
Test2 receiver2:第8次发送的时间:27607883957798
Test2 receiver2:第9次发送的时间:27607884305953
Test1 receiver1:第7次发送的时间:27607883489777
Test1 receiver1:第8次发送的时间:27607883957798
Test1 receiver1:第9次发送的时间:27607884305953

可以看到不同的接收者接收不同发送者发送的消息,消息也可以是实体对象,这里就不做演示。

Topic Exchange的使用配置

Topic Exchange是RabbitMQ中最灵活的一种方式,它能够根据routing_key自由的绑定不同的队列,可以适用绝大部分的项目需求

1、新建RabbitMqTopicConfig配置类:


package com.lyh.demo;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:linyuanhuang* @Description:Topic Exchange配置类* @Date:2017/12/11 17:13*/
@Configuration
public class {//只接一个topicfinal static String message = "topic.message";//接收多个topicfinal static String messages = "topic.messages";@Beanpublic Queue queueMessage() {return new Queue(RabbitMqTopicConfig.message);}@Beanpublic Queue queueMessages() {return new Queue(RabbitMqTopicConfig.messages);}@BeanTopicExchange exchange() {return new TopicExchange("exchange");}@BeanBinding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");}@BeanBinding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {//这里的#表示零个或多个词。return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");}

2、在SendController添加发送消息方法:


@RequestMapping("/topicSend1")public String  topicSend1() {String context = "my topic 1";System.out.println("发送者说 : " + context);this.amqpTemplate.convertAndSend("exchange", "topic.message", context);return context;}@RequestMapping("/topicSend2")public String topicSend2() {String context = "my topic 2";System.out.println("发送者说 : " + context);this.amqpTemplate.convertAndSend("exchange", "topic.messages", context);return  context;

3、创建接收者的方法TopicReceiver1和TopicReceiver2:

TopicReceiver1:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topic.message")
public class TopicReceiver1 {@RabbitHandlerpublic void process(String msg) {System.out.println("TopicReceiver1:" + msg);}}
TopicReceiver2:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topic.messages")
public class TopicReceiver2 {@RabbitHandlerpublic void process(String msg) {System.out.println("TopicReceiver2 :" + msg);}

4、测试:

浏览器访问http://localhost:8080/topicSend1,终端输出:

 
发送者说 : my topic 1
TopicReceiver1:my topic 1
TopicReceiver2 :my topic 1

浏览器访问http://localhost:8080/topicSend2,终端输出:


发送者说 : my topic 2
TopicReceiver2 :my topic 2
5、总结:

这里的Topic Exchange 转发消息主要是根据通配符,队列topic.message只能匹配topic.message的路由。而topic.messages匹配路由规则是topic.#,所以它可以匹配topic.开头的全部路由。而topic.#发送的消息也只能是topic.#的接受者才能接收。

GitHub地址:https://github.com/lyhkmm/spring-boot-examples/tree/master/spring-boot-rabbitmq

Spring Boot——RabbitMQ相关推荐

  1. Spring boot Rabbitmq 示例

    Spring boot Rabbitmq 示例 简介     Spring boot RabbitMQ 简单程序示例 编写详情 RabbitMQ docker     避免麻烦,直接使用docker启 ...

  2. spring boot + rabbitMq整合之死信队列(DL)

    rabbit mq 死信队列 什么是死信队列? DL-Dead Letter 死信队列 死信,在官网中对应的单词为"Dead Letter",可以看出翻译确实非常的简单粗暴.那么死 ...

  3. Spring boot + RabbitMQ延迟队列实战

    一.背景 延时队列顾名思义,即放置在该队列里面的消息是不需要立即消费的,而是等待一段时间之后取出消费. 那么,为什么需要延迟消费呢?我们来看以下的场景: 订单业务: 在电商/点餐中,都有下单后 30 ...

  4. spring boot rabbitmq 延时消费的简单实现

    目录 实现一:TTL 设置队列过期时间实现延时消费 设置消息过期时间实现延时消费 实现二:插件实现 公司最近需要用到rabbitmq,考虑到业务需求,后期可能需要用到mq延时消费机制.工作一年,对很多 ...

  5. Spring Boot RabbitMQ 详解

    1.美图 2.RabbitMQ 介绍 AMQP(Advanced Message Queuing Protocol,⾼级消息队列协议)是应用层协议的一个开放标准,为⾯向消息的中间件设计.消息中间件主要 ...

  6. spring boot整合RabbitMQ —— 十分钟急速上手

    安装运行rabbitmq 1.docker安装rabbitmq: docker run -it --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq ...

  7. 面试那点小事,你从未见过的spring boot面试集锦(附详细答案)

    一, 什么是spring boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问页面https://spring.io/projects,我们将看到所有在应用程序中使用的不同功能的 ...

  8. 精通spring——深入java ee开发核心技术 pdf_2019精通Spring Boot 42讲 高清pdf完整版

    <精通springboot42讲价值99元入门到实教程>2019年最新spring boot教程,共计42讲从入门到精通,真正的实战教程. 课程内容和技术栈都会使⽤最新稳定版本,课程数量也 ...

  9. RabbitMQ使用及与spring boot整合

    1.MQ 消息队列(Message Queue,简称MQ)--应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...

最新文章

  1. 计算机思维采用抽象和分解,凤凰机器人----什么是计算思维?凤凰机器人的编程课中是如何体现它的?...
  2. !! 机器学习常用工具
  3. Android开发实践:为什么要继承onMeasure()
  4. 亿级流量场景下如何为HTTP接口限流?看完我懂了!!
  5. python:opencv 二值化处理
  6. php mysql主从延迟_如何解决主从数据库同步延迟问题?php连接 mysql 数据库如何添加一个公共的配置文件50...
  7. h3csyslog_H3C Syslog简单配置
  8. ConcurrentHashMap源码解读,java大厂面试攻略
  9. [凯立德]2014秋季版C1204-C7K05-3321J0L(SP2)WinCE版
  10. 投篮机投篮有技巧吗_卡梅伦·约翰逊:投篮高效,跑位积极,会是太阳队外线新答案吗?...
  11. 126. PHP 加密
  12. Aruba7010 默认密码_收藏 | 各大品牌的变频器默认密码、万能密码、超级密码汇总...
  13. 学习《华为基本法》(11):项目管理与审计制度
  14. C语言 分解质因数。例如:输入90,打印出90=2*3*3*5。
  15. 搜狗输入法候选窗口不跟随光标
  16. 无线访问域服务器,管理用户的RADIUS服务器认证无线局域网控制器WLC配置-Cisco.PDF...
  17. 三星s8android版本,三星Galaxy S8的手机系统是什么
  18. 工业物联网在制造业中有哪些用途?以数网星工业物联网平台为例
  19. C. Equalize
  20. 1009 说反话 (20分)快速易理解C++版

热门文章

  1. MFC中混合使用Duilib制作界面
  2. cocos2d-x游戏实例(9)-A星算法(5)
  3. ARP欺骗:先认识再防御
  4. 《MySQL实战45讲》实践篇 9-15讲 学习笔记
  5. Java 8 Lambda 表达式被编译成了什么?
  6. 无人值守的自动 dump(二)
  7. 高精度加法(非负)和大数阶乘及和汽水问题
  8. 【今晚七点半】:爱奇艺DRM探索之路
  9. 音视频技术开发周刊 | 159
  10. 唐敏豪:我给MSU评测打9分