Basic Concepts
Broker:简单来说就是消息队列服务器实体。
Exchange: A message switch that specifies what rules the message is routed to and to which queue. Queue: A message queue carrier in which each message is put into one or more queues. Binding: Bind, which is the role of binding exchange and queue according to routing rules. Routing key: The routing keyword, exchange messages are delivered based on this keyword. Vhost: Virtual host, a broker can open multiple vhost, as a separate user permissions. Producer: The message producer is the program that delivers the message. Consumer: The message consumer is the program that receives the message. Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task. Message Persistence 1) Set the switch to be durable, 2) to make the channel durable 3) when the message is sent, the setting is persistent. When we "produce" a persistent message, try to interrupt the MQ service, start the consumer to get the message, the message can still be recovered. Instead, an exception is thrown.
Use procedure
消息队列的使用过程大概如下:(1)客户端连接到消息队列服务器,打开一个channel。
(2) The client declares an exchange and sets the related properties. (3) The client declares a queue and sets the related properties. (4) The client uses routing key to establish a good binding relationship between Exchange and queue. (5) Clients post messages to exchange. When Exchange receives a message, it routes messages to one or more queues based on the key of the message and the binding that has been set. There are several types of exchange, which are called direct switches that are delivered entirely according to key, for example, when the routing key is set to "ABC", the client submits a message that only the key "ABC" is set to be posted to the queue. When the key is matched with a pattern, it is called a topic switch, and the symbol "#" matches one or more words, and the symbol "" matches exactly one word. For example "abc.#" matches "Abc.def.ghi", "ABC." Matches only "Abc.def". There is also a need for a key, called the fanout Switch, which takes broadcast mode, when a message comes in, it is posted to all queues that are bound to the switch. RABBITMQ supports the persistence of messages, that is, data is written on disk, and for data security reasons, I think most users will choose to persist. Message Queuing persistence consists of 3 parts: (1) Exchange persistence, specifying durable + 1 (2) Queue persistence on declaration, specifying durable + 1 (3) Message persistence on declaration, specifying delivery_mode when posting = > 2 (1 non-persistent) if both Exchange and queue are persisted, the binding between them is persistent. If there is a persistence between Exchange and queue, a non-persisted, binding is not allowed.
Basic Examplesamqp_task.php
' 192.168.6.30 ', ' port ' = ' 5672 ', ' login ' = ' Dpjia ', ' password ' = ' Dpjia ', ' vhost ' = '/');//Create Connection $conn = new Amqpconnection ($conn _args), if ($conn->connect ()) {echo "Connection succeeded \ n";} Else{die ("Connection failed \ n");} Create Channel$channel = new Amqpchannel ($conn);//create exchange switch $ex_name = ' Ex_dpjia '; $ex = new Amqpexchange ($channel); $ Ex->setname ($ex _name), $ex->settype (amqp_ex_type_direct), $ex->setflags (amqp_durable); echo "Exchange Status: ". $ex->declareexchange ()." \ n "; $message = Json_encode (Array (' Hello world3! ', ' php3 ', ' c++3: ')); $routing _key = '; for ($i =0; $i <10; $i + +) {if ($ Routing_key = = ' key ') {$routing _key = ' Key2 '; }else{$routing _key = ' key '; } $ex->publish ($message, $routing _key);} $qu _name = ' qu_dpjia3 '; $qu = new Amqpqueue ($channel); $qu->setname ($qu _name); $qu->setflags (amqp_durable); Echo ' Queue status: '. $qu->declare (). " \ n "; Echo ' queue bind: '. $qu->bind ($ex _name, ' Route.key ')." \ n "; $channel->starttransaction (); echo" Send: ". $eX->publish ($message, ' Route.key '); Send your message through the enactment of Routingkey $channel->committransaction (); $conn->disconnect ();//Send Message//echo ' send message: '. $ex- >publish ("TEST message,key_1 by Xust". Date (' H:i:s ', Time ()), ' Key_1 '). " \ n ";//echo" Send Message: ". $ex->publish (" TEST message,key_2 by Xust ". Date (' H:i:s ', Time ()), ' key_2 '). " \ n ";? >
amqp_worker.php
' 192.168.6.30 ', ' port ' = ' 5672 ', ' login ' = ' Dpjia ', ' password ' = ' Dpjia ', ' vhost ' and ' = ' /');//Create connection $conn = new Amqpconnection ($conn _args), if ($conn->connect ()) {echo "Connection succeeded \ n";} Else{die ("Connection failed \ n");} $ex _name = ' Ex_dpjia '; $qu _name = ' Qu_dpjia '; $channel = new Amqpchannel ($conn);/* $ex = new Amqpexchange ($channel); $ex- >setname ($ex _name), $ex->settype (amqp_ex_type_direct), $ex->setflags (amqp_durable); Echo Exchange Status: ". $ex->declare ()." \ n "; *///Create queue queue$qu = new Amqpqueue ($channel); $qu->setname ($qu _name); $qu->setflags (amqp_durable); Echo $qu- >declare ();//bind switch and queue $qu->bind ($ex _name, ' Roukey2 ');//block mode Receive Message//echo "message:\n";//$qu->consume (' ProcessMessage ', amqp_autoack); Auto ack Answer $msg = $qu->get (amqp_autoack), if ($msg) {var_dump ($msg->getbody ());} $conn->disconnect ();//Message callback function Processmessagefunction ProcessMessage ($envelope, $queue) {var_dump ($envelope Getroutingkey); $msg = $envelope->gEtbody (); echo $msg. " \ n ";}?" >
PHP uses RABBITMQ