PHP消息队列httpsqs安装与使用

项目网址:http://code.google.com/p/httpsqs/
使用文档:http://blog.s135.com/httpsqs/

说明:由于需要安装的东西有些多,原文可能写的有些简略,所以适当补充了

 

1.安装libevent-2.0.12-stable.tar.gz
wget http://httpsqs.googlecode.com/files/libevent-2.0.12-stable.tar.gz
tar zxvf libevent-2.0.12-stable.tar.gz
cd libevent-2.0.12-stable/ 
./configure --prefix=/usr/local/libevent-2.0.12-stable/ 
make 
make install 
cd ../ 


2.安装tokyocabinet-1.4.47.tar.gz
wget http://httpsqs.googlecode.com/files/tokyocabinet-1.4.47.tar.gz 
tar zxvf tokyocabinet-1.4.47.tar.gz 
cd tokyocabinet-1.4.47/ 
./configure --prefix=/usr/local/tokyocabinet-1.4.47/ 
#注:在32位Linux操作系统上编译Tokyo cabinet,请使用./configure --enable-off64代替./configure,可

以使数据库文件突破2GB的限制。 
#./configure --enable-off64 --prefix=/usr/local/tokyocabinet-1.4.47/ 
make 
make install 
cd ../ 
当执行第二步骤的会出错,是由于缺少bzip2-1.0.6.tar.gz
下面进行安装

http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz安装开始; 
wget  http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz 
tar -xvzf bzip2-1.0.6.tar.gz 
cd bzip2-1.0.6. 
make 
 make install 
在重复安装tokyocabinet-1.4.47.tar.gz还是失败,由于缺少zlib-1.2.5.tar.gz
下面进行安装

http://www.winimage.com/zLibDll/zlib-1.2.5.tar.gz安装开始; 
wget  http://www.winimage.com/zLibDll/zlib-1.2.5.tar.gz 
tar -xvzf zlib-1.2.5.tar.gz 
cd zlib-1.2.5. 
./configure 
make 
sudo make instal 
继续第2步骤 OK


3.安装httpsqs-1.7.tar.gz

wget http://httpsqs.googlecode.com/files/httpsqs-1.7.tar.gz 
tar zxvf httpsqs-1.7.tar.gz 
cd httpsqs-1.7/ 
make 
make install 
cd ../ 


4.测试

如果安装成功 执行httpsqs –h 会出现
------------------------------------------------------------------------------------------------

-- 
HTTP Simple Queue Service - httpsqs v1.7 (April 14, 2011) 
 
 
Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected] 
This is free software, and you are welcome to modify and redistribute it under the New BSD

License 
 
 
-l <ip_addr>  interface to listen on, default is 0.0.0.0 
-p <num>      TCP port number to listen on (default: 1218) 
-x <path>     database directory (example: /opt/httpsqs/data) 
-t <second>   keep-alive timeout for an http request (default: 60) 
-s <second>   the interval to sync updated contents to the disk (default: 5) 
-c <num>      the maximum number of non-leaf nodes to be cached (default: 1024) 
-m <size>     database memory cache size in MB (default: 100) 
-i <file>     save PID in <file> (default: /tmp/httpsqs.pid) 
-a <auth>     the auth password to access httpsqs (example: mypass123) 
-d            run as a daemon 
-h            print this help and exit 

 

5.设置

存储目录

#mkdir /usr/local/httpsqs_queue

mkdir /home/bf
vi /home/bf/httpsqs.sh
内容如下:
#! /bin/sh
ulimit -SHn 65535

启动方法

带密码

# /usr/bin/httpsqs -d -p 1218 -t 10 -c 10000 -m 512 -x /usr/local/httpsqs_queue -a pwd123

无密码

# /usr/bin/httpsqs -d -p 1218 -t 10 -c 10000 -m 512 -x /usr/local/httpsqs_queue

干掉的方法
# killall httpsqs 

 

注意需要打开防火墙端口

vim /etc/sysconfig/iptables

添加下面一行,打开防火墙1218端口:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 1218 -j ACCEPT

重启防火墙

#/etc/init.d/iptables restart

 

 

6.使用

写队列
http://192.168.2.103:1218/?name=your_queue_name&opt=put&data=TEXT&auth=pwd123

 

读队列
http://192.168.2.103:1218/?charset=utf-8&name=your_queue_name&opt=get&auth=pwd123

 

写队列无密码
http://192.168.2.103:1218/?name=your_queue_name&opt=put&data=TEXT

读队列无密码
http://192.168.2.103:1218/?charset=utf-8&name=your_queue_name&opt=get


 

 

 


写队列
http://192.168.2.103:1218/?name=your_queue_name&opt=put&data=TEXT&auth=pwd123

读队列
http://192.168.2.103:1218/?charset=utf-8&name=your_queue_name&opt=get&auth=pwd123

 

7.php扩展

参考:http://code.google.com/p/php-httpsqs-client/

安装说明

# mkdir php_httpsqs_client 
# cd php_httpsqs_client 
# wget http://php-httpsqs-client.googlecode.com/files/php_httpsqs_client_0.2.1.tar.gz # tar vzxf php_httpsqs_client_0.2.1.tar.gz
# /usr/local/php/bin/phpize 
如果

运行/usr/local/webserver/php/bin/phpize时出现: Configuring for: PHP Api Version:         20041225 Zend Module Api No:      20060613 Zend Extension Api No:   220060519 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. 根据网上的解决办法是: yum install m4 yum install autoconf

 

# ./configure --enable-httpsqs --with-php-config=/usr/local/php/bin/php-config 
# make && make install
#接着在php.ini中添加一行 
extension=httpsqs.so;

 

函数说明

/** 
 * 创建httpsqs连接 
 * @param string $host 服务器地址,可以为空,默认为127.0.0.1 
 * @param int    $port 服务器端口,可以为空,默认为1218 
 * @return resource 
 */ 
$hr = httpsqs_connect("127.0.0.1", 1218);
/** 
 * 写入队列数据 
 * @param resource $hr      服务器连接句柄 
 * @param string   $queue   队列名称 
 * @param string   $data    写入数据 
 * @param string   $charset 字符集,可以为空,默认为utf-8 
 * @return boolean 
 */ 
$putRes = httpsqs_put($hr, "testQueue", "This is a test Data", "UTF-8");
/**  
 * 获取队列最后一条数据 
 * @param resource $hr 
 * @param string   $queue 
 * @param boolean  $return_array 是否返回数组,可以为空,默认为false 
          返回数组格式:array('pos'=>'队列插入点', 'data'=>'数据值') 
 * @param string   $charset 可以为空 
 * @return mixed 
 */ 
$content = httpsqs_get($hr, "testQueue", true, "UTF-8");
/** 
 * 获取队列状态 
 * @param resource $hr 
 * @param string   $queue 
 * @param boolean  $return_json 是否返回状态的json格式,可以为空,默认为false 
 * @return string 
 */ 
$status = httpsqs_status($hr, "testQueue", true);
/** 
 * 获取队列某个点数据 
 * @param resource $hr 
 * @param string   $queue 
 * @param int      $pos 要获取的某条数据的位置 
 * @param string   $charset 可以为空 
 * @return string 
 */ 
$posData = httpsqs_view($hr, "testQueue", 10, "UTF-8");
/** 
 * 队列重置 
 * @param resource $hr 
 * @param string   $queue 
 * @return boolean 
 */ 
$resetRes = httpsqs_reset($hr, "testQueue");
/** 
 * 设置队列最大数据条数 
 * @param resource $hr 
 * @param string   $queue 
 * @param int      $maxqueue 队列最大数据条数 
 * @return boolean 
 */ 
$maxqueueRes = httpsqs_maxqueue($hr, "testQueue", 10000);
/** 
 * 修改定时刷新内存缓冲区内容到磁盘的间隔时间 
 * @param resource $hr 
 * @param string   $queue 
 * @param int      $synctime 间隔时间 
 * @return boolean 
 */ 
$synctimeRes = httpsqs_synctime($hr, "testQueue", 10);

 

对象调用

// 参数与httpsqs_connect对应 
$hr = new HttpSQS($host, $port); 
  // 参数与httpsqs_get对应 
$hr->get($queuename, $return_array, $charset); 
  // 参数与httpsqs_put对应 
$hr->put($queuename, $data, $charset); 
  // 参数与httpsqs_status对应 
$hr->status($queuename, $return_json); 
  // 参数与httpsqs_view对应 
$hr->view($queuename, $pos); 
  // 参数与httpsqs_reset对应 
$hr->reset($queuename); 
  // 参数与httpsqs_maxqueue对应 
$hr->maxqueue($queuename); 
  // 参数与httpsqs_synctime对应 
$hr->synctime($queuename);

 

示例

// 取数据Daemon 
$hr = httpsqs_connect($host, $port);  while (1) { 
    $data = httpsqs_get($hr, $queuename, $charset); 
    if ($data === false) { 
       sleep(1); 
    } else { 
      // do something... 
    }  } 
  // 或者 
$hr = new HttpSQS($host, $port);  while (1) { 
    $data = $hr->get($queuename, $charset); 
    if ($data === false) { 
        sleep(1); 
    } else { 
      // do something... 
    }  } 
  // 写数据 
$hr = httpsqs_connect($hort, $port); 
httpsqs_put($hr, $queuename, $data, $charset); 
  // 或者 
$hr = new HttpSQS($hort, $port); 
$hr->put($queuename, $data, $charset);

 

写入

<?php
$hr = httpsqs_connect("127.0.0.1", 1218);
$putRes = httpsqs_put($hr, "testQueue", "Text", "UTF-8");
?>

读取

<?php
$hr = httpsqs_connect("127.0.0.1", 1218);
$content = httpsqs_get($hr, "testQueue", true, "UTF-8");
//print_r($content);
echo $content['data'];

?>

你可能感兴趣的:(PHP消息队列httpsqs安装与使用)