1. Before installing RABBITMQ first
(1) Install Erlang.:
Erlang: Download the latest Erlang installation package from Erlang's official website http://www.erlang.org/download.html, Linux and MacOSX downloads are r15b01 Source File (72.0 MB I am the Mac OS X system so i download the corresponding version of the installation package directly in the HTTP://WWW.ERLANG-SOLUTIONS.COM/SECTION/132/DOWNLOAD-ERLANG-OTP, the province itself configured and installed the Then unzip the downloaded GZ Pack tar zxcf *.tar.gz CD into the unpacked folder to perform./configure--prefix=/opt/erlang will start compiling the installation compiles to/opt/erlang and executes make and made After the install is compiled, enter/opt/erlang and ERL test whether Erlang is installed successfully. Modify the/etc/profile file to add the following environment variables: #set Erlang Environment Export path= $PATH:/opt/erlang/bin Source profile makes the file effective or:
Yum Install Erlang
(2). Install Python
Yum Install Python-y
(3) installation SimplejsonYum-y Install Xmlto yum-y Install Python-simplejson(4) Installation RABBITMQ
1. Download:
# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-generic-unix-3.4.2.tar.gz
2. Decompression:
# TAR-ZXF Rabbitmq-server-generic-unix-3.4.2.tar.gz
3. Move this directory down to/usr/local and rename it to RABBITMQ:
# MV RABBITMQ-SERVER-GENERIC-UNIX-3.4.2/USR/LOCAL/RABBITMQ
4. Open the/etc/profile file and add the following two rows of environment variables at the end of the file
#set RABBITMQ Environment
Export path= $PATH:/usr/local/rabbitmq/sbin
5. Make Environment variables effective:
# Source/etc/profile
6. Install RABBITMQ Web page Management plugin:
# cd/usr/local/rabbitmq/sbin/
#./rabbitmq-plugin Enable Rabbitmq-management
7. Start RABBITMQ:
# Cd/usr/local/rabbitmq/sbin
#./rabbitmq-server-detached (can be run in the background)
8. See if Startup success: # NETSTAT-TUNLP | grep Beam
TCP 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 3308/beam.smp
TCP 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3308/beam.smp
TCP 0 0::: 5672:::* LISTEN 3308/BEAM.SMP
You can see startup success: 15672 is the RABBIMQ Web Management listening port, 5672 is the port used by the PHP client, enter localhost:15672 in the browser, you can see the following page:
Enter user name guest and password guest can manage RABBITMQ through Web page.
9. Close RABBITMQ: # Cd/usr/local/rabbitmq/sbin then: #./rabbitmqctl stop
(5) PHP client use RABBITMQ Composer is the tool that PHP uses to manage dependency (dependency) relationships. You can declare your dependencies on the External tool Library (libraries) in your project, Composer will help you install these dependent library files, specifically refer to Composer authoritative website: http://www.phpcomposer.com/ 1) Download Composer executable # curl-ss Https://getcomposer.org/installer | php# chmod u+x composer.phar# mv Composer.phar/usr /local/bin/composer2) declares that PHP dependencies create composer.json files under a PHP project that describe the dependencies of the project.
{
"Reqire": {
"Videlalvaro/php-amqplib": "v2.1.0"
}
}
3. Use of ComposerRun the following code in the same directory as the Composer.json file:
# Composer Install
4. In this case, the vendor directory is generated in the same directory as Composer.json, where the PHP dependent libraries specified in the Composer.json file are placed after the download.
# ls
AMQP index.php receive_logs.phpVendor
Application new_task.php receive_logs_topic.php worker.php
Composer.json phpinfo.php send.php
emit_log_direct.php Public test.php
emit_log.php readme.md thinkphp
--------------------------------------------------------------------------------------------------------------- --------
The above build up the use of PHP and RABBITMQ environment, the following is how to use PHP as a client and RABBITMQ communication.
Read this article first: http://blog.csdn.net/sun305355024sun/article/details/41913105
Read these articles again: http://blog.csdn.net/wind_324/article/category/2165209
At the official website of Hello World This article when send.php and receive.php code may have errors, this time, can be generated from the Composer.json file based on the dependent library vendor/videlalvaro/ Php-amqplib/demo directory to find amqp_publisher.php and amqp_consumer.php These two demo files, copied to the/usr/local/apache/htdocs directory, Do not use the official web send.php and receive.php files.
Open the amqp_publisher.php file for modification with the following modified code:
<?php include (__dir__.
'/vendor/autoload.php ');
Use phpamqplib\connection\amqpconnection;
Use Phpamqplib\message\amqpmessage;
Define (' HOST ', ' 127.0.0.1 ');
Define (' PORT ', 5672);
Define (' USER ', ' guest ');
Define (' Pass ', ' guest ');
Define (' Vhost ', '/');
$exchange = ' router ';
$queue = ' msgs ';
$conn = new Amqpconnection (HOST, PORT, USER, pass, vhost);
$ch = $conn->channel ();
/* The following code is the same both the consumer and the producer.
In this way we are sure we always have a \ A queue to consume and a exchange where to publish. */* Name: $queue passive:false durable:true//The queue would survive server restarts Exclusive:false The queue can be accessed in other channels Auto_delete:false//the queue won ' t be deleted once the channel is CL
osed.
*/$ch->queue_declare ($queue, False, True, false, false); /* Name: $exchange type:direct passive:false durable:true//The exchange would survive server resTarts Auto_delete:false//the exchange won ' t be deleted once the channel of the IS closed.
* * $ch->exchange_declare ($exchange, ' direct ', false, True, false);
$ch->queue_bind ($queue, $exchange);
$msg _body = Implode ("", Array_slice ($ARGV, 1));
$msg = new Amqpmessage ($msg _body, Array (' Content_Type ' => ' text/plain ', ' Delivery_mode ' => 2));
$ch->basic_publish ($msg, $exchange);
$ch->close ();
$conn->close ();
Amqp_consumer.php the modified code:
<?php include (__dir__.
'/vendor/autoload.php ');
Use phpamqplib\connection\amqpconnection;
Define (' HOST ', ' 127.0.0.1 ');
Define (' PORT ', 5672);
Define (' USER ', ' guest ');
Define (' Pass ', ' guest ');
Define (' Vhost ', '/');
$exchange = ' router ';
$queue = ' msgs ';
$consumer _tag = ' consumer ';
$conn = new Amqpconnection (HOST, PORT, USER, pass, vhost);
$ch = $conn->channel ();
/* The following code is the same both the consumer and the producer.
In this way we are sure we always have a \ A queue to consume and a exchange where to publish. */* Name: $queue passive:false durable:true//The queue would survive server restarts Exclusive:false The queue can be accessed in other channels Auto_delete:false//the queue won ' t be deleted once the channel is CL
osed.
*/$ch->queue_declare ($queue, False, True, false, false);
/* Name: $exchange type:direct passive:false durable:true//The exchange would survive server restarts Auto_delete:false//the exchange won ' t be deleted once the channel is closed.
* * $ch->exchange_declare ($exchange, ' direct ', false, True, false);
$ch->queue_bind ($queue, $exchange);
function Process_message ($msg) {echo \ \ n--------\ n];
Echo $msg->body;
echo "\ n--------\ n"; <pre name= "code" class= "PHP" > $msg->delivery_info[' Channel ']-> basic_ack ($msg->delivery_info[' de
Livery_tag ']);
Send a message has the string "quit" to cancel the consumer. if ($msg->body = = ' quit ') {$msg->delivery_info[' channel ']-> basic_cancel ($msg->delivery_
info[' Consumer_tag ']); }/* Queue:queue from where to get the messages Consumer_tag:consumer identifier No_local:don ' t receive
Messages published by this consumer.
No_ack:tells the server if the consumer would acknowledge the messages. Exclusive:request Exclusive consumer access, meaning only this consumer can access the queue NowaIT:CALLBACK:A PHP Callback */$ch->basic_consume ($queue, $consumer _tag, False, False, False, false, ' process_mes
Sage ');
function shutdown ($ch, $conn) {$ch->close ();
$conn->close ();
} register_shutdown_function (' Shutdown ', $ch, $conn);
Loop as long as the channel has callbacks registered while (count ($ch->callbacks)) {$ch->wait ();}
Test:
Turn on consumer listening:
[Root@ip10 htdocs]# php amqp_consumer.php
Publisher Post message:
[Root@ip10 htdocs]# php amqp_publisher.php ' LSI ';
[Root@ip10 htdocs]# php amqp_publisher.php ' Zhangsan '
You can see the listener print out these two messages:
[Root@ip10 htdocs]# php amqp_consumer.php
--------
Lsi
--------
--------
Zhangsan
--------
You can see a msgs queue (just created) in the LOCALHOST:15672 Web page management system and see total as 2 in messages because of the two messages we just posted, so there are 2 messages in this queue.