PHP消息隊列httpsqs安裝與使用

dy72 9年前發布 | 11K 次閱讀 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: 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'];

?>

 本文由用戶 dy72 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!