RABBITMQ source Installation and configuration

Source: Internet
Author: User
Tags autoload json php file php and composer install rabbitmq

1. Before installing RABBITMQ, first

(1) Install Erlang.:

Download Erlang: Download the latest Erlang installation package from Erlang's official website http://www.erlang.org/download.html, the version of Linux and MacOSX download is r15b01 Source File (72.0 MB My is Mac OSX system so i downloaded the corresponding version of the installation package directly in HTTP://WWW.ERLANG-SOLUTIONS.COM/SECTION/132/DOWNLOAD-ERLANG-OTP, the province's own configuration and installed Then unzip the downloaded GZ package tar zxcf *.tar.gz CD into the extracted folder to execute./configure--prefix=/opt/erlang will start compiling the installation will compile to/opt/erlang and execute make After the install is completed, enter/opt/erlang and enter the ERL test Erlang to see if the installation was successful. Modify the/etc/profile file to add the following environment variable: #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. Unzip:
# TAR-ZXF Rabbitmq-server-generic-unix-3.4.2.tar.gz
3. Move this directory to/usr/local and rename to RABBITMQ:
# MV RABBITMQ-SERVER-GENERIC-UNIX-3.4.2/USR/LOCAL/RABBITMQ
4. Open the/etc/profile file and add the following two line environment variables at the end of the file
#set RABBITMQ Environment
Export path= $PATH:/usr/local/rabbitmq/sbin
5. Make the environment variable effective:
# Source/etc/profile
6. Install the RABBITMQ Web 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 is successful: # 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 the 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 the username guest and password guest to manage RABBITMQ via Web page.
9. Close RABBITMQ: # Cd/usr/local/rabbitmq/sbin then: #./rabbitmqctl stop

(5) PHP client using RABBITMQ Composer is a tool that PHP uses to manage dependency (dependency) relationships. You can declare your dependent external tool library (libraries) in your project, Composer will help you install these dependent library files, 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 a Composer.json file under a PHP project, describing the dependencies of the project.

{
"Reqire": {
"Videlalvaro/php-amqplib": "v2.1.0"
}
}


3. Using ComposerRun the following code in the same directory as the Composer.json file:

# Composer Install

4. At this time, the vendor directory is generated in the same directory as Composer.json, which is the location of the download of the PHP dependent library specified in the Composer.json file.
# 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 set 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 this article again: http://blog.csdn.net/wind_324/article/category/2165209


The send.php and receive.php codes may have errors when you introduce Hello World on the official website, and this time, you can get from the dependent libraries that you just generated from the Composer.json file vendor/videlalvaro/ Php-amqplib/demo directory to find the amqp_publisher.php and amqp_consumer.php the two demo files, copied to the/usr/local/apache/htdocs directory, Do not use send.php and receive.php files on the official website.
Open the amqp_publisher.php file for modification, and the modified code is as follows:

<?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 in the consumer and the producer.
In the This is sure we always has a queue to consume from and an exchange where to publish messages.  */* Name: $queue passive:false durable:true//The queue would survive server restarts Exclusive:false The queue can be accessed on other channels Auto_delete:false//the queue won ' t being 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);
$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 in the consumer and the producer.
In the This is sure we always has a queue to consume from and an exchange where to publish messages.  */* Name: $queue passive:false durable:true//The queue would survive server restarts Exclusive:false The queue can be accessed on other channels Auto_delete:false//the queue won ' t being 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 with 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 a callbacks registered while (count ($ch->callbacks)) {$ch->wait ();}



Test it:

Turn on consumer monitoring:
[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 that the listener has printed out these two messages:
[Root@ip10 htdocs]# php amqp_consumer.php

--------
Lsi
--------

--------
Zhangsan
--------

In the localhost:15672 Web management system you can see a queue of msgs (just created) and see total as 2 in messages because of the two messages we just released, so there are 2 messages in this queue.






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.