December 10, 2019 10:02:52
Original: https://www.rabbitmq.com/tutorials/tutorial-four-php.html
routing
(Using PHP-amqplib )
prerequisites
This tutorial assumes that RabbitMQ is the standard port ( 5672 ) of the local host on install and run . If you use a different host, port or credentials, the connection settings need to be adjusted.
Where to get help
If you have trouble reading this tutorial, you can by mailing list with our contact.
In the previous tutorial, we built a simple logging system. We can log messages to many recipients broadcasting.
In this tutorial, we will add these features - we will only integrated sub-subscribe messaging is possible. For example, we can only critical error messages to the log file (to save disk space), while still being able to print all log messages on the console.
Binding
In the previous example, we have created binding. You may recall a similar code:
$ channel-> queue_bind($ queue_name,'logs');
Binding is the relationship between the exchange and the queue. It can be simply understood as: the message from the queue interested exchanges.
Binding can use additional routing_key parameters. To avoid basic_publish $ channel :: parameters of confusion, we will call the key bindings . This is what we can create with a key method of binding:
$ binding_key = '黑色' ;
$ channel-> queue_bind($ queue_name,$ exchange_name,$ binding_key);
Meaning the binding key depends on the type of exchange. Before we use the fanout exchange just ignore its value.
Direct exchange
On a tutorial system will log all messages broadcast to all users. We want to expand it to allow the filter messages based on the severity of the message. For example, we may want to log messages written to disk script only receive a serious error, without wasting disk space on the warning or information log messages.
We are using fan-out switch, it does not bring too much flexibility for us - it can only be unconscious broadcast.
We will use direct exchange. Directly exchange routing algorithm behind it is simple - the message into its binding key and the message of the routing keys exact match queue .
To illustrate this point, consider the following settings:
In this setting, we can see the Binding two queues of direct exchange X. The first queue is bound key orange , the second key bindings to two, a binding key is Black , other key bindings to Green .
In this arrangement, the use of routing keys orange publish messages to the switch will be routed to the queue Ql . Routing keys to black or green message will go to Q2 . All other messages are discarded.
Multiple Binding
Binding multiple queues with the same binding key is perfectly legal. In our example, we can use the key bindings black in X and Q1 add a binding between . In this case, the direct exchange behavior will be similar to a fan-out , and the message is broadcast to all matching queues. With a black message routing keys simultaneously transferred to the Q1 and Q2 .
Launch log
We will use this model in the recording system. We will send a message to direct the switch, rather than fan-out . We will provide the log severity as routing keys . In this way, the receiver will be able to choose the seriousness of the script they want to receive. Let us focus first launch logs.
As always, we need to first create a swap:
$ channel-> exchange_declare('direct_logs','direct',false,false,false);
We are ready to send a message:
$ channel-> exchange_declare('direct_logs','direct',false,false,false); $ channel-> basic_publish($ msg,'direct_logs',$ severity);
For simplicity, we will assume that "the severity" may be "information", "warning", one of "error."
subscription
Work and receive messages on a tutorial sessions, with one exception - we will be interested in the seriousness of each to create a new binding.
foreach($ severity 为 $ severity){
$ channel-> queue_bind($ queue_name,'direct_logs',$ severity);
}
put it together
emit_log_direct.php class code :
<?php
require_once __DIR__。'/vendor/autoload.php' ;
使用 PhpAmqpLib \ Connection \ AMQPStreamConnection ; 使用 PhpAmqpLib \ Message \ AMQPMessage ; $ connection = new AMQPStreamConnection('localhost',5672,'guest','guest'); $ channel = $ connection-> channel(); $ channel-> exchange_declare('direct_logs','direct',false,false,to false ); $ Severity = isset ($ the argv [ . 1 ]) &&! Empty ($ argv [ 1 ]) do? The argv $ [ . 1 ]: 'info' ; $ = Data The implode ( '' , array_slice ($ the argv, 2 )); IF ( empty ($ Data)) { $ Data = "Hello World!" ; } $ MSG = new AMQPMessage ($ Data); $ Channel-> basic_publish ($ MSG, 'direct_logs' , $ Severity); echo '[X] send' , $ Severity, ':' , $ Data, "\ n-" ; $ channel-> close(); $ connection-> close();
receive_logs_direct.php code :
<?php
require_once __DIR__。'/vendor/autoload.php' ;
使用 PhpAmqpLib \ Connection \ AMQPStreamConnection ; $ connection = new AMQPStreamConnection('localhost',5672,'guest','guest'); $ channel = $ connection-> channel(); $ channel-> exchange_declare('direct_logs','direct',false,false,false); list($ queue_name,,)= $ channel-> queue_declare(“”,to false , to false , to true , to false ); $ = array_slice severities (the argv $, . 1 ); IF ( empty ($ severities)) { file_put_contents ( 'PHP: // stderr' , "Usage: $ argv [0] [info ] [warning] [error] \ n-" ); exit ( . 1 ); } the foreach ($ Severity of $ Severity) { $ Channel-> queue_bind ($ queue_name, 'direct_logs' , $ Severity); } echo " [*] wait The log to exit, press C + the CTRL \ n-" ; $ = the callback function ($ MSG) {echo '[x]',$ msg-> delivery_info [ 'routing_key' ],':',$ msg-> body,“ \ n” ; }; $ channel-> basic_consume($ queue_name,'',false,true,false,false,$ callback); 而($ channel-> is_using()){ $ channel-> wait(); } $ channel-> close(); $ connection-> close();
If you want to save the "Warning" and "error" (instead of "information") log messages to a file, simply open the console and type:
PHP's receive_logs_direct.php warning error> logs_from_rabbit.log
If you want to see all log messages on the screen, open a new terminal and do the following:
php receive_logs_direct.php Information Warning Error
# => [*] wait for the log. To exit, press CTRL + C
For example, to send out the error log message, type:
php generate_log_direct.php error "Run run or it will explode..."
# => [the X-] to send the "wrong": "Run." run. Otherwise it will explode.
( (Emit_log_direct.php source code) and a (receive_logs_direct.php source code) complete source code )
Continue to learn first five tutorials to learn how to listen for messages depending on the mode.