(Notes) Getting Started with RabbitMQ

PHP using RabbitMQ introductory tutorial

Detailed introductory tutorial: PHP using RabbitMQ introductory tutorial

The concept of RabbitMQ

Message model

All MQ products have the same process from the model abstraction:
consumers subscribe to a certain queue. The producer (producer) creates a message, then publishes it to the queue (queue), and finally sends the message to the listening consumer.
Insert picture description here

RabbitMQ basic concepts

The above is just the simplest and abstract description, specific to RabbitMQ, there are more detailed concepts that need to be explained. As mentioned above, RabbitMQ is an open source implementation of the AMQP protocol, so its internal is actually a basic concept in AMQP:
Insert picture description here

  • Message

Message, the message is anonymous, it consists of a message header and a message body. The message body is opaque, while 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.

  • Publisher

The message producer is also a client application that publishes messages to the exchange.
Producer workflow: Create connection --> Create channel --> Create switch object --> Send message --> Close connection

  • Exchange

The exchange is used to receive the messages sent by the producer and route these messages to the queue in the server.

  • Binding

Binding, used for the association between the message queue and the exchange. 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.

  • Queue

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. A message can be put into one or more queues. The message has been in the queue, waiting for the consumer to connect to this queue to take it away.

  • Connection

Network connection, such as a TCP connection.

  • Channel

Channel, an independent two-way data flow channel in a multiplexed connection. 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.

  • Consumer

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

Consumer workflow: create connection --> create channel --> create switch --> create queue --> bind switch/queue/routing key --> receive message

  • Virtual Host

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. Each vhost is essentially a mini version of RabbitMQ server, with its own queue, switch, binding, and permission mechanism. The vhost is the basis of the AMQP concept and must be specified when connecting. The default vhost of RabbitMQ is /.

  • Broker

Represents the message queue server entity.

Exchange type

There are three main types of Exchange: Fanout, Direct and Topic, (there are other two Header, Default).
Generally speaking, direct and topic are used for specific routing messages. If you want to use broadcast messages, you generally use fanout.

Fanout

Fanout Exchange will ignore the RoutingKey setting and directly broadcast the Message to all bound Queues.
Insert picture description here
The application scenario
takes the log system as an example: suppose we define an Exchange to receive log messages, and define two Queues to store messages: one record will be printed to the console log message; the other record will be written to a disk file Log message. We hope that every message received by Exchange will be forwarded to two Queues at the same time. In this scenario, Fanout Exchange can be used to broadcast messages to all bound Queues.

Direct

Direct Exchange is the default Exchange of RabbitMQ, which routes messages completely based on the RoutingKey. When setting the Binding of Exchange and Queue, you need to specify the RoutingKey (generally Queue Name), and specify the same RoutingKey when sending a message, and the message will be routed to the corresponding Queue.
Insert picture description here
Application Scenario
Now we consider only writing important log messages to disk files, for example, sending only Error level logs to the Queue responsible for writing records to disk files. In this scenario, we can use the specified RoutingKey (such as error) to bind the Queue written to the disk file to Direct Exchange.

Topic
Topic Exchange is similar to Direct Exchange. It also needs RoutingKey to route messages. The difference is that Direct Exchange matches RoutingKey exactly, while Topic Exchange supports fuzzy matching. Support "*" character and # respectively, * means matching one word, # means matching no or multiple words.
Insert picture description here
Application scenario
Assume that our message routing rules need to be distributed according to the source of the message in addition to the log level. The RoutingKey can be defined as the source of the message. Levels such as order.info, user.error, etc. Processing all Queues whose source is user can be bound to Topic Exchange through user.*, and processing all Queues whose log level is info can be bound to Exchange through *.info.

PHP example

send.php

<?php
require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();

receive.php

<?php
require_once __DIR__ . '/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
 
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
    
    
    echo ' [x] Received ', $msg->body, "\n";   // $msg->body 即接收到的消息
};
 
$channel->basic_consume('hello', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
    
    
    $channel->wait();
}
$channel->close();
$connection->close();

Reference: Understanding RabbitMQ Exchange

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/105711404