“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Let’s learn RabbitMQ

Hi, everyone, I am Nezha. We will start our study of open source components from today. We will summarize and share while learning

The article is outlined as follows:

  • A brief introduction to RabbitMQ
  • Six working modes of RabbitMQ

This article does not involve coding, coding we put in the next unified

RabbitMQ brief introduction

RabbitMQ is an open source (MPL) Message Queuing service implemented by LShift as part of the Advanced Message Queuing Protocol (AMQP). It is written by Erlang, which is renowned for its high performance, robustness, and scalability.

What is AMQP?

Advanced message queuing protocol, which enables full functional interoperability between compliant client applications and messaging middleware servers

This protocol allows client applications to interact with the message broker and the AMQP model it implements

What is MQ?

Message Queue, Message bus

It is a cross-process, asynchronous communication mechanism

Used for upstream and downstream transmission of messages, by the message system to ensure the reliable transmission of messages.

What scenarios are RabbitMQ used in?

  • Peak flow control
  • Ensure final consistency of data
  • Upstream and downstream logic decoupling
  • The specific usage scenarios are described belowRabbitMQIs closely related to six working modes

What is included in RabbitMQ components?

  • consumers

You can subscribe to a queue

  • producers

The message is created, published to a queue, and eventually sent to the listening consumer

  • Broker:

An entity that identifies the message queue server

  • Virtual Host

Virtual host

Identifies a batch of switches, message queues, and related objects

A virtual host is a separate server domain that shares the same authentication and encryption environment

Each Vhost is essentially a mini RabbitMQ server with its own queue, switch, binding and permission mechanism

  • Exchange

A switch that receives messages sent by producers and routes them to queues in the server

  • Queue

Message queues, used to hold messages until they are sent to consumers

It is the container and destination of the message

A message can be put into one or more queues

The message stays in the queue, waiting for the consumer to connect to the queue and pick it up

  • Banding

Binding for association between message queues and switches

A binding is a routing rule that connects a switch to a message queue based on a routing key, so a switch can be thought of as a routing table made up of bindings

  • Channel

Channel: an independent two-way data channel in a multiplexing connection

The channel is built on the real TCP connection and the virtual link. The AMQP command is sent out through the new arrival. Whether it is to publish messages, subscribe to queues or receive messages, these actions are completed through the channel in order to reuse a TCP connection

  • Connection

Network connections, such as a TCP connection, can be multiple

  • Publisher

Message producer

Also a client application that publishes messages to the exchange

  • Consumer

Consumers of information

Represents a client application that retrieves messages from a message queue

  • Message

A message, which consists of a header and a body

The body of the message is opaque, while the header is made up of a series of optional attributes

These attributes include the following parameters

  • routing-key(Routing key)
  • priority(Priority)
  • delivery-mode(Messages may require persistent storage [routing pattern for messages])

What MQ is on the market?

  • RabbitMQ
  • Kafka
  • ActiveMQ
  • ZeroMQ
  • RocketMQ

So why did we choose RabbitMQ?

Let’s take a look at RabbitMQ’s features

  • reliability

RabbitMQ uses persistence, transport confirmation, and publication confirmation mechanisms to ensure reliability

  • Flexible routing

Messages are routed through Exchange before they are queued

For typical routing, RabbitMQ already provides some built-in Exchange

For complex routing, RabbitMQ can bind multiple exchanges together and implement its own Exchange through a plug-in mechanism

  • Message cluster

Multiple RabbitMQ servers can form a cluster to form a logical Broker

  • High availability

RabbitMQ queues can be mirrored on machines in a cluster, making them available even if some nodes fail

  • Multilingual client

Support for almost all common languages, such as Java,.NET, Ruby, GO, etc

  • Management interface

Provides an easy-to-use user interface that allows users to monitor and manage message brokers

  • Tracking mechanism

If a message is abnormal, RabbitMQ provides a mechanism for tracing the message to find out what happened

  • Plug-in mechanism

Many plug-ins are provided to extend from many aspects, and you can also edit your own plug-ins

Six working modes of RabbitMQ

Images from the RabbitMQ official documentation www.rabbitmq.com/getstarted….

Simple Simple mode

The relationship between a simple message producer and a simple consumer, the producer produces the message, puts it in the queue, consumes the message in the queue

  • The message producer queues the message
  • The consumer of the message listens to the message queue and consumes it if there is a message in the queue

When a message is removed, it is automatically deleted from the queue

Work mode

Multiple consumers consume messages in the same queue, and queues send messages to consumers on average in the form of polling. Resources here are competitive

  • The message producer puts the message into the queue, where there can be more than one consumer

Consumer C1, consumer C2, listening on the same queue

The message is competed by consumers C1 and C2 for the current message queue content, and the one who gets the message first is responsible for consuming the message

There is the problem that the same message will be consumed by different consumers, and we need to deal with that

You can set a switch, syncronize, to ensure that a message can only be used by one consumer

  • Used in the scene
    • A red envelope scene
    • Resource scheduling in large projects

Publish /subscribe publish/subscribe mode

Publish /subscribe, with one more switch than the Work Queues, where resources are shared

The production end sends messages to the switch and the switch sends messages to bound queues. Each bound queue can receive messages sent by the production end

  • X stands for switchRabbitMQInternal components, where each consumer listens on its own queue
  • The producer sends the messagebroker, the switch forwards the message to each queue bound to the switch, and each queue bound to the switch receives the message
  • Used in the scene
    • E-mail group
    • Group chat
    • Broadcasting (advertising, etc)

Routing Routing mode

  • Each consumer listens to its own queue and sets a wildcardroutingkey
  • The producer sends the message to the broker for the exchange to followroutingkeyTo forward messages to the specified queue

The general process involved is as follows:

Producer processing process:

Declare a queue and declare a switch -> Create a connection -> Create a channel -> Channel Declaration Switch -> Channel declaration queue -> Bind a queue to a switch through a channel and specify a RoutingKey for that queue -> Make a message -> Send a message and specify a RoutingKey

Consumer processing process:

Declare queues and declare switches -> Create connections -> Create channels -> Channel declaration switches -> Channel declaration queues -> Bind queues to switches through channels and specify routingKey (wildcard) -> Override message consumption methods -> Execute message methods

The Topics pattern is actually one of the routing patterns

The biggest difference between the two is that the Topics mode sends and consumes messages matched by wildcards

Note the following for wildcards:

  • * / #Asterisks or pound signs represent wildcards
  • Asterisks stand for multiple words, while pound signs stand for one word
  • Fuzzy matching was added to the routing function
  • Message producers produce messages and deliver them to the switchexchange
  • Exchange according tokeyThe rules ofFuzzy matchingTo the corresponding queue, by the queueSpying on consumersReceive message consumption

RPCmodel

RPC remote procedure call, the client remote call server method, using MQ can achieve RPC asynchronous call

This is based on the Direct switch and the flow is as follows:

  • The client, both producer and consumer, sends RPC call messages to the RPC request queue and listens to the RPC response queue

  • The server listens to the message in the RPC request queue, executes the method of the server after receiving the message, and gets the result returned by the method

  • The server sends the result of the RPC method to the RPC response queue.

  • The client listens to the RPC response queue and receives the result of the RPC call

conclusion

  • RabbitMQIs a set of open source (MPL) message queue service software
  • Each RabbitMQ component contains the following parts
    • producers
    • consumers
    • The broker entity
    • Virtual Host INDICATES the VM
    • Exhcnage switches
    • Binding binding
    • The queue queue
    • The connection to connect
    • The channel channel
    • The message the message
  • chooseRabbitMQWhat are the reasons for
  • RabbitMQThere are six working modes:
    • Simple Simple mode
    • Work mode
    • Publish/Subscribe Subscribe publish mode
    • Routing Routing mode
    • The RPC model

References:

RabbitMQ Tutorials

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~