02.RabbitMQ function overview

02. What is RabbitMQ

1.1MQ (Message Queue) message queue

  • Message queue middleware is an important component in distributed systems

  • Mainly solve problems such as asynchronous processing, application decoupling, traffic peak shaving, etc.

  • Thus enabling a high-performance, highly available, scalable and eventually consistent architecture

  • Use more message queue products: RabbitMQ, RocketMQ, ActiveMQ, ZeroMQ, Kafka, etc.

1.2The main role of MQ

1.2.1 Asynchronous processing

For example, in the ordering system, the overall process only needs the user to place an order and pay for the process to end. However, there are many systems involved in the payment of e-commerce companies, and the payment may involve the coupon system, the point system, and the SMS system . Assuming that the payment takes 100ms, the coupon system, points system, and SMS system also consume 100ms, and the entire time spent is 400ms.

process:
insert image description here

As you can see, only three have been added. I can tell you with absolute certainty that the real ordering process involves more than 10 systems (mainstream e-commerce), and the bigger the number, the more.

If this link goes on like this, it will take a lot of time for users to find out that it takes tens of seconds for me to buy something. I don’t buy junk e-commerce from you, but if it’s as cheap as Xixixi , it’s really delicious !

But the company does not have the economic strength of Xixi, so it can only optimize the system.

Well, yes, the link becomes slow when the link is long. How to solve it?

The longer the link is, the slower it will be, but we found that the above process can actually be done at the same time. After you pay successfully , I can increase or decrease the points while verifying the coupon, and I can also send a text message at the same time. .

We can't achieve the normal process, what should we do, asynchronous .

If you compare it, you can find that it only takes 100 milliseconds at most for the user to know that the order is successful. As for the text message you send him a few seconds later, he doesn't care at all, right?

The three systems only need to monitor whether there is a payment message in the queue. If there is a coupon system to verify the coupons, the points system can also increase or decrease points, and the SMS system can send a text message at the same time.
insert image description here

Boy, let me interrupt you, you said asynchronous, so I use threads and thread pools to do it, isn't it the same?

Hey , interviewer, don't worry , I'll talk about it later, Sao, etc.

1.2.2 Decoupling:

Since the interviewer asked this, let me tell you why we can't do it with threads, because do it with threads, do you want to write code?

You have an order process, you deduct points, deduct coupons, send text messages, and deduct inventory. . . Waiting for so many businesses to call so many interfaces, each time you add one, you have to call an interface and then republish the system !

And if it’s really all written together, it’s not just the coupling problem, it’s also troublesome to troubleshoot if you have a problem. If a problem occurs in any part of the process, it might affect other points. My friend said that I have every process. If you don't try catch , don't do it. Believe me, don't do it. This kind of code is like a time bomb . You don't know when it will explode. Usually it doesn't explode , but it explodes when you are doing activities . New Years at home .

Tip: P0-PN is the mechanism that Internet companies often use to determine the accident level, and P0 is the highest level.

But if you use a message queue , the problem of coupling is easily solved.

When you place an order, you will tell other systems that your payment is successful , and they will process it when they receive it. You only need to go through your own process and send your message. Then what system do you want to connect to? It's simple, just subscribe to the payment success message you send, and I'll monitor it if your payment is successful .
insert image description here

Then your process is over, don't you care if others succeed? For example, if you place an order, but the points are not added, and the coupons are not deducted, what should I do?

The question is a good question, but there is no need to think about it. The business system itself is maintained by its own developers. What does it mean for me to place an order if you fail to deduct points? Just take care of your ordering system.

Tip : That's what I said, but this is actually a disadvantage of using message queues, which involves the knowledge points of distributed transactions , which I will mention below.

1.2.3 Flow peak clipping

  • Snap purchases, spikes and other businesses, targeting high concurrency scenarios
  • Because the traffic is too large, the surge will cause the application to hang. To solve this problem, add a message queue at the front end

img

  • After the user's request is received by the server, it is first written to the message queue. If it exceeds the length of the queue, it is discarded and a page that ends in a flash!
  • To put it bluntly, the one who succeeds in the spike is the user who enters the queue;

2. Background knowledge introduction

2.1 AMQP Advanced Message Queuing Protocol

  • The Advanced Message Queuing Protocol, an application layer standard advanced message queuing protocol that provides unified messaging services

  • Protocol: The rules that must be followed in the process of data transmission

  • Clients based on this protocol can pass messages with message middleware

  • Not limited by product, development language, etc.

2.2 JMS

  • Java Message Server, Java Message Service API, a specification that plays a similar role to JDBC.
  • It is an API for message-oriented middleware in the Java platform, which is used to send messages between two applications or distributed systems for asynchronous communication.

2.3 The connection between the two

  • JMS defines a unified interface and unified message operation; AMQP unifies data interaction format through protocol
  • JMS must be in java language; AMQP is just a protocol, not language related

2.4 Erlang language

  • Erlang (['ə:læŋ]) is a general-purpose concurrency-oriented programming language developed by CS-Lab, which is owned by Swedish telecom equipment manufacturer Ericsson, to create a programming language that can handle large-scale concurrent activity and operating environment

  • Originally designed by Ericsson specifically for communication applications, such as control switches or conversion protocols, etc., it is ideal for building distributed, real-time soft-parallel computing systems

  • The Erlang runtime environment is a virtual machine, a bit like Java's virtual machine, so that once the code is compiled, it can also be run anywhere.

3 Why choose RabbitMQ

  1. We started by saying that there are so many message queue products, why did you choose RabbitMQ?

  2. First look at the naming: Rabbits move very fast and breed insanely, so Rabbit is used as the name of this distributed software (it's that simple)

  3. Erlang development, the best partner of AMQP, easy installation and deployment, low threshold for getting started

  4. Enterprise-level message queue, highly reliable after a lot of practical tests, a large number of successful application cases, such as Ali, NetEase and other first-tier manufacturers have used it

  5. Has a powerful WEB management page

  6. Strong community support to power technological progress

  7. Support message persistence, support message confirmation mechanism, flexible task distribution mechanism, etc., support functions are very rich

  8. Cluster scaling is easy, and performance gains can be multiplied by adding nodes

  9. Summary: If you want to use a reliable, powerful, and easy-to-manage message queuing system, then choose

  10. RabbitMQ, if you want to use a high performance, but don't care about losing data occasionally, you can use kafka or zeroMQ

  11. The performance of kafka and zeroMQ is beyond the table, and it can definitely overwhelm RabbitMQ!

4. Functions of each component of RabbitMQ

img

  • Broker : message queue server entity

  • Virtual Host : virtual host

    • Identifies a collection of exchanges, message queues, and related objects, forming a whole
    • Virtual hosts are separate server domains that share the same authentication and encryption environment
    • Each vhost is essentially a mini version of the RabbitMQ server, with its own queues, exchanges, bindings and permission mechanisms
    • vhost is the basis of the AMQP concept, the default vhost of RabbitMQ is /, which must be specified when linking
  • Exchange : Exchange (routing)

    • Used to receive messages sent by producers and route those messages to queues in the server
  • Queue : message queue

    • Used to hold the message until it is sent to the consumer.
    • It is the container for the message and the destination for the message.
    • A message can be put into one or more queues.
    • The message has been in the queue, waiting for the consumer to connect to the queue to take it away.
  • Banding : Binding for association between message queues and exchanges.

  • Channel : channel (channel)

    • An independent bidirectional data stream channel in a multiplexed connection.
    • Channels are virtual links established within real TCP connections
    • AMQP commands are all sent through channels, whether publishing messages, subscribing to queues or receiving messages, are all done through channels
    • Because it is very expensive for the operating system to establish and destroy TCP connections, the concept of channels is introduced to reuse TCP connections.
  • Connection : A network connection, such as a TCP connection.

  • Publisher : The producer of the message, which is also a client application that publishes messages to the exchange.

  • Consumer : The consumer of the message, representing a client application that gets messages from the message queue.

  • Message : message

    • The message is anonymous, it consists of a message header and a message body.
    • The message body is opaque, and the message header is composed of a series of optional attributes, including routing-key (routing key), priority (priority), delivery-mode (message may require persistent storage [message's routing mode]), etc.

Guess you like

Origin blog.csdn.net/qq_41239465/article/details/123548723