PHP消息隊列httpsqs安裝與使用

項目網址http://code.google.com/p/httpsqs/
使用文檔http://blog.s135.com/httpsqs/ php

說明:因爲須要安裝的東西有些多,原文可能寫的有些簡略,因此適當補充了 node

 

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 ../  web


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,可 redis

以使數據庫文件突破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
下面進行安裝 json

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 vim


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.測試 tcp

若是安裝成功 執行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 <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'];

?>

相關文章
相關標籤/搜索