Learn notes and summaries using Php-amqplib connection RABBITMQ

Source: Internet
Author: User
Tags composer install server port rabbitmq

1. Use composer to install Php-amqplib
Add a composer to your project. JSON file:  {     "require": {      "php-amqplib/php-amqplib": "2.6.*"     }} As long as you have installed the composer feature, you can run the following:   $ Composer Install an existing project then execute    $ composer update
At this time, the Verdor directory has been downloaded

For details, refer to the official documentation: HTTPS://GITHUB.COM/PHP-AMQPLIB/PHP-AMQPLIB
2. Using Php-amqplib
English Document: http://www.rabbitmq.com/getstarted.html
English Document: https://rabbitmq.shujuwajue.com/tutorials_with_php/[2]work_queues.md.html

Summary: Official documents look a little messy, summed up a bit, basically are interoperability, the following is a simple basic step, for reference only, NO BB. Producer:1, create a connection key parameter description:$host:RABBITMQ Server Host IP address$port:RABBITMQ Server Port$user:user name to connect to the RABBITMQ server$password:user password to connect to the RABBITMQ server$vhost:connection to the RABBITMQ server Vhost (server can have multiple vhost, virtual host, like Nginx Vhost)$connection=NewAmqpstreamconnection ($host,$port,$user,$password,$vhost); 2, get channel//$channel _id Channel ID, do not pass to get $channel[""] channel, then the Loop $this->channle array, subscript from 1 to the maximum number of channels to find the first is not the Amqpchannel object subscript, Instantiate and return Amqpchannel object, none throws exception no free channel IDs$channel=$connection->channel ($channel _id);3, creating a switch in the letter tunnel#$exhcange _name Exchanger name #$type Exchanger Type:The default switch anonymous exchanger does not show that the claim type is the type fanout sector switch sends a message to all the queues it knows, and each consumer gets a consistent message from the headers Head Exchanger Direct Direct-connect switch, The switch will bind the binding key ( theKey) and the routing key (routingKey) to precisely match the topic topic exchanger The switch will match the routing key and must be* (a single word),#(multiple words, separated by.), User.key. abc.* type keyRPC#$passive false#Durable false#Auto_detlete false$channel->exchange_declare ($exhcange _name,$type,$passive,$durable,$auto _delete);//Common Settings $passive =>false $durable =>false $auto _delete->false4, create the information you want to send, and create multiple messages#$data String type of message to send #$properties The property set by the array type, such as setting the message persisted [' Delivery_mode ' =>2]$msg=NewAmqpmessage ($data,$properties)5, send a message#$msg Object Amqpmessage#$exchange string Switch name#$routing _key String Routing key if switch typeFanout: This value is ignored because the type of switch sends a message to all of the queues it knows, without differentiating between direct only the queue that exactly matches the routing key, and the queue that sends the message to the queue topic only the regular match to the routing key, before it is sent to the queue$channel->basic_publish ($msg,$exchange,$routing _key);6, close channels and links$channel-close ();$connection-close (); Consumer:1, create a connection key parameter description:$host:RABBITMQ Server Host IP address$port:RABBITMQ Server Port$user:user name to connect to the RABBITMQ server$password:user password to connect to the RABBITMQ server$vhost:connection to the RABBITMQ server Vhost (server can have multiple vhost, virtual host, like Nginx Vhost)$connection=NewAmqpstreamconnection ($host,$port,$user,$password,$vhost); 2, get channel//$channel _id Channel ID, do not pass to get $channel[""] channel, then the Loop $this->channle array, subscript from 1 to the maximum number of channels to find the first is not the Amqpchannel object subscript, Instantiate and return Amqpchannel object, none throws exception no free channel IDs$channel=$connection->channel ($channel _id);3, creating a switch in the tunnel without explicitly declaring that the switches are using anonymous switches#$exhcange _name Exchanger name #$type Exchanger Type:The default switch anonymous exchanger does not show that the claim type is the type fanout sector switch sends a message to all the queues it knows, and each consumer gets a consistent message from the headers Head Exchanger Direct Direct-connect switch, The switch will bind the binding key ( theKey) and the routing key (routingKey) to precisely match the topic topic exchanger The switch will match the routing key and must be* (a single word),#(multiple words, separated by.), User.key. abc.* type keyRPC#$passive false#Durable false#Auto_detlete false$channel->exchange_declare ($exhcange _name,$type,$passive,$durable,$auto _delete);//Common Settings $passive =>false $durable =>false $auto _delete->false4, declaring the consumer queue (1) Non-persistent queue,when RABBITMQ exits or crashes, the queue does not existList($queue _name, ,) =$channel->queue_declare ("",false,false,false,false) (2persist the queue (need to display the declaration, the third parameter is set to TRUE), save to disk, but not necessarily completely guaranteed not to lose information, because the save always has time. List($queue _name, ,) =$channel->queue_declare ("Ex_queue",false,false,true,false)5, bind the switch, when the bound switch is not displayed, the default is to bind the anonymous switch#bindings: The relationship between the switch and the queue, as the following code means, $queue the _name queue is interested in the logs switch data, the queue consumes the data transmitted by the switch: This queue is interested in the message for this switch (Exchange). $ Binding_key is empty by default, indicating interest in all messages for the switch, and if the value is not NULL, the queue is only interested in messages of that type (except for the fanout switch) $channel->queue_bind ($queue _name, ' logs ',$binding _key);6, Consumer news#the code represents the use of the Basic.qos method and sets the Prefetch_count=1. This tells RABBITMQ that at the same moment, do not send more than 1 messages to a worker until it has processed the previous message and responded. In this way, RABBITMQ will distribute the message to the next idle worker, poll, load balancer configuration#$channel->basic_qos (NULL, 1, NULL);#The fourth parameter, No_ack = False, indicates an ACK response to ensure that the message has been processed#$callback represents the callback function, the incoming message parameter$channel->basic_consume (' Ex_queue ', ',false,false,false,false,$callback);$callback=function($msg){ Echo"[x] Received",$msg->body, "\ n"; Sleep(Substr_count($msg->body, '. ')); Echo"[x] Done", "\ n";#when No_ack=false, it is necessary to write down code, otherwise there may be an out-of-memory condition # $msg->delivery_info[' channel ']->basic_ack ($msg->delivery_info [' Delivery_tag ']);};#listen to the message and deal with it immediately. while(Count($channel-callbacks)) { $channel-wait ();}

Learn notes and summaries using Php-amqplib connection RABBITMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.