编程知识 cdmana.com

The first-line Internet architect's design idea interprets the open source framework and shares my Tencent Java interview experience

Basic knowledge of

RabbitMQ Is an open source message broker and queue server , It is used to share data between different applications through common protocol , It's using Erlang Written in language , And based on AMQP Agreed ;

RabbitMQ Reasons for high performance

  • Erlang The language has excellent performance in the interaction of switches (Erlang The language originally lies in the architecture pattern of switch field , This makes RabbitMQ stay Broker The performance of data interaction between them is excellent )
  • Erlang Have and original Socket The same delay

AMQP agreement

What is? AMQP Advanced message queue protocol

AMQP(Advanced Message Queueing Protocol) Definition : Binary protocols with modern characteristics . It is an application layer standard advanced message queuing protocol providing unified message service , Is an open standard for application layer protocols , Designed for message-oriented middleware

AMQP Protocol model :

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _Java

Publisher Contact before pushing messages Server Establishing a connection , find Virtual host, Then push the message to Exchange Switch . And the switch and Message Queue There is a binding relationship ( A switch is equivalent to a separate virtual machine , Various independent applications in this virtual machine are equivalent to one Queue, This Queue Bound to the switch ),Consumer Through the bound pair queue , The switch is also bound to the queue . The sender sends the message to the switch , In this way, the message push can be completed

Overall architecture

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The backend development _02

Basic concepts

Broker

Message Queuing service process , Receive a connection from the client , Realization AMQP Physical services .

Connection

Connect , Application and Broker The Internet connection .

Producer

Message producer , That is, the client side of the manufacturer , The production client sends the message to MQ.

Consumer

Message consumer , That is, the consumer client , receive MQ Forward message .

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 create multiple Channel, Every Channel Represents a conversational task

Message

news , Data transferred between the server and the application , from Properties and Body form .Properties You can decorate the message , For example, the priority of a message 、 Advanced features like delay ;Body Is the body of the message .

Virtual Host

Virtual address , For logical isolation , Top level message routing . One Virtual Host There can be several Exchange and Queue, The same Virtual Host It can't have the same name Exchange or Queue

Exchange

Switch , receive messages , Forward the message to the bound queue according to the routing key .

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The programmer _03

Common are 4 There are two different switch types :

  • Direct connection switch :Direct exchange
  • Sector switches :Fanout exchange
  • Theme switch :Topic exchange
  • The first switch :Headers exchange

Sector switches

Sector switch is the most basic switch type , The sector switch will send all the messages it can receive to the queue bound to itself . Because broadcasting doesn't need to think , Therefore, the fan-shaped switch is also the fastest of all switch types

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The backend development _04

Direct connection switch

Direct connect switch is a kind of switch with routing function , A queue is bound to a switch , In addition, bind another routing_key, When a message is sent , One needs to be specified binding_key, When this message is delivered to the switch , It will be sent to the designated queue by this switch . The same one binding_key It also supports applications to multiple queues

In this way, when a switch binds multiple queues , Will be sent to the corresponding queue for processing
 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _Java_05

Applicable scenario : Priority tasks , Send the message to the corresponding queue according to the priority of the task , This allows more resources to be allocated to handle high priority queues

Theme switch

Direct connected switch routing_key The plan is very simple , If we want to send a message to multiple queues , So this switch needs to be bound with a lot of routing_key, Suppose that each switch is bound with a pile of routing_key Connect to each queue . Then the management of messages will be extremely difficult .

therefore RabbitMQ A theme switch is provided , Messages sent to the subject switch need to carry the specified rules routing_key, The subject switch will send data to the corresponding server according to this rule ( Multiple ) On the queue .

Theme switch routing_key There need to be rules , Switches and queues binding_key Need to adopt *.#.*..... The format of , Each part uses . Separate , among :

  • * Represents a word
  • # Represents any number of ( Zero or more ) word

When the binding key of a queue is # When , This queue will ignore the routing key of the message , Receive all the messages
 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The programmer _06

The first switch

The first switch ignores routing_key A way of routing . The rules of router and switch routing are through Headers To exchange information , This is a bit like HTTP Of Headers.

Declare a switch as the first switch , When binding a queue , Define a Hash Data structure of , When the message is sent , Will carry a set of hash Data structure information , When Hash When the content of matches , The message is written to the queue .

When Binding switches and queues ,Hash A key is required in the structure x-match, This key is Value It can be any perhaps all, This means that the message carries Hash Yes, you need to match them all (all), Or just match one key (any) That's all right.

Compared to direct attached switches , The advantage of the head switch is that the matching rules are not limited to strings

  • any: As long as you carry a pair of key value pairs when publishing a message headers Satisfy multiple parameters of the queue definition arguments One of them can match , Note that here is the exact match of key value pairs , Only the key is matched , It's not good to have different values ;
  • all: All that you carry when you publish a message Entry Must be associated with all... Bound to the queue Entry perfect match

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The backend development _07

Binding

Exchange and Queue Virtual connection between ,Exchange With multiple Message Queue happen Binding Then a routing table will be generated , The routing table stores Message Queue The limitation of the required message is Binding Key. When Exchange received Message It will be resolved when Header obtain Routing Key,Exchange according to Routing Key And Exchange Type take Message Route to Message Queue.Binding Key from Consumer stay Binding Exchange And Message Queue When you specify , and Routing Key from Producer send out Message When you specify , The way the two match is by Exchange Type decision

Routing Key

A routing rule , The virtual machine can use it to determine how to route a particular message .

Queue

Also known as Message Queue, Message queue , Save messages and forward them to consumers .

Message release process :

  1. Producers and Broker establish TCP Connect .
  2. Producers and Broker Establish a channel .
  3. Producers send channel messages to Broker, from Exchange Forward the message .
  4. Exchange Forward the message to the specified Queue( queue )

Message receiving process :

  1. Consumers and Broker establish TCP Connect .
  2. Consumers and Broker Establish a channel .
  3. The consumer listens to the specified Queue( queue )
  4. When a message arrives Queue when Broker By default, messages are pushed to consumers .
  5. Consumer receives message .

Message flow process

The producer produces Message And deliver it to Exchange On

One Exchange You can bind multiple Message Queue, It depends on the routing policy (routing key) Route to the specified queue , Finally, the consumer listens to the queue

Working mode

Queue mode :

For heavy tasks or more tasks, using work queue can improve the speed of task processing .

1、 A message will only be received by one consumer ;

2、rabbitmq Polling is used to send messages to consumers on average ;

3、 After the consumer has processed a message , To receive the next message .

Publish subscribe mode :

1、 Each consumer listens to its own queue .

2、 The producer sent the message to broker, Messages are forwarded by the switch to each queue bound to the switch , Each queue bound to the switch will receive a message

Corresponding to... In the switch fanout type

Routing mode :

1、 Each consumer listens to its own queue , And set up routingkey.

2、 The producer sends the message to the switch , By switch according to routingkey To forward messages to the specified queue .

Corresponding to... In the switch direct type

Wildcard pattern :

Corresponding to... In the switch topics type

Header Transponder mode :

Corresponding to... In the switch header type

Remote procedure call mode :

RPC That is, the client calls the method of the server remotely , Use MQ Can achieve RPC Asynchronous invocation of , be based on Direct Switch implementation , The process is as follows :

  1. The client is the producer and the consumer , towards RPC Request queue send RPC Call message , Simultaneous monitoring RPC Response queue .
  2. Server monitoring RPC Message for request queue , The method of executing the server after receiving the message , Get the result returned by the method .
  3. The server will RPC The result of the method is sent to RPC Response queue .
  4. client (RPC The caller ) monitor RPC Response queue , Received RPC The results .

Last

It's not my style to give only the interview questions but not the answers . There are only a few interview questions , And the answer would be great Increase the length of the article , Reduce the readability of the article

Java Interview treasure 2021 edition

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The programmer _08

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _Java_09

Most common Java Analysis of interview questions (2021 The latest version )

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _Java_10

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The programmer _11

2021 Enterprises Java A selection of interview questions

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _Java_12

 The first-line Internet architect's design idea interprets the open source framework , My Tencent Java Interview experience sharing _ The backend development _13

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

版权声明
本文为[SDK integrated development]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002010503635G.html

Scroll to Top