Php amqp Message Queue installation Configuration

Source: Internet
Author: User
Tags rabbitmq

1. apt-get install rabbitmq-server

2. service rabbitmq-server restart

3. wget https://github.com/alanxz/rabbitmq-c/tarball/0.2

4. tar zxf 0.2

5. wget https://github.com/rabbitmq/rabbitmq-codegen/tarball/master

6. tar zxf master

7. Music rabbitmq-rabbitmq-codegen-0a95a69/alanxz-rabbitmq-c-f8f4fc7/codegen

8. cd alanxz-rabbitmq-c-f8f4fc7/

9. autoreconf-I &./configure & make install

10. wget http://pecl.php.net/get/amqp-1.0.4.tgz

11. tar zxf amqp-1.0.4.tgz

12./usr/bin/phpize5

13../configure -- with-php-config =/usr/bin/php-config5 -- with-amqp;

14. make & make install

15. vim/etc/php5/apache2/php. ini

16. extension = "amqp. so"

17. service apache2 restart

Configure php. ini in the command line. Otherwise, the AMQPConnection class cannot be found in the command line.

Vim/etc/php5/cli/php. ini

Extension = "amqp. so"


Browser access address:

Http: // localhost: 55672

Http: // localhost: 55672/api/

PORT 4369: Erlang makes use of a Port Mapper Daemon (epmd) for resolution of node names in a cluster. nodes must be able to reach each other and the port mapper daemon for clustering to work.

PORT 35197 set by inet_dist_listen_min/max firewils must permit traffic in this range to pass between clustered nodes

PORT 55672RMQ Management console

PORT 5672RMQ main port.


Add User:

Rabbitmqctl add_user rainbird password

Add permission:

Rabbitmqctl set_permissions-p "/" rainbird ".*"".*"".*"

Delete test user:

Rabbitmqctl delete_user guest


List of all commands (simple English ):

Add_user <UserName> <Password>

Delete_user <UserName>

Change_password <UserName> <NewPassword>

List_users

Add_vhost <VHostPath>

Delete_vhost <VHostPath>

List_vhosts

Set_permissions [-p <VHostPath>] <UserName> <Regexp>

Clear_permissions [-p <VHostPath>] <UserName>

List_permissions [-p <VHostPath>]

List_user_permissions <UserName>

List_queues [-p <VHostPath>] [<QueueInfoItem>...]

List_exchanges [-p <VHostPath>] [<ExchangeInfoItem>...]

List_bindings [-p <VHostPath>]

List_connections [<ConnectionInfoItem>...]


Test code:

Incoming message queue

<? Php
Ini_set ('display _ errors ', 1 );
// Connect to RabbitMQ
$ Conn_args = array ('host' => 'localhost', 'Port' => '123', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/');
$ Conn = new AMQPConnection ($ conn_args );
$ Conn-> connect ();
// Create an exchange name and type
$ Channel = new AMQPChannel ($ conn );
$ Ex = new AMQPExchange ($ channel );
$ Ex-> setName ('exchange _ chenhw ');
$ Ex-> setType (AMQP_EX_TYPE_DIRECT );
$ Ex-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Ex-> declare ();
// Create a queue name. Use exchange to bind the routingkey.
$ Q = new AMQPQueue ($ channel );
$ Q-> setName ('queue _ chen ');
$ Q-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Q-> declare ();
$ Q-> bind ('exchange _ chenhw ', 'routingkey _ chenhw ');
// Message Publishing
$ Channel-> startTransaction ();
$ Message = "insert into 'member' ('name', 'sex') values ('chenhw". rand (0,999). "', 1 )";
Echo $ message;
$ Ex-> publish ($ message, 'routingkey _ chenhw ');
$ Channel-> commitTransaction ();
$ Conn-> disconnect ();
Var_dump ('succ ');
?>

Outbound Message Queue

<? Php
Require_once ('mysql. php ');
// Connect to RabbitMQ
$ Conn_args = array ('host' => 'localhost', 'Port' => '123', 'login' => 'guest ', 'Password' => 'guest ', 'vhost' => '/');
$ Conn = new AMQPConnection ($ conn_args );
$ Conn-> connect ();
// Set the queue name, use exchange, and bind the routingkey
$ Channel = new AMQPChannel ($ conn );
$ Q = new AMQPQueue ($ channel );
$ Q-> setName ('queue _ chenhw ');
$ Q-> setFlags (AMQP_DURABLE | AMQP_AUTODELETE );
$ Q-> declare ();
$ Q-> bind ('exchange _ chenhw ', 'routingkey _ chenhw ');
// Obtain the message
While (true ){
$ Messages = $ q-> get (AMQP_AUTOACK );
If ($ messages ){
$ SQL = $ messages-> getBody ();
Mysql_query ($ SQL, $ db );
}
Sleep (1 );
}
$ Conn-> disconnect ();
Mysql_close ($ db );
Var_dump ('succ ');
?>


RabbitMQ supports message persistence. Message Queue persistence consists of three parts:
1) exchange persistence, specifying durable as true during Declaration
2) the queue is persistent and the durable value is set to true during the declaration.
3) Message persistence. The delivery_mode specified as 21 during delivery is non-persistent)

Code Configuration:

$ Ex-> setFlags (AMQP_DURABLE );

$ Q-> setFlags (AMQP_DURABLE );

$ Ex-> publish ($ message, 'routingkey _ chenhwa ', AMQP_NOPARAM, array ('delivery _ mode' => 2 ));


Effective when rabbitmq-server is restarted

References: http://dc0127.blog.163.com/blog/static/11217896201322394228794/


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.