Skip to content


windows下安装rabbitmq及支持apache+php-ampq扩展,nginx+php-amqplib扩展


一.安装erlang拓展

因为RabbitMQ是由erlang语言实现的,所以先要安装erlang环境

erlang 下载安装 http://www.erlang.org/downloads
http://erlang.org/download/otp_win64_21.3.exe
rabbitmq 下载安装 https://www.rabbitmq.com/download.html
https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.9.exe
php的amqp扩展下载地址:http://pecl.php.net/package/amqp

erlang和rabbitmq_server直接下载安装包安装;
amqp拓展如果web服务器是apache,可以用amqp,nginx可以用compose下的php-amqplib

二.安装amqp


amqp拓展下载要选好版本;根据系统选择32位还是64位的。查看phpInfo中:Thread Safety:如果是enabled就选ts版。disabled选择nts版。
安装拓展方法:

下载解压;将php_amqp.dll复制到php/ext;
php.ini中加上extension=php_amqp.dll;

复制rabbitmq.1.dll 到php/
apache 修改http.conf 文件 添加

LoadFile “D:/wnmp/php/rabbitmq.1.dll”

重启apache,phpinfo()中既可以看到amqp拓展了;

amqp
Version 1.4.0
Revision $Revision: 327551 $
Compiled May 22 2014 @ 16:57:32
AMQP protocol version 0-9-1
librabbitmq version 0.5.0
Directive Local Value Master Value
amqp.auto_ack 0 0
amqp.connect_timeout 0 0
amqp.host localhost localhost
amqp.login guest guest
amqp.password guest guest
amqp.port 5672 5672
amqp.prefetch_count 3 3
amqp.read_timeout 0 0
amqp.timeout no value no value
amqp.vhost / /
amqp.write_timeout 0 0

如果apache没装dll,可能会报以下错误
Fatal error: Uncaught exception ‘AMQPConnectionException’ with message ‘Socket error: could not connect to host.’

Fatal error: Uncaught exception ‘AMQPConnectionException’ with message ‘Library error: a socket error occurred – Potential login failure.’

三.安装compose下的php-amqplib

Composer 需要 PHP 5.3.2+ 才能运行。

windows下安装
https://getcomposer.org/Composer-Setup.exe

如有需要可以安装git
http://git-scm.com/download/win

php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
php -r “if (hash_file(‘sha384’, ‘composer-setup.php’) === ’48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5′) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”
php composer-setup.php
php -r “unlink(‘composer-setup.php’);”

这个命令会将 composer.phar 下载到当前目录。PHAR(PHP 压缩包)是一个压缩格式,可以在命令行下直接运行。

php composer.phar
这将返回给你一个可执行的命令列表。

创建一个composer.json
d:/wnmp/composer/composer.json
{ “require”: { “monolog/monolog”: “1.0.*”, “php-amqplib/php-amqplib”: “>=2.6.1” } }

php composer.phar up

编辑php.ini
将d:/wnmp/composer 放入include_path和 open_basedir

四.rabbitmq

进入sbin目录cmd执行rabbitmq-service.bat start

进入sbin目录cmd执行rabbitmqctl.bat status
如果出现以下的图,说明安装是成功的,并且说明现在
RabbitMQ Server 已经启动了,运行正常
Status of node rabbit@QFNO88I … [{pid,2208}, {running_applications, [{rabbit,”RabbitMQ”,”3.7.9″}, {rabbit_common, “Modules shared by rabbitmq-server and rabbitmq-erlang-client”, “3.7.9”}, {ranch_proxy_protocol,”Ranch Proxy Protocol Transport”,”2.1.1″}, {ranch,”Socket acceptor pool for TCP protocols.”,”1.6.2″}, {ssl,”Erlang/OTP SSL application”,”9.1″}, {public_key,”Public key infrastructure”,”1.6.4″}, {asn1,”The Erlang ASN1 compiler version 5.0.8″,”5.0.8″}, {mnesia,”MNESIA CXC 138 12″,”4.15.5″}, {crypto,”CRYPTO”,”4.4″}, {os_mon,”CPO CXC 138 46″,”2.4.7″}, {xmerl,”XML parser”,”1.3.18″}, {inets,”INETS CXC 138 49″,”7.0.3″}, {jsx,”a streaming, evented json parsing toolkit”,”2.9.0″}, {recon,”Diagnostic tools for production use”,”2.3.6″}, {lager,”Erlang logging framework”,”3.6.5″}, {goldrush,”Erlang event stream processor”,”0.1.9″}, {compiler,”ERTS CXC 138 10″,”7.3″}, {syntax_tools,”Syntax tools”,”2.1.6″}, {sasl,”SASL CXC 138 11″,”3.3″}, {stdlib,”ERTS CXC 138 10″,”3.7″}, {kernel,”ERTS CXC 138 10″,”6.2″}]}, {os,{win32,nt}}, {erlang_version, “Erlang/OTP 21 [erts-10.2] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64]\n”}, {memory, [{connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,0}, {queue_procs,0}, {queue_slave_procs,0}, {plugins,13724}, {other_proc,23284296}, {metrics,195772}, {mgmt_db,0}, {mnesia,73736}, {other_ets,2460472}, {binary,211248}, {msg_index,29824}, {code,24360636}, {atom,1082561}, {other_system,10339171}, {allocated_unused,14166928}, {reserved_unallocated,0}, {strategy,rss}, {total,[{erlang,62051440},{rss,76218368},{allocated,76218368}]}]}, {alarms,[]}, {listeners,[{clustering,25672,”::”},{amqp,5672,”::”},{amqp,5672,”0.0.0.0″}]}, {vm_memory_calculation_strategy,rss}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,3415903436}, {disk_free_limit,50000000}, {disk_free,42944823296}, {file_descriptors, [{total_limit,8092}, {total_used,2}, {sockets_limit,7280}, {sockets_used,0}]}, {processes,[{limit,1048576},{used,230}]}, {run_queue,1}, {uptime,29084}, {kernel,{net_ticktime,60}}]

如果没有出现的话在执行

  1. rabbitmqservice.bat install
  2. rabbitmqservice.bat start,如果是关闭的话就是rabbitmqservice.bat stop

显示管理界面
1.进入RabbitMQ的sbin目录 cmd执行
2.rabbitmq-plugins.bat list (查看所有插件)
3.rabbitmq-plugins.bat enable rabbitmq_management (启动后台管
理界面插件)

.\rabbitmq-plugins.bat list

Listing plugins with pattern “.*” …
Configured: E = explicitly enabled; e = implicitly enabled
| Status: * = running on rabbit@QFNO88I
|/
[ ] rabbitmq_amqp1_0 3.7.9
[ ] rabbitmq_auth_backend_cache 3.7.9
[ ] rabbitmq_auth_backend_http 3.7.9
[ ] rabbitmq_auth_backend_ldap 3.7.9
[ ] rabbitmq_auth_mechanism_ssl 3.7.9
[ ] rabbitmq_consistent_hash_exchange 3.7.9
[ ] rabbitmq_event_exchange 3.7.9
[ ] rabbitmq_federation 3.7.9
[ ] rabbitmq_federation_management 3.7.9
[ ] rabbitmq_jms_topic_exchange 3.7.9
[ ] rabbitmq_management 3.7.9
[ ] rabbitmq_management_agent 3.7.9
[ ] rabbitmq_mqtt 3.7.9
[ ] rabbitmq_peer_discovery_aws 3.7.9
[ ] rabbitmq_peer_discovery_common 3.7.9
[ ] rabbitmq_peer_discovery_consul 3.7.9
[ ] rabbitmq_peer_discovery_etcd 3.7.9
[ ] rabbitmq_peer_discovery_k8s 3.7.9
[ ] rabbitmq_random_exchange 3.7.9
[ ] rabbitmq_recent_history_exchange 3.7.9
[ ] rabbitmq_sharding 3.7.9
[ ] rabbitmq_shovel 3.7.9
[ ] rabbitmq_shovel_management 3.7.9
[ ] rabbitmq_stomp 3.7.9
[ ] rabbitmq_top 3.7.9
[ ] rabbitmq_tracing 3.7.9
[ ] rabbitmq_trust_store 3.7.9
[ ] rabbitmq_web_dispatch 3.7.9
[ ] rabbitmq_web_mqtt 3.7.9
[ ] rabbitmq_web_mqtt_examples 3.7.9
[ ] rabbitmq_web_stomp 3.7.9
[ ] rabbitmq_web_stomp_examples 3.7.9

.\rabbitmq-plugins.bat enable rabbitmq_management

Enabling plugins on node rabbit@QFNO88I:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@QFNO88I…
The following plugins have been enabled:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch

started 3 plugins.

浏览器输入:127.0.0.1:15672

输入用户名和密码,初始化的用户名和密码是 guest: guest
注意: guest只能在localhost登录

手动添加账号
a) 查看所有账号 rabbitmqctl.bat list_users
b) 创建账号 rabbitmqctl.bat add_user
e) 设置角色 rabbitmqctl.bat set_user_tags keving administrator
f) 设置权限, rabbitmqctl.bat set_permissions –p / “.” “.” “.*”
/代表 vhost, 后面是权限在即可以 配置、写、读权限

./rabbitmqctl.bat add_user user1 user1
添加个vhost yjs

五.测试

打开send.php文件:

<?php

require_once DIR . ‘/vendor/autoload.php’;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

// RabbitMQ 默认端口5672, 默认账号密码: guest / guest
// $host = ‘localhost’;
$host = ‘192.168.99.100’;
$port = ‘5672’;
$username = ‘guest’;
$password = ‘guest’;

// $connection = new AMQPStreamConnection(‘localhost’, 5672, ‘guest’, ‘guest’);
$connection = new AMQPStreamConnection($host, $port, $username, $password);
$channel = $connection->channel();
$channel->queue_declare(‘hello’, false, false, false, false);

$msg = new AMQPMessage(‘Hello World!’);
$channel->basic_publish($msg, ”, ‘hello’);
echo ” [x] Sent ‘Hello World!’\n”;

$channel->close();
$connection->close();

打开 receive.php 文件:

<?php
// file: receive.php
require_once DIR . ‘/vendor/autoload.php’;
use PhpAmqpLib\Connection\AMQPStreamConnection;

// RabbitMQ 默认端口5672, 默认账号密码: guest / guest
// $host = ‘localhost’;
// $host = ‘5672’;
$host = ‘192.168.99.100’;
$port = ‘5672’;
$username = ‘guest’;
$password = ‘guest’;

$connection = new AMQPStreamConnection($host, $port, $username, $password);
$channel = $connection->channel();

$queueName = ‘hello-test’;
$channel->queue_declare($queueName, false, false, false, false);

echo ‘ [*] Waiting for messages. To exit press CTRL+C’, “\n”;

$callback = function($msg) {
echo ” [x] Received: {“, $msg->body, “}\n”;
};

$channel->basic_consume($queueName, ”, false, true, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}

发送和结束队列消息
先执行receive.php.
再执行send.php文件,来发送消息:

$ php send.php

[x]

Sent: {18-02-10 02:35:15: Hello World!}
$ php send.php

[x]

Sent: {18-02-10 02:35:34: Hello World!}

https://docs.phpcomposer.com/01-basic-usage.html
http://www.rabbitmq.com/tutorials/tutorial-one-php.html
php-amqplib amqp-ext 性能对比
https://blog.forma-pro.com/php-amqp-clients-benchmark-them-all-8a4e6adb1a6b


https://blog.forma-pro.com/php-amqp-clients-benchmark-them-all-8a4e6adb1a6b

RabbitMQ的用户角色分类:


none、management、policymaker、monitoring、administrator

RabbitMQ各类角色描述:
none
不能访问 management plugin

management
用户可以通过AMQP做的任何事外加:
列出自己可以通过AMQP登入的virtual hosts
查看自己的virtual hosts中的queues, exchanges 和 bindings
查看和关闭自己的channels 和 connections
查看有关自己的virtual hosts的“全局”的统计信息,包含其他用户在这些virtual hosts中的活动。

policymaker
management可以做的任何事外加:
查看、创建和删除自己的virtual hosts所属的policies和parameters

monitoring
management可以做的任何事外加:
列出所有virtual hosts,包括他们不能登录的virtual hosts
查看其他用户的connections和channels
查看节点级别的数据如clustering和memory使用情况
查看真正的关于所有virtual hosts的全局的统计信息

administrator
policymaker和monitoring可以做的任何事外加:
创建和删除virtual hosts
查看、创建和删除users
查看创建和删除permissions
关闭其他用户的connections

创建用户并设置角色:
可以创建管理员用户,负责整个MQ的运维,例如:
$sudo rabbitmqctl add_user user_admin passwd_admin
赋予其administrator角色:
$sudo rabbitmqctl set_user_tags user_admin administrator

可以创建RabbitMQ监控用户,负责整个MQ的监控,例如:
$sudo rabbitmqctl add_user user_monitoring passwd_monitor
赋予其monitoring角色:
$sudo rabbitmqctl set_user_tags user_monitoring monitoring

可以创建某个项目的专用用户,只能访问项目自己的virtual hosts
$sudo rabbitmqctl add_user user_proj passwd_proj
赋予其monitoring角色:
$sudo rabbitmqctl set_user_tags user_proj management

创建和赋角色完成后查看并确认:
$sudo rabbitmqctl list_users

RabbitMQ 权限控制
默认virtual host:”/”
默认用户:guest
guest具有”/”上的全部权限,仅能有localhost访问RabbitMQ包括Plugin,建议删除或更改密码。可通过将配置文件中loopback_users置孔来取消其本地访问的限制:
[{rabbit, [{loopback_users, []}]}]

用户仅能对其所能访问的virtual hosts中的资源进行操作。这里的资源指的是virtual hosts中的exchanges、queues等,操作包括对资源进行配置、写、读。配置权限可创建、删除、资源并修改资源的行为,写权限可向资源发送消息,读权限从资源获取消息。比如:
exchange和queue的declare与delete分别需要exchange和queue上的配置权限
exchange的bind与unbind需要exchange的读写权限
queue的bind与unbind需要queue写权限exchange的读权限
发消息(publish)需exchange的写权限
获取或清除(get、consume、purge)消息需queue的读权限

对何种资源具有配置、写、读的权限通过正则表达式来匹配,具体命令如下:
set_permissions [-p ]
其中, 的位置分别用正则表达式来匹配特定的资源,如’^(amq.gen.*|amq.default)$’可以匹配server生成的和默认的exchange,’^$’不匹配任何资源

需要注意的是RabbitMQ会缓存每个connection或channel的权限验证结果、因此权限发生变化后需要重连才能生效。

为用户赋权:
$sudo rabbitmqctl set_permissions -p /vhost1 user_admin ‘.’ ‘.’ ‘.*’
该命令使用户user_admin具有/vhost1这个virtual host中所有资源的配置、写、读权限以便管理其中的资源

按用户查看权限:

$sudo rabbitmqctl list_user_permissions user_admin

按服务器查看权限

$sudo rabbitmqctl list_permissions -p /vhost1

Posted in 技术, 消息rabbitmq.

Tagged with , , , , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.