How RabbitMQ Works and RabbitMQ Core Concepts

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How RabbitMQ Works and RabbitMQ Core Concepts相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/dep' title='dep'>dep   ike   涓嶅悓   sign   flow   sage   iss   cer   one   

https://www.javaguides.net/2018/12/how-rabbitmq-works-and-rabbitmq-core-concepts.html

n this quick article, we will learn what is RabbitMQ, how it works and core concepts of RabbitMQ.

RabbitMQ is an open source message broker software. It accepts messages from producers and delivers them to consumers. It acts like a middleman which can be used to reduce loads and delivery times taken by web application servers.

 

How RabbitMQ Works?

Let’s briefly have a look at how RabbitMQ works.
Let鈥榮 first familiar with a few important concepts of RabbitMQ:
  1. Producer: Application that sends the messages.
  2. Consumer: Application that receives the messages.
  3. Queue: Buffer that stores messages.
  4. Message: Information that is sent from the producer to a consumer through RabbitMQ.
  5. Connection: A connection is a TCP connection between your application and the RabbitMQ broker.
  6. Channel: A channel is a virtual connection inside a connection. When you are publishing or consuming messages from a queue - it鈥榮 all done over a channel.
  7. Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. To receive messages, a queue needs to be bound to at least one exchange.
  8. Binding: A binding is a link between a queue and an exchange. exchange鍙互鎶妋essage鍙戠粰涓嶅悓鐨剄ueue锛屽氨渚濋潬binding鏉ュ喅瀹?/li>
  9. Routing key: The routing key is a key that the exchange looks at to decide how to route the message to queues. The routing key is like an address for the message.
Producers send/publish the messages to the broker -> Consumers receive the messages from the broker. RabbitMQ acts a communication middleware between both producers and consumers even if they run on different machines.
While the producer is sending a message to the queue, it will not be sent directly, but sent using the exchange instead. The design below demonstrates how are the main three components are connected to each other.
The exchanges agents that are responsible for routing the messages to different queues. So that the message can be received from the producer to the exchange and then again forwarded to the queue. This is known as the ‘Publishing’ method.
鎶€鏈浘鐗? src=

 

 The message will be picked up and consumed from the queue; this is called ‘Consuming’.

 

 Send Message to Multiple Queues

By having a more complex application we would have multiple queues. So the messages will send it in the multiple queues.
鎶€鏈浘鐗? src=

 

Sending messages to multiple queues exchange is connected to the queues by the binding and the routing key.

A Binding is a “link” that you set up to connect a queue to an exchange. The Routing key is a message attribute. The exchange might look at this key when deciding how to route the message to queues (depending on exchange type). 

 

 Exchanges

Messages are not published directly to a queue, instead, the producer sends messages to an exchange.
An exchange is responsible for the routing of the messages to the different queues.
An exchange accepts messages from the producer application and routes them to message queues with the help of bindings and routing keys.
A binding is a link between a queue and an exchange.
鎶€鏈浘鐗? src=

 The message flow in RabbitMQ

  • The producer publishes a message to an exchange. When you create the exchange, you have to specify the type of it. The different types of exchanges are explained in detail later on.
  • The exchange receives the message and is now responsible for the routing of the message. The exchange takes different message attributes into account, such as routing key, depending on the exchange type.
  • Bindings have to be created from the exchange to queues. In this case, we see two bindings to two different queues from the exchange. The Exchange routes the message into the queues depending on message attributes.
  • The messages stay in the queue until they are handled by a consumer
  • The consumer handles the message.

Types of Exchanges

鎶€鏈浘鐗? src=
  1. Direct: A direct exchange delivers messages to queues based on a message routing key.
  2. Fanout: A fanout exchange routes messages to all of the queues that are bound to it.
  3. Topic: The topic exchange does a wildcard match between the routing key and the routing pattern specified in the binding.
  4. Headers: Headers exchanges use the message header attributes for routing.
 

 RabbitMQ Core Concepts

Here are some important concepts that need to be described before we dig deeper into RabbitMQ.
  1. Producer: Application that sends the messages.
  2. Consumer: Application that receives the messages.
  3. Queue: Buffer that stores messages.
  4. Message: Information that is sent from the producer to a consumer through RabbitMQ.
  5. Connection: A connection is a TCP connection between your application and the RabbitMQ broker.
  6. Channel: A channel is a virtual connection inside a connection. When you are publishing or consuming messages from a queue - it鈥榮 all done over a channel.
  7. Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. To receive messages, a queue needs to be bound to at least one exchange.
  8. Binding: A binding is a link between a queue and an exchange.
  9. Routing key: The routing key is a key that the exchange looks at to decide how to route the message to queues. The routing key is like an address for the message.
  10. AMQP: AMQP (Advanced Message Queuing Protocol) is the protocol used by RabbitMQ for messaging.
  11. Users: It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance.
 
 
 
 

 

以上是关于How RabbitMQ Works and RabbitMQ Core Concepts的主要内容,如果未能解决你的问题,请参考以下文章

What is Web Application Architecture? How It Works, Trends, Best Practices and More

How Xtuner E3 works for BMW 520d Diagnosis and initialization of CBS service

How it works(14) GDAL2Tiles源码阅读

how_tomcat_works

How `delete’ works ?

How Hystrix Works?--官方