JAVA core knowledge points: a detailed introduction to the basic concepts of RabbitMQ

RabbitMQ preface

RabbitMQ is an open source message broker software (also known as message-oriented middleware) that implements the Advanced Message Queuing Protocol (AMQP). The RabbitMQ server is written in Erlang language, and the clustering and failover are built on the framework of the open telecom platform. All major programming languages ​​have client libraries that communicate with the agent interface.

1.1. Concept

RabbitMQ is an open source implementation of AMQP developed by Erlang language.
AMQP :Advanced Message Queue,高级消息队列协议。它是应用层协议的一个开放标准, Is designed for message-oriented middleware. Clients and message middleware based on this protocol can transmit messages without being restricted by conditions such as products and development languages.
RabbitMQ 最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗. Specific features include:

  1. 可靠性(Reliability): RabbitMQ uses some mechanisms to ensure reliability, such as persistence, transmission confirmation, and release confirmation.
  2. 灵活的路由(Flexible Routing): Before the message enters the queue, the message is routed through Exchange. For typical routing functions, RabbitMQ has provided some built-in Exchange implementations. For more complex routing functions, you can bind multiple Exchanges together, and also implement your own Exchange through the plug-in mechanism.
  3. 消息集群(Clustering): Multiple RabbitMQ servers can form a cluster to form a logical Broker.
  4. 高可用(Highly Available Queues): The queue can be mirrored on the machines in the cluster, so that the queue is still available when some nodes have problems.
  5. 多种协议(Multi-protocol): RabbitMQ supports multiple message queue protocols, such as STOMP, MQTT, etc.
  6. 多语言客户端(Many Clients): RabbitMQ supports almost all common languages, such as Java, .NET, Ruby, etc.
  7. 管理界面(Management UI):RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage many aspects of the message broker.
  8. 跟踪机制(Tracing): If the message is abnormal, RabbitMQ provides a message tracking mechanism so that the user can find out what happened.
  9. Plugin System (Plugin System): RabbitMQ provides many plug-ins to expand in many ways, and you can also write your own plug-ins.

1.2. RabbitMQ architecture

Insert picture description here

1.2.1. Message

Message, the message is anonymous, it consists of the message header and the message body. The message body is opaque, and the message header consists of a series of optional attributes, including routing-key (routing key), priority (priority relative to other messages), delivery-mode (pointing out that the message may require Persistent storage) etc.

1.2.2. Publisher

  1. The message producer is also a client application that publishes messages to the exchange.

1.2.3. Exchange (route messages to queues)

  1. Exchangers用来接收生产者发送的消息并将这些消息路由给服务器中的队列 .

1.2.4. Binding (the association between the message queue and the exchange)

  1. Binding, used for 消息队列和交换器之间的关联. A binding is a routing rule that connects the switch and the message queue based on the routing key, so the switch can be understood as a routing table composed of bindings.

1.2.5. Queue

  1. The message queue is used to save the message until it is sent to the consumer. It is the container of the message and the end of the message. 一个消息可投入一个或多个队列。消息一直在队列里面,等待消费者连接到这个队列将其取走.

1.2.6. Connection

  1. Network connection, such as a TCP connection.

1.2.7. Channel

  1. Channel多路复用连接中的一条独立的双向数据流通道 . A channel is a virtual connection established within a real TCP connection. AMQP commands are sent through the channel. Whether it is publishing messages, subscribing to queues or receiving messages, these actions are all completed through the channel. Because it is very expensive for the operating system to establish and destroy TCP,
    the concept of channel is introduced to reuse a TCP connection.

1.2.8. Consumer

  1. The consumer of the message represents a client application that gets the message from the message queue.

1.2.9. Virtual Host

  1. Virtual host, which represents a batch of exchanges, message queues and related objects. A virtual host is an independent server domain that shares the same authentication and encryption environment.

1.2.10.Broker

  1. Represents the message queue server entity.

1.3. Exchange Type

When Exchange distributes messages, there are differences according to different types of distribution strategies 四种类型:direct、fanout、topic、headers. headers match the header of the AMQP message instead of the routing key. In addition, the headers switch and the direct switch are exactly the same, but the performance is much worse, and it is almost unused at present, so look directly at the other three types:

1.3.1. Direct key (routing key) distribution:

  1. Direct: In the message, the 路由键(routing key)如果和 Binding 中的 binding key 一致exchange sends the message to the corresponding queue. It is an exact match, unicast mode.

1.3.2. Fanout (Broadcast Distribution)

  1. Fanout: Each message sent to the fanout type exchange will be distributed to all bound queues. Much like subnet broadcasting, every host in the subnet gets a copy of the message. The fanout type is the fastest to forward messages.

1.3.3. topic switch (pattern matching)

  1. topic topic 交换器通过模式匹配分配消息的路由键属性,将路由键和某个模式进行匹配,此时队列需要绑定到一个模式上switch: . It divides the string of routing key and binding key into words, separated by dots. It also recognizes two wildcard characters: the symbol "#" and the symbol "". #Match 0 or more words, match no more than one word.

JAVA core knowledge points: a detailed introduction to the basic concepts of MongoDB

Guess you like

Origin blog.csdn.net/qq_46914021/article/details/109203696