Heim > php教程 > php手册 > PHP消息队列httpsqs安装与使用无错版

PHP消息队列httpsqs安装与使用无错版

WBOY
Freigeben: 2016-06-06 19:54:29
Original
2050 Leute haben es durchsucht

项目网址 :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/libeven

项目网址: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: net@s135.com 
This is free software, and you are welcome to modify and redistribute it under the New BSD

License 
 
 
-l   interface to listen on, default is 0.0.0.0 
-p       TCP port number to listen on (default: 1218) 
-x      database directory (example: /opt/httpsqs/data) 
-t    keep-alive timeout for an http request (default: 60) 
-s    the interval to sync updated contents to the disk (default: 5) 
-c       the maximum number of non-leaf nodes to be cached (default: 1024) 
-m      database memory cache size in MB (default: 100) 
-i      save PID in (default: /tmp/httpsqs.pid) 
-a      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/

安装说明

<span># 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 
</span>
Nach dem Login kopieren
<span>如果</span>
Nach dem Login kopieren
<p>运行/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</p><p> </p>
Nach dem Login kopieren
<span># ./configure --enable-httpsqs --with-php-config=/usr/local/php/bin/php-config 
# make && make install</span>
Nach dem Login kopieren
<span>#接着在php.ini中添加一行</span><span> 
extension</span><span>=</span><span>httpsqs</span><span>.</span><span>so</span><span>;</span>
Nach dem Login kopieren

 

函数说明

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

 

对象调用

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

 

示例

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

 

写入

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

读取

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

?>

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage