本次测试环境
系统:CentOS5.8 x86-64
PHP:5.2.16(源码编译安装的)
扩展是C写的,由于C与RabbitMQ通信一般需要依赖rabbitmq-c库(也就是librabbitmq),所以编译扩展前需要先装依赖库。
原文是用hg工具,需要装mercurial(一个轻量级的版本控制系统,可以直接用yum install hg),这里就不介绍了,我们还是用传统的方法。
在网上找了很多方法,最后没有一个很详细的,此处将主要步骤做了一次细化,大部分原因是缺少相关的安装包
1.配置yum的安装地址
[root amqp]# wget -O /etc/yum.repos.d/epel-erlang.repo http://repos.fedorapeople.org/repos/peter/erlang/epel-erlang.repo
[root amqp]# rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
2.安装librabbitmq,建议通过yum的方式的安装,否则后面将会非常的痛苦
[root amqp]# yum install librabbitmq
3.下载rabbitmq-c的v0.2版:(为什么0.2版?好吧,我也不知道,总之它的各版本兼容性非常不好,经过我屡试屡败屡败试……发现0.2版还算顺利--)
https://github.com/alanxz/rabbitmq-c/tags
或者直接shell:(注意:此处要求你的系统已经安装了openssl,否则就会下载失败,如果没有安装 yum install openssl)
[root amqp]# wget https://github.com/alanxz/rabbitmq-c/tarball/0.2
[root amqp]# tar zxvf 0.2
[root amqp]# mv alanxz-rabbitmq-c-* rabbitmq-c
[root amqp]# cd rabbitmq-c
4.到这里下载最新版的codegen配件:
https://github.com/rabbitmq/rabbitmq-codegen/downloads
或者直接shell:(注意现在所在目录,下载的文件或者压缩包必须放到rabbitmq-c的目录中的codegen目录中)
[root rabbitmq-c]# wget https://github.com/rabbitmq/rabbitmq-codegen/tarball/master
[root rabbitmq-c]# tar zxvf master
[root rabbitmq-c]# mv rabbitmq-rabbitmq-codegen-* codegen
5.按照手册介绍进行:
[root rabbitmq-c]# autoreconf -i
[root rabbitmq-c]# ./configure && make && make install
如果此步没有报错,恭喜你,基本成功了,不过大部分情况下你或许和我一样遇到各种各样报错信息,参考最后的错误,这里先把编译扩展顺带说完。
6.直接回到官方下载(我下载的是1.0.9版,此处用的是截止发blog时的最新版本)
http://pecl.php.net/package/amqp
或者shell:(注意:此时的目录不再是rabbitmq-c了)
[root amqp]# wget http://pecl.php.net/get/amqp-1.0.9.tgz
[root amqp]# tar zxvf amqp-1.0.9.tgz
[root amqp]# cd amqp-1.0.9
[root amqp-1.0.9]# /opt/webserver/php/bin/phpize
[root amqp-1.0.9]# ./configure --with-php-config=/opt/webserver/php/bin/php-config --with-amqp
[root amqp-1.0.9]# make && make install
[root amqp-1.0.9]# vim /opt/webserver/php/etc/php.ini
将amqp.so 加入到 extension_dir下一行,结果如下
extension_dir = "/opt/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "amqp.so"
演示代码
登录名、密码是在服务端添加好的
代码参考官方进行修改
<?php
$channelName="myisccchange";
$channelType="fanout";
$queueName="myisccqueue";
$key="myiscckey";
$text = "我是陈聪,测试一下中文";
$connection = new AMQPConnection();
$connection->setLogin("cc");
$connection->setPassword("123456");
$connection->setVhost("/cc");
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :(' . PHP_EOL);
}
// Open Channel
$channel = new AMQPChannel($connection);
// Declare exchange
$exchange = new AMQPExchange($channel);
$exchange->setName($channelName);
$exchange->setType($channelType);
$exchange->declare();
// Create Queue
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->declare();
$message = $exchange->publish($text.time(), $key);
if (!$message) {
echo 'Message not sent', PHP_EOL;
} else {
echo 'Message sent!', PHP_EOL;
}
?>
<?php
$channelName="myisccchange";
$channelType="fanout";
$queueName="myisccqueue";
$key="myiscckey";
$connection = new AMQPConnection();
$connection->setLogin("cc");
$connection->setPassword("123456");
$connection->setVhost("/cc");
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :('. PHP_EOL);
}
// Open channel
$channel = new AMQPChannel($connection);
// Open Queue and bind to exchange
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->bind($channelName, $key);
$queue->declare();
// Prevent message redelivery with AMQP_AUTOACK param
while(true){
while ($envelope = $queue->get(AMQP_AUTOACK)) {
echo ($envelope->isRedelivery()) ? 'Redelivery' : 'New Message';
echo PHP_EOL;
echo $envelope->getBody(), PHP_EOL;
}
sleep(5);
}
?>
安装过程中的错误
好了,如果你和我一样悲催,从autoreconf -i 就开始玩命报错,那么我这里提供一些可能有用的处理方法(部分是实际遇到的,部分是从网上查找的)
1.autoreconf 时有可能遇到如下报错:
场景一:
错误描述:onfigure.ac:60: error: possibly undefined macro: AS_CASE
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
configure.ac:69: error: possibly undefined macro: AS_IF
configure.ac:92: error: possibly undefined macro: AC_PATH_PROGS_FEATURE_CHECK
configure.ac:97: error: possibly undefined macro: AC_MSG_RESULT
configure:2907: error: possibly undefined macro: m4_foreach_w
autoreconf: /usr/bin/autoconf failed with exit status: 1
解决办法:奇怪的是,当我再次执行却报不同的错,网上有人也是这样....
场景二:
错误描述:configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
configure.ac:34: installing `./config.guess'
configure.ac:34: installing `./config.sub'
Makefile.am:3: Libtool library used but `LIBTOOL' is undefined
Makefile.am:3:
Makefile.am:3: The usual way to define `LIBTOOL' is to add `AC_PROG_LIBTOOL'
Makefile.am:3: to `configure.ac' and run `aclocal' and `autoconf' again.
Makefile.am: C objects in subdir but `AM_PROG_CC_C_O' not in `configure.ac'
Makefile.am: installing `./compile'
Makefile.am: installing `./depcomp'
autoreconf: automake failed with exit status: 1
解决办法:首先确保一些必要的依赖库是否已安装,如:libtool、autoconf、automake 等
这个大家自行google下载并编译安装,常规操作,没啥说的,这里我直接yum了:
yum install libtool autoconf automake
看下libtoolize在哪:
whereis libtoolize
显示例如:libtoolize: /usr/bin/libtoolize,则可执行一下
/usr/bin/libtoolize
还有可能会出现这种错:
/usr/share/aclocal/libmcrypt.m4:17: warning: underquoted definition of AM_PATH_LIBMCRYPT
/usr/share/aclocal/libmcrypt.m4:17: run info '(automake)Extending aclocal'
/usr/share/aclocal/libmcrypt.m4:17: or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
解决办法如下:
编辑:/usr/share/aclocal/libmcrypt.m4
修改17行的:AC_DEFUN(AM_PATH_LIBMCRYPT, 为 AC_DEFUN([AM_PATH_LIBMCRYPT],
场景三:
错误描述:/usr/bin/libtoolize 时可能会报的错:
libtoolize: putting auxiliary files in `.'.
libtoolize: linking file `./ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.ac and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
解决办法:报错很明确,按照提示操作即可:
编辑:configure.ac
添加一行:AC_CONFIG_MACRO_DIR([m4])
重新执行:/usr/bin/libtoolize
编辑:Makefile.am
添加一行:ACLOCAL_AMFLAGS=-I m4
场景四:
错误描述:./configure 时可能会报的错:
checking location of AMQP codegen directory... configure: error: could not find AMQP spec file at "'/codegen/amqp-0.9.1.json'"
解决办法:configure文件中目录与实际情况不符,虽然可以通过手动修改解决,但不建议,其实应该是rabbitmq-c和codegen的版本不匹配造成的,更换版本。
场景五:
错误内容:configure: error: could not find a python that can 'import simplejson'
解决办法:请安装 python 的 simplejson 库:
(因为simplejson是最新版本,所以python的版本要在2.5以上,或者下载一个低版本的simplejson,例如python2.4对应的版本 simplejson- 2.0.0就可以用,如果没有安装python或者版本不够 yum install python)
[root amqp]# wget http://pypi.python.org/packages/source/s/simplejson/simplejson-3.0.7.tar.gz
[root amqp]# tar zxf simplejson-3.0.7.tar.gz
[root amqp]# cd simplejson-3.0.7
[root amqp]# python setup.py install