RABBITMQ PHP Message Queuing installation Tutorial

Source: Internet
Author: User
Tags bind install php rabbitmq

RABBITMQ is a complete, reusable enterprise messaging system based on AMQP. He follows the Mozilla Public License open source agreement.

1, install rabbitmq,php extension

# yum Install PHP-PECL-AMQP rabbitmq-server epel-release

2, start

#/etc/init.d/rabbitmq-server Start

If you want to change what configuration, add the file in the/ETC/RABBITMQ directory: rabbitmq-env.conf, note: file name is fixed
rabbitmq_node_ip_address=192.168.10.208
rabbitmq_node_port=5672
Rabbitmq_nodename=rabbit

3, enable the monitoring module


# Cd/usr/lib/rabbitmq/lib/rabbitmq_server-3.1.5/sbin
#./rabbitmq-plugins Enable rabbitmq_management//Enable monitoring module
The following plugins have been enabled:
Mochiweb
Webmachine
Rabbitmq_web_dispatch
Amqp_client
Rabbitmq_management_agent
Rabbitmq_management

#/etc/init.d/rabbitmq-server Restart//reboot


4,php Test Code


<?php
Class RABBITMQ
{
Public $configs = Array (' Host ' => ' 192.168.10.208 ', ' Port ' =>5672, ' username ' => ' guest ', ' Password ' => ' guest ' , ' vhost ' => '/');
Switch name
Public $exchange _name = ' test-e ';
Queue name
Public $queue _name = ' test-q ';
Route Name
Public $route _key = ' test-r ';
/*
* Persistent, default true
*/
Public $durable = true;
/*
* Automatic deletion
* Exchange is deleted when all queues have finished using it
* Queue is deleted the last consumer Unsubscribes
*
*/
Public $autodelete = true;
/*
* Mirroring
* Mirrored queues, open messages are replicated between nodes, with master and slave concepts
*/
Public $mirror = false;

Private $_conn = null;
Private $_exchange = null;
Private $_channel = null;
Private $_queue = null;

/**
* Create the virtual host, switch that the queue needs to use
*/
Public function __construct ($configs = Array (), $exchange _name = ', $queue _name = ', $route _key = ') {
if (!emptyempty ($configs)) {
$this->setconfigs ($configs);
}
if (!emptyempty ($exchange _name)) {
$this->exchange_name = $exchange _name;
}
if (!emptyempty ($queue _name)) {
$this->queue_name = $queue _name;
}
if (!emptyempty ($route _key)) {
$this->route_key = $route _key;
}

$this->_conn = new Amqpconnection ($this->configs);
$this->_conn->connect ();

Create Channel
$this->_channel = new Amqpchannel ($this->_conn);
Creating Exchange Switches
$this->_exchange = new Amqpexchange ($this->_channel);
$this->_exchange->setname ($this->exchange_name);//Create Name
$this->_exchange->settype (Amqp_ex_type_direct);
$this->_exchange->setflags (amqp_durable | Amqp_autodelete);
$this->_exchange->declare ();

creating queues
$this->_queue = new Amqpqueue ($this->_channel);
Set the queue name if it does not exist add
$this->_queue->setname ($this->queue_name);
$this->_queue->setflags (amqp_durable | Amqp_autodelete);
echo "Queue status:". $this->_queue->declare ();

echo "<br/>";
Echo ' queue bind: '. $this->_queue->bind ($this->exchange_name, $this->route_key);//bind your queue to Routingkey
Echo ' queue bind: '. $this->_queue->bind ($this->exchange_name, ' route. $TIMESTR)//bind your queue to Routingkey
echo "<br/>";

}

/**
*
* Add information to the queue
*/
Public Function Setrabbitmqinfo ()
{
Post message
$message = Json_encode (Array (' Hello world! ', ' php ', ' C + + '));
$this->_channel->starttransaction ();
echo "Send:". $this->_exchange->publish ($message, $this->route_key); Send your message through the development Routingkey
echo "Send:". $ex->publish ($message, ' route. ') $TIMESTR); Send your message through the development Routingkey
$this->_channel->committransaction ();
$this->_conn->disconnect ();
}

/**
*
* Get the information in the queue
*/
Public Function Getrabbitmqinfo ()
{

$this->_queue->consume (Array ($this, ' processmessage ')); Need to answer manually
while ($this->_queue->declare ()) {
$messages = $this->_queue->get (amqp_autoack);
if ($messages) {
echo "<pre>";
Print_r (Json_decode ($messages->getbody (), true). " \ n ";
}
}

Disconnect
$this->_conn->disconnect ();
}
}


<?php
Set_time_limit (0);
Include_once (' rabbitmq.inc.php ');

$ra = new RABBITMQ ();
$ra->setrabbitmqinfo ();
$ra->getrabbitmqinfo ();

The results are as follows
[root@vpn208 ~]# php test.php
Queue status:0<br/>queue bind:1<br/>send:1<pre>array
(
[0] => Hello world!
[1] => PHP
[2] => C + +
)
command line down, or direct URL access is OK.

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.