Spring Boot(五)——RabbitMQ
来源:互联网 发布:windows域服务器管理 编辑:程序博客网 时间:2024/05/17 04:02
Spring Boot 学习笔记:
Spring Boot(一)——入门环境搭建 Spring Boot(二)——MyBatis Spring Boot(三)——Redis
Spring Boot(四)——Mongodb Spring Boot(五)——RabbitMQ Spring Boot(六)——Spring Data JpaGitHub地址:
https://github.com/lyhkmm/SpringBoot
一、RabbitMq的介绍
RabbitMq的基本原理可以自行上网查阅,或者点击传送门:RabbitMQ的基本原理。
二、使用配置
1、老规矩,先在pom.xml中添加相关依赖:
<!--消息队列模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
spring.application.name=spirng-boot-rabbitmqspring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest
端口、用户名和密码都是默认的,根据自己的实际情况配置,rabbitmq的安装教程网上很多了,这里暂时不介绍,以后有时间补上。
package com.lyh.demo;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @Author:linyuanhuang * @Description:队列配置,队列的名称,发送者和接受者的名称必须一致,否则接收不到消息 * @Date:2017/12/11 14:50 */@Configurationpublic class RabbitMqConfig { @Bean public Queue Queue1() { return new Queue("lyhTest1"); }}
package com.lyh.demo.controller;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;@RestControllerpublic class SendController { @Autowired private AmqpTemplate amqpTemplate; @RequestMapping("/send") public String send(){ String content="Date:"+new Date(); amqpTemplate.convertAndSend("lyhTest1",content); return content; }}
package com.lyh.demo.Receiver;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Component@RabbitListener(queues = "lyhTest1")public class Receiver1 { @RabbitHandler public void receiver(String msg){ System.out.println("Test1 receiver1:"+msg); }}
浏览器访问地址:http://localhost:8080/send,如下图:
终端输出接受的内容:
查看RabbitMQ的Web客户端http://localhost:15672,帐号密码和配置文件一样,如下图:
三、一对多的使用配置
1、一对多,一个发送者发送消息,多个接受者接受同一个消息,添加新的接收者Receiver2:
package com.lyh.demo.Receiver;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Component@RabbitListener(queues = "lyhTest1")public class Receiver2 { @RabbitHandler public void receiver(String msg){ System.out.println("Test1 receiver2:"+msg); }}
2、发送者循环发送10个消息,在SendController添加一对多发送方法:
@RequestMapping("/multiSend") public String multiSend(){ StringBuilder times=new StringBuilder(); for(int i=0;i<10;i++){ long time=System.nanoTime(); amqpTemplate.convertAndSend("lyhTest1","第"+i+"次发送的时间:"+time); times.append(time+"<br>"); } return times.toString(); }
4、终端输出接收数据:
Test1 receiver2:第1次发送的时间:25953655163399Test1 receiver1:第0次发送的时间:25953641137213Test1 receiver2:第2次发送的时间:25953655403734Test1 receiver1:第3次发送的时间:25953655591967Test1 receiver1:第5次发送的时间:25953655949458Test1 receiver2:第4次发送的时间:25953655772971Test1 receiver1:第6次发送的时间:25953656111790Test1 receiver1:第8次发送的时间:25953656492471Test1 receiver1:第9次发送的时间:25953656687330Test1 receiver2:第7次发送的时间:25953656277133
四、多对多的使用配置
@Configurationpublic class RabbitMqConfig { @Bean public Queue Queue1() { return new Queue("lyhTest1"); } @Bean public Queue Queue2() { return new Queue("lyhTest2"); }}
@Component@RabbitListener(queues = "lyhTest2")//这里的lyhTest2是多对多,如果要测试一对多改成lyhTest1public class Receiver2 { @RabbitHandler public void receiver(String msg){ System.out.println("Test2 receiver2:"+msg); }}
@RequestMapping("/multi2MultiSend") public String mutil2MutilSend(){ StringBuilder times=new StringBuilder(); for(int i=0;i<10;i++){ long time=System.nanoTime(); amqpTemplate.convertAndSend("lyhTest1","第"+i+"次发送的时间:"+time); amqpTemplate.convertAndSend("lyhTest2","第"+i+"次发送的时间:"+time); times.append(time+"<br>"); } return times.toString(); }
4、测试,浏览器访问:http://localhost:8080/multi2MultiSend,如下图:
5、终端输出接收数据:
Test1 receiver1:第0次发送的时间:27607875773748Test2 receiver2:第0次发送的时间:27607875773748Test2 receiver2:第1次发送的时间:27607882272138Test2 receiver2:第2次发送的时间:27607882429049Test1 receiver1:第1次发送的时间:27607882272138Test2 receiver2:第3次发送的时间:27607882594693Test1 receiver1:第2次发送的时间:27607882429049Test2 receiver2:第4次发送的时间:27607882897371Test1 receiver1:第3次发送的时间:27607882594693Test2 receiver2:第5次发送的时间:27607883163005Test1 receiver1:第4次发送的时间:27607882897371Test2 receiver2:第6次发送的时间:27607883319916Test2 receiver2:第7次发送的时间:27607883489777Test1 receiver1:第5次发送的时间:27607883163005Test1 receiver1:第6次发送的时间:27607883319916Test2 receiver2:第8次发送的时间:27607883957798Test2 receiver2:第9次发送的时间:27607884305953Test1 receiver1:第7次发送的时间:27607883489777Test1 receiver1:第8次发送的时间:27607883957798Test1 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 */@Configurationpublic class { //只接一个topic final static String message = "topic.message"; //接收多个topic final static String messages = "topic.messages"; @Bean public Queue queueMessage() { return new Queue(RabbitMqTopicConfig.message); } @Bean public Queue queueMessages() { return new Queue(RabbitMqTopicConfig.messages); } @Bean TopicExchange exchange() { return new TopicExchange("exchange"); } @Bean Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { return BindingBuilder.bind(queueMessage).to(exchange).with("topic.single"); } @Bean Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) { //这里的#表示零个或多个词。 return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); }}
@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; }
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 { @RabbitHandler public void process(String msg) { System.out.println("TopicReceiver1:" + msg); }}
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 { @RabbitHandler public void process(String msg) { System.out.println("TopicReceiver2 :" + msg); }}
发送者说 : my topic 1TopicReceiver1:my topic 1TopicReceiver2 :my topic 1浏览器访问http://localhost:8080/topicSend2,终端输出:
发送者说 : my topic 2TopicReceiver2 :my topic 2
这里的Topic Exchange 转发消息主要是根据通配符,队列topic.message只能匹配topic.single的路由。而topic.messages匹配路由规则是topic.#,所以它可以匹配topic.开头的全部路由。而topic.#发送的消息也只能是topic.#的接受者才能接收。
- Spring Boot(五)——RabbitMQ
- spring-boot 集成 rabbitmq
- Spring boot集成RabbitMQ
- spring boot集成rabbitmq
- spring boot+RabbitMQ
- SPRING BOOT+ELASTICSEARCH+RABBITMQ
- Spring Boot RabbitMQ 集成
- Spring Boot RabbitMQ Exchange
- spring-boot 集成 rabbitmq
- Spring Boot 集成RabbitMQ
- Spring Boot 集成RabbitMQ
- rabbitMQ配置spring boot
- spring-boot 集成 rabbitmq
- Spring Boot整合RabbitMQ
- spring boot rabbitmq学习
- Spring Boot中使用RabbitMQ
- Spring Boot中使用RabbitMQ
- Spring Boot中使用RabbitMQ
- apache ab 压力测试
- struts2原理模拟
- 设计模式学习--工厂模式
- tomcat 设置windows服务
- 忍不住为游戏音乐疯狂打Call!
- Spring Boot(五)——RabbitMQ
- Ubuntu 软件包管理工具(以kali为栗)
- postgresql杀进程
- 水仙花问题
- JOIN语法解析
- 移动端适配方案
- pyspark rdd def partitionBy自定义partitionFunc
- 十大Python机器学习开源项目
- 如何站在巨人的肩膀上,将自己的产品赋予AI的能力?百度UNIT