[MQ middleware] rabbitmq -- rabbitmq message mode (2)

mq middleware rabbitmq rabbitmq message

RabbitMQ Message schema

Code and notes related to each mode md Summarized to gitHub Prefecture :



1.RabbitMQ framework

1.1.RabbitMQ The core of

The core concept :
Server: also called Broker , Accept client connections , Realization AMQP Physical services . install rabbitmq-server
Connection: Connect , Application and Broker The Internet connection TCP/IP/ Three handshakes and four waves
Channel: Network channel , Almost all operations are in Channel In the middle of ,Channel It's a channel for reading and writing messages , The client can set up a system for each Channel, Every Channel Represents a conversational task .
Message : news : Data transferred between services and Applications , from Properties and body form ,Properties But retouching the message , For example, the priority of a message , Advanced features like delay ,Body Is the content of the message body .
Virtual Host  Virtual address , For logical isolation , Top level message routing , A virtual host can have several reasons Exhange and Queueu, You can't have the same name in the same virtual host Exchange
Exchange: Switch , Receive a message , Send messages to the bound queue according to the routing key .(== No message storage capability ==)
Bindings:Exchange and Queue Virtual connection between ,binding You can protect multiple routing key.
Routing key: It's a routing rule , Virtual machines can use it to determine how to route a particular message .
Queue: queue : Has become a Message Queue, Message queue , Save messages and forward them to consumers .


1.2.RabbitMQ The overall architecture


  We can see from the architecture diagram that :RabbitMQ The architecture contains :Provider( producer )Exchanges( Switch )Queues( queue )Consumer( consumer ).


1.3.RabbitMQ Operation flow

The producer may package the business data , Then encapsulate it as a message , send out ( AMQP The command corresponding to this action in the protocol is Basic.Publish) To Broker in . Consumers subscribe and receive messages (AMQP The command corresponding to this action in the protocol is Basic.Consume perhaps Basic. Get) , The original data can be obtained through the possible unpacking process , After that, the business processing logic . This business processing logic does not necessarily need to use the same thread as the receiving logic .

Consumer processes can use a thread to receive messages , Put it in memory , For example, use Java Medium BlockingQueue . Business processing logic uses another thread to read data from memory , In this way, the application can be further interpreted , Improve the processing efficiency of the whole application .

The detailed implementation of the underlying communication process , Those who are interested can learn about another blog post , It's very detailed , I will not elaborate too much here :

rabbitMQ Basic concepts

rabbitMQ Operation process


2.RabbitMQ Supported message patterns

Refer to the official website :https://www.rabbitmq.com/getstarted.html

2.1. Simple mode Simple


In simple mode messaging , There's no need to create switches in particular ( Specify the type of switch ), Through the default switch Binding Default exchange, By designation Routing key relation Queues Medium queue name To the corresponding Queue Queue to send messages .

Be careful :

rabbitmq There must be a switch to send messages , If the switch is not created , By default Default_exchange Binding .



Finally to queue1 Send message in , Get the message as follows :


Ack Mode: Message reply mode

Nack message: After receiving the message, do not reply to the message , And consumers don't respond to feedback to producers ,Queue The message will still be saved in ;

Ack message: Receive the message and reply , At this point, the producer receives a reply from the consumer , take Queue In this message consumption .


2.2. Work queue mode WorkQueues

The work queue mode is also specified as the default default Default switch For many Queue Consumers send messages ,

There are two main patterns :
1、 Polling mode distribution : One for each consumer , To distribute equally , average , It will not reduce the sending of messages because of which consumer's faster internal processing speed ;
2、 Fair distribution : Fair distribution according to the consumption ability of consumers , Faster processing, more processing , Slow processing, less processing ; distribution according to work ;


The difference in the actual code is that the response mode of fair distribution must be set to manual response , And polling mode is set to auto answer :

// According to the actual scene settings : Depending on the processing speed of the service 、 Disk space, etc , by 1 It means to consume only at a time 1 strip 
finalChannel.basicQos(1); // Fair distribution requires manual response finalChannel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);


 2.3. Publish subscribe mode  Publish/Subscribe

Publish subscribe mode uses fanout type The switch binds every consumer Queue queue , It's equivalent to broadcasting , Subscribe to the switch ( channel ) Of Queue Will receive a message from the producer sent through the switch .

And in the process, consumers Queues Must be with fanout_exchange One to one binding .




 2.4. Routing mode Routing

The switch type of routing mode is :direct type

The routing pattern is very similar to the publish subscribe pattern , It's also very easy to understand . It just adds routing to the publish subscribe model key, Through the corresponding route key With the corresponding Queue Binding , This Queue I was key It's the only sign .

And then when the switch sends the message again , Will be bound to the switch Queue Specify the corresponding route in key Send , Only corresponding routes key For sending key One to one correspondence to receive messages .


Test to Routing key by error Of queue queue ( Queues are bound with routes direct_exchange):






 2.5. The theme Topic Pattern


Theme mode topics It's very similar to routing patterns , It's just that we've added support to routing Fuzzy matching Routing key In the form of . 

Switch type selection for theme mode must be topic type .

Matching rules :

#: Express 0 One or more , The value range is [0,+ infinity )

*: Express 1 individual , The value is 1, That is, there must be a .


Test queue Routing key as follows , It's all tied up topic Topic type switches :



You can see , Only queue1 And queue2 Received the corresponding message :



2.6. Parameter mode

Parameter mode exchange The type is :headers.

The parameter pattern will match the route according to the corresponding Queue Carry the parameter information to match , and exchange According to arguement Parameters are sent one-to-one :

Arguments Information identification , Test selection x=1 Send to the queue of :




You can see queue1 We've received a message from :



3. summary

RabbitMQ Supported message patterns :

Simple mode Simple

  • Switch type :default
  • characteristic : One to one distribution through the default switch

Working mode Work

  •   Switch type :default
  • characteristic : Poll distribution : To distribute equally ; Fair distribution : distribution according to work ;

Publish subscribe mode

  • Switch type :fanout
  • characteristic : Subscribed Channels , Each queue is bound to the switch , You can get messages

Routing mode

  • Switch type :direct
  • characteristic : Yes route key As identification , Send a message via routing key One on one transmission , There will be a loss in actual performance

Theme mode

  • Switch type :topic
  • characteristic : stay route key On this basis, fuzzy matching is added ,#:0 One or more ;*: There has to be one

Parameter mode

  • Switch type :headers
  • characteristic : Portability parameter Arguments, The actual sending matches the queue with corresponding parameters to send


本文为[If you don't know who you are, you can concentrate on practicin]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云