Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does RabbitMQ work?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces to you what is the working principle of RabbitMQ, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Introduction to RabbitMQ

Before introducing RabbitMQ, I would like to introduce what is MQ,MQ?

The full name of MQ is Message Queue, which can be understood as message queue, which simply means that messages are delivered in a pipeline way.

RabbitMQ is a message queuing service that implements AMQP (Advanced Message Queuing Protocol) advanced message queuing protocol, using Erlang language.

Working with scen

When we rush to buy goods in seconds, the system will remind us to wait in the queue, rather than jam or report the wrong page to the user as we did a few years ago.

Like this kind of queuing settlement, message queuing mechanism is used to put a settlement process in the channel, rather than a sudden influx of a large number of queries at a certain time to cause database downtime, so the essential role of RabbitMQ is to cut peaks and fill valleys to protect the business.

Why choose RabbitMQ?

Now there are many MQ choices on the market, such as ActiveMQ, ZeroMQ, Appche Qpid, so the question is why choose RabbitMQ?

Besides Qpid,RabbitMQ, it is the only message server that implements the AMQP standard.

Reliability, persistence support of RabbitMQ, ensuring the stability of messages

High concurrency, RabbitMQ uses the Erlang development language, Erlang is a language developed for telephone exchanges, born with high concurrency halo and high availability features

Cluster deployment is simple. It is Erlang that makes RabbitMQ cluster deployment super simple.

Community activity is high. According to online information, RabbitMQ is also the first choice.

Working mechanism

Producers, consumers and agents

Before you understand messaging, you need to understand three concepts: producer, consumer, and agent.

Producer: the creator of the message, responsible for creating and pushing data to the message server

Consumer: the recipient of the message, used to process the data and confirm the message

Agent: it is RabbitMQ itself, which is used to play the role of "express delivery". It does not produce messages, but only plays the role of "express delivery".

Message sending principle

First of all, you have to connect to Rabbit to publish and consume messages, so how do you connect and send messages?

Your application and Rabbit Server will create a TCP connection, once the TCP is opened, and passed the authentication, authentication is you try to connect to Rabbit before sending the Rabbit server connection information and user name and password, a bit like the program to connect to the database, there are two ways to connect to authentication using Java, the following code will describe in detail, once the authentication through your application and Rabbit to create an AMQP channel (Channel).

A channel is a virtual connection created on a "real" TCP. AMQP commands are sent through the channel, and each channel has a unique ID, whether it is publishing messages, subscribing to queues, or introducing messages.

Why not send commands directly through TCP?

It is very expensive for the operating system to create and destroy TCP sessions. Assuming that there are thousands of connections per second during the peak period, each connection has to create a TCP session, which results in a huge waste of TCP connections, and the operating system can create a limited number of TCP per second, so it will soon encounter system bottlenecks.

If we use one TCP connection for each request, it not only meets the performance needs, but also ensures the privacy of each connection, which is why the concept of channel is introduced.

You have to know, Rabbit.

There are some nouns you must know if you want to really understand Rabbit.

Including: ConnectionFactory (connection manager), Channel (channel), Exchange (switch), Queue (queue), RoutingKey (routing key), BindingKey (binding key).

* * ConnectionFactory (connection manager): * * A manager that establishes a connection between an application and Rabbit, which is used in program code

* * Channel (channel): * * the channel used for message push

* * Exchange (switch): * * for receiving and assigning messages

Queue (queue): used to store messages from producers

RoutingKey (routing key): used to distribute the generator's data to the switch

BindingKey (bind key): used to bind messages from the switch to the queue

Seeing the above explanation, the routing keys and binding keys are the most difficult to understand, so how do they work, please see the following figure:

For more information about switches, we will talk about them later.

Message persistence

Rabbit queues and switches have a dark secret, that is, restarting the server by default will result in message loss, so how to ensure that Rabbit is not lost when it is restarted? The answer is to persist the message.

When you send a message to the Rabbit server, you need to choose whether you want to persist, but this does not guarantee that Rabbit can recover from the crash. There are three conditions that must be met for Rabbit message to recover:

When delivering a message, durable is set to true, message persistence, code: channel.queueDeclare (x, true, false, false, null), and parameter 2 is set to true persistence

Set delivery mode deliveryMode to 2 (persistent), code: channel.basicPublish (x, x, MessageProperties.PERSISTENT_TEXT_PLAIN,x), parameter 3 to store plain text to disk

The message has arrived on the persistence switch

The message has reached the persistent queue

How persistence works

Rabbit will write your persistence message to the persistence log file on disk, and after the message is consumed, Rabbit will identify the message as waiting for garbage collection.

The disadvantage of persistence

The advantage of message persistence is obvious, but the disadvantage is also obvious, that is performance, because the performance of writing to hard disk is much lower than that of writing memory, thus reducing the throughput of the server. Although using SSD hard disk can ease things, it still sucks up the performance of Rabbit, which is very low when thousands of messages are written to disk.

So users should choose the way that suits them according to their own situation.

Virtual host

Each Rabbit can create a lot of vhost, which we call virtual hosts. Each virtual host is actually a mini version of RabbitMQ, with its own queues, switches and bindings, and its own permission mechanism.

Vhost characteristics

The default vhost for RabbitMQ is "/" out of the box.

Multiple vhost are isolated, multiple vhost can not communicate, and do not have to worry about naming conflicts (queues and switches and bindings), achieving multi-layer separation

Vhost must be specified when creating a user

Vhost operation

You can create: with the rabbitmqctl tool command:

Rabbitmqctl add_ vhost[vhost _ name]

Delete vhost:

Rabbitmqctl delete_ vhost[vhost _ name]

View all vhost:

Rabbitmqctl list_vhosts

Environment building

Earlier, we have introduced the steps of Ubuntu to build RabbitMQ: the environment of RabbitMQ on Ubuntu.

It's even easier if you install it on Windows10, put it in the download address:

Erlang/Rabbit Server Baidu network disk link: https://pan.baidu.com/s/1TnKDV-ZuXLiIgyK8c8f9dg password: wct9

Of course, you can also go to the official websites of Erlang and Rabbit, but the speed is relatively slow. My latest version of Baidu Cloud Rabbit: 3.7.6 Magi Erlang version: 20.2, note: do not download the latest Erlang, there is a problem opening the extension on Windows10, it won't open.

Install Erlang

Install Rabbit Server

Go to the installation directory\ sbin and use the command "rabbitmq-plugins enable rabbitmq_management" to start the web page management plug-in

Restart the Rabbit service

Use: http://localhost:15672 for testing. The default login account is guest and the password is guest.

Repeated installation of Rabbit Server pit

If it is not the first time to install Rabbit Server on Windows, be sure to uninstall Rabbit and Erlang clean and find the registry: HKEY_LOCAL_MACHINE\ SOFTWARE\ Ericsson\ Erlang\ ErlSrv to delete all entries under it.

Otherwise, it will not be able to start after Rabbit installation, and in theory, the order of uninstalling is to Rabbit in Erlang first.

Code implementation

Java version of the implementation, using maven project, create can see: MyEclipse2017 cracking settings and maven project build

After the project is created successfully, add the Rabbit Client jar package, which only needs to be configured in pom.xml, as shown below:

Com.rabbitmq amqp-client 5.2.0 copy code

The java implementation code is divided into two classes, the first is to create a Rabbit connection, and the second is that the application class publishes and consumes messages in the simplest way.

There are two ways to connect Rabbit:

Method 1:

Public static Connection GetRabbitConnection () {ConnectionFactory factory = new ConnectionFactory (); factory.setUsername (Config.UserName); factory.setPassword (Config.Password); factory.setVirtualHost (Config.VHost); factory.setHost (Config.Host); factory.setPort (Config.Port); Connection conn = null; try {conn = factory.newConnection () } catch (Exception e) {e.printStackTrace ();} return conn;} copy the code

Method 2:

Public static Connection GetRabbitConnection2 () {ConnectionFactory factory = new ConnectionFactory (); / / connection format: amqp://userName:password@hostName:portNumber/virtualHost String uri = String.format ("amqp://%s:%s@%s:%d%s", Config.UserName, Config.Password, Config.Host, Config.Port, Config.VHost); Connection conn = null Try {factory.setUri (uri); factory.setVirtualHost (Config.VHost); conn = factory.newConnection ();} catch (Exception e) {e.printStackTrace ();} return conn;} copy the code

Part II: application classes that publish and consume messages in the simplest way

Public static void main (String [] args) {Publisher (); / / push message Consumer (); / / consume message} / * push message * / public static void Publisher () {/ / create a connection Connection conn = ConnectionFactoryUtil.GetRabbitConnection () If (conn! = null) {try {/ / create channel Channel channel = conn.createChannel (); / / declare queue [parameter description: parameter 1: queue name, parameter 2: whether to persist Parameter 3: whether to monopolize the mode; parameter 4: whether to delete the queue when the consumer disconnects Parameter 5: other parameters of the message] channel.queueDeclare (Config.QueueName, false, null); String content = String.format ("current time:% s", new Date (). GetTime ()); / / send content [parameter description: parameter 1: switch name Parameter 2: queue name, parameter 3: other properties of the message-routing headers, this property is MessageProperties.PERSISTENT_TEXT_PLAIN used to set plain text messages to be stored to the hard disk Parameter 4: message body] channel.basicPublish ("", Config.QueueName, null, content.getBytes ("UTF-8")); System.out.println ("sent message:" + content); / / close connection channel.close (); conn.close () } catch (Exception e) {e.printStackTrace ();} / * * consumption message * / public static void Consumer () {/ / create a connection Connection conn = ConnectionFactoryUtil.GetRabbitConnection () If (conn! = null) {try {/ / create channel Channel channel = conn.createChannel (); / / declare queue [parameter description: parameter 1: queue name, parameter 2: whether to persist Parameter 3: whether to monopolize the mode; parameter 4: whether to delete the queue when the consumer disconnects Parameter 5: other parameters of the message] channel.queueDeclare (Config.QueueName, false, null) / / create a subscriber And accept the message channel.basicConsume (Config.QueueName, false, "", new DefaultConsumer (channel) {@ Override public void handleDelivery (String consumerTag, Envelope envelope, AMQP.BasicProperties properties) Byte [] body) throws IOException {String routingKey = envelope.getRoutingKey () / / queue name String contentType = properties.getContentType (); / / content type String content = new String (body, "utf-8"); / / message body System.out.println ("message body:" + content) Channel.basicAck (envelope.getDeliveryTag (), false); / / manually confirm the message [parameter description: parameter 1: index of the message Parameter 2: whether to reply in batch, true batch acknowledges messages smaller than index]}});} catch (Exception e) {e.printStackTrace ();} copy code

This article participates in the "OSC Source Program". You are welcome to join us and share it.

About the working principle of RabbitMQ is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report