SpringBootRabbitMQ七种工作模式入门

更新时间:2023-07-01 11:59:05 阅读: 评论:0

SpringBootRabbitMQ七种⼯作模式⼊门
RabbitMQ ⼯作模式
应急演练计划表
简单模式
电视剧上映时间表
简单:⼀个⽣产者、⼀个队列和⼀个消费者,⽣产者发送消息⾄队列,消费者监听队列并消费消息
Work 模式
Work:⼀个⽣产者、⼀个队列和多个消费者,⽣产者发送消息⾄队列,多个消费者监听同⼀队列消费消息
发布/订阅模式
发布/订阅:publish/subscribe 模式包含⼀个⽣产者、⼀个交换机、多个队列及多个消费者,交换机(Exchange)和队列直接绑定,⽣产者通过交换机(Exchange)将消息存储在与交换机绑定的队列中,消费者监听队列并进⾏消费
路由模式
路由:routing 模式可以根据 routing key 将消息发送给指定队列,交换机(Exchange)和队列通过routing key 进⾏绑定,⽣产者通过交换机(Exchange)和 routing key 将消息精准发送⾄队列,消费者监听队列并消费消息
主题模式
主题:Topics 模式在路由模式的基础上⽀持通配符操作,交换机会根据通配符将消息存储在匹配成功的队列中,消费者监听队列并进⾏消费
Header 模式
Header:header 模式取消了 routing key,⽽是使⽤ header 中的 key/value 键值对来进⾏匹配,匹配成功后消息会通过交换机发送给队列,消息者才能获取到消息并消费
RPC 模式
RPC:RPC 模式主要针对需要获取消费者处理结果的情况,通常是⽣产者将消息发送给了消费者,消费者接收到消息并进⾏消费后返回给⽣产者处理结果
SpringBoot 集成 RabbitMQ
⾸先创建⼀个SpringBoot 项⽬,l ⽂件加⼊以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置⽂件修改,加⼊以下 RabbitMQ 配置
rver:
port:8888  # 设置端⼝号
spring:
语言基础知识rabbitmq:
host: 127.0.0.1  # 设置RabbitMQ所在主机port:5672      # 设置RabbitMQ服务端⼝urname: guest  # 设置RabbitMQ⽤户名password: guest  # 设置RabbitMQ密码
新增公共常量类
public interface RabbitConstant {
/**
* 简单模式
*/
String SIMPLE_QUEUE_NAME ="simple_queue";
/**
蒸美味加盟* work 模式
*/
String WORK_QUEUE_NAME ="work_queue";
/**
* 发布/订阅(publish/subscribe)模式
*/
String PUBLISH_SUBSCRIBE_EXCHANGE_NAME ="publish_subscribe_exchange";
String PUBLISH_SUBSCRIBE_FIRST_QUEUE_NAME ="publish_subscribe_first_queue";
String PUBLISH_SUBSCRIBE_SECOND_QUEUE_NAME ="publish_subscribe_cond_queue";
/**
* 路由(routing)模式
*/
String ROUTING_EXCHANGE_NAME ="routing_exchange";
String ROUTING_FIRST_QUEUE_NAME ="routing_first_queue";
String ROUTING_SECOND_QUEUE_NAME ="routing_cond_queue";
String ROUTING_THIRD_QUEUE_NAME ="routing_third_queue";
String ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME ="routing_first_queue_routing_key";
String ROUTING_SECOND_QUEUE_ROUTING_KEY_NAME ="routing_cond_queue_routing_key";    String ROUTING_THIRD_QUEUE_ROUTING_KEY_NAME ="routing_third_queue_routing_key";
/**
* 主题(topics)模式
*/
String TOPICS_EXCHANGE_NAME ="topics_exchange";
String TOPICS_FIRST_QUEUE_NAME ="topics_first_queue";
String TOPICS_SECOND_QUEUE_NAME ="topics_cond_queue";
String TOPICS_THIRD_QUEUE_NAME ="topics_third_queue";
String TOPICS_FIRST_QUEUE_ROUTING_KEY ="uting.key";
String TOPICS_SECOND_QUEUE_ROUTING_KEY ="uting.key";
String TOPICS_THIRD_QUEUE_ROUTING_KEY ="uting.key";
String TOPICS_ROUTING_KEY_FIRST_WILDCARD ="#.first.#";
String TOPICS_ROUTING_KEY_SECOND_WILDCARD ="*.cond.#";
String TOPICS_ROUTING_KEY_THRID_WILDCARD ="*.third.*";
/
**
贝希斯敦铭文* header 模式
*/
String HEADER_EXCHANGE_NAME ="header_exchange";
String HEADER_FIRST_QUEUE_NAME ="header_first_queue";
String HEADER_SECOND_QUEUE_NAME ="header_cond_queue";
/**
* rpc 模式
*/
String RPC_QUEUE_NAME ="rpc_queue";
}
新增 Controller 请求类(⽤于验证结果,可最后新增)
stant.RabbitConstant;
import org.Message;
import org.MessageProperties;
import org.springframework.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.nio.chart.StandardCharts;
@RestController
public class RabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping(value ="/simple")
public void simple(){
}
@GetMapping(value ="/work")
public void work(){
}
@GetMapping(value ="/pubsub")
public void pubsub(){
}
@GetMapping(value ="/routing")
房屋续租合同范本public void routing(){
// 给第⼀个队列发送消息
}
@GetMapping(value ="/topics")
public void topics(){
// 给第⼀个队列发送消息,此时队列能接受到消息,因为队列通配符为 #.first.#,⽽ routing_key 为 uting.key,匹配成功
// 给第⼆个队列发送消息,此时队列也能接受到消息,因为队列通配符为 *.cond.#,⽽ routing_key 为 uting.key,匹配成功
司空曙
// 给第三个队列发送消息,此时队列⽆法接受到消息,因为队列通配符为 *.third.*,⽽ routing_key 为 uting.key,匹配失败
}
@GetMapping(value ="/header")
public void header(){
// 这条消息应该能被两个队列都接收到,第⼀个队列 all 匹配成功,第⼆个队列 hello-value any匹配成功
MessageProperties messageProperties =new MessageProperties();
messageProperties.tHeader("matchAll","YES");
messageProperties.tHeader("hello","world");
Message message =new Message("header first hello".getBytes(StandardCharts.UTF_8), messageProperties);
// 这条消息应该只被第⼆个队列接受,第⼀个队列 all 匹配失败,第⼆个队列 matchAll-NO any匹配成功
MessageProperties messagePropertiesSecond =new MessageProperties();
messagePropertiesSecond.tHeader("matchAll","NO");
Message messageSecond =new Message("header cond hello".getBytes(StandardCharts.UTF_8), messagePropertiesSecond);
}
@GetMapping(value ="/rpc")
驾考宝典摩托车
public void rpc(){
Object responMsg = vertSendAndReceive(RabbitConstant.RPC_QUEUE_NAME,"rpc hello!");
System.out.println("rabbit rpc respon message: "+ responMsg);

本文发布于:2023-07-01 11:59:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1072090.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:队列   消息   模式   消费者   设置   新增   产者
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图