打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
轻量级简单队列服务HTTPSQS安装与使用

http://blog.csdn.net/unix21/article/details/8750303

2013

原文地址:http://blog.s135.com/httpsqs

1.安装

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

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

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


性能调优(可选

#ulimit -SHn 65535


2.启动

#httpsqs -d -p 1218 -x /data0/queue

请使用命令“killall httpsqs”、“pkill httpsqs”和“kill `cat /tmp/httpsqs.pid`”来停止httpsqs。

  注意:请不要使用命令“pkill -9 httpsqs”和“kill -9 httpsqs的进程ID”来结束httpsqs,否则,内存中尚未保存到磁盘的数据将会丢失。


3.调用


(1)、入队列(将文本消息放入队列):

  HTTP GET 协议(以curl命令为例):

curl "http://host:port/?name=your_queue_name&opt=put&data=经过URL编码的文本消息&auth=mypass123"


  HTTP POST 协议(以curl命令为例):
curl -d "经过URL编码的文本消息" "http://host:port/?name=your_queue_name&opt=put&auth=mypass123"

(2)、出队列(从队列中取出文本消息):

  HTTP GET 协议(以curl命令为例):

curl "http://host:port/?charset=utf-8&name=your_queue_name&opt=get&auth=mypass123"


curl "http://host:port/?charset=gb2312&name=your_queue_name&opt=get&auth=mypass123"


httpsqs_client.php

  1. <?php   
  2. /* ----------------------------------------------------------------------------------------------------------------  
  3. HTTP Simple Queue Service - httpsqs client class for PHP v1.7.1   
  4. Author: Zhang Yan (http://blog.s135.com), E-mail: net@s135.com  
  5. This is free software, and you are welcome to modify and redistribute it under the New BSD License  
  6. ----------------------------------------------------------------------------------------------------------------  
  7. Useage:  
  8. <?php include_once("httpsqs_client.php");  
  9. $httpsqs = new httpsqs($httpsqs_host, $httpsqs_port, $httpsqs_auth, $httpsqs_charset);  
  10. $result = $httpsqs->put($queue_name, $queue_data); //1. PUT text message into a queue. If PUT successful, return boolean: true. If an error occurs, return boolean: false. If queue full, return text: HTTPSQS_PUT_END  
  11. $result = $httpsqs->get($queue_name); //2. GET text message from a queue. Return the queue contents. If an error occurs, return boolean: false. If there is no unread queue message, return text: HTTPSQS_GET_END  
  12. $result = $httpsqs->gets($queue_name); //3. GET text message and pos from a queue. Return example: array("pos" => 7, "data" => "text message"). If an error occurs, return boolean: false. If there is no unread queue message, return: array("pos" => 0, "data" => "HTTPSQS_GET_END")  
  13. $result = $httpsqs->status($queue_name); //4. View queue status  
  14. $result = $httpsqs->status_json($queue_name); //5. View queue status in json. Return example: {"name":"queue_name","maxqueue":5000000,"putpos":130,"putlap":1,"getpos":120,"getlap":1,"unread":10}  
  15. $result = $httpsqs->view($queue_name, $queue_pos); //6. View the contents of the specified queue pos (id). Return the contents of the specified queue pos.  
  16. $result = $httpsqs->reset($queue_name); //7. Reset the queue. If reset successful, return boolean: true. If an error occurs, return boolean: false  
  17. $result = $httpsqs->maxqueue($queue_name, $num); //8. Change the maximum queue length of per-queue. If change the maximum queue length successful, return boolean: true. If  it be cancelled, return boolean: false  
  18. $result = $httpsqs->synctime($num); //9. Change the interval to sync updated contents to the disk. If change the interval successful, return boolean: true. If  it be cancelled, return boolean: false  
  19. ?>  
  20. ---------------------------------------------------------------------------------------------------------------- */    
  21. class httpsqs {           
  22. public $httpsqs_host;           
  23. public $httpsqs_port;           
  24. public $httpsqs_auth;           
  25. public $httpsqs_charset;                    
  26. public function __construct() {       
  27. $host='192.168.20.59';$port=1218; $auth=''; $charset='utf-8';  
  28. $this->httpsqs_host = $host;                   
  29. $this->httpsqs_port = $port;                   
  30. $this->httpsqs_auth = $auth;                   
  31. $this->httpsqs_charset = $charset;                   
  32. return true;           
  33. }        
  34.   
  35. public function http_get($query)       
  36. {           
  37. $socket = fsockopen($this->httpsqs_host, $this->httpsqs_port, $errno, $errstr, 5);           
  38. if (!$socket)           
  39. {               
  40. return false;           
  41. }           
  42. $out = "GET ${query} HTTP/1.1\r\n";           
  43. $out .= "Host: ".$this->httpsqs_host."\r\n";           
  44. $out .= "Connection: close\r\n";           
  45. $out .= "\r\n";           
  46. fwrite($socket, $out);           
  47. $line = trim(fgets($socket));     
  48. $header="";  
  49. $header .= $line;           
  50. list($proto, $rcode, $result) = explode(" ", $line);           
  51. $len = -1;           
  52. while (($line = trim(fgets($socket))) != "")           
  53. {               
  54. $header .= $line;               
  55. if (strstr($line, "Content-Length:"))               
  56. {                   
  57. list($cl, $len) = explode(" ", $line);                 
  58. }               
  59. if (strstr($line, "Pos:"))               
  60. {                   
  61. list($pos_key, $pos_value) = explode(" ", $line);               
  62. }                                  
  63. if (strstr($line, "Connection: close"))               
  64. {                   
  65. $close = true;               
  66. }           
  67. }           
  68. if ($len < 0)           
  69. {               
  70. return false;           
  71. }                    
  72. $body = fread($socket, $len);           
  73. $fread_times = 0;           
  74. while(strlen($body) < $len){                   
  75. $body1 = fread($socket, $len);                   
  76. $body .= $body1;                   
  77. unset($body1);                   
  78. if ($fread_times > 100)   
  79. {                           
  80. break;                   
  81. }                   
  82. $fread_times++;           
  83. }           
  84. //if ($close) fclose($socket);                   
  85. fclose($socket);                   
  86. $result_array["pos"] = (int)$pos_value;                   
  87. $result_array["data"] = $body;           
  88. return $result_array;       
  89. }        
  90.   
  91. public function http_post($query, $body)       
  92. {           
  93. $socket = fsockopen($this->httpsqs_host, $this->httpsqs_port, $errno, $errstr, 1);           
  94. if (!$socket)           
  95. {               
  96. return false;           
  97. }           
  98. $out = "POST ${query} HTTP/1.1\r\n";           
  99. $out .= "Host: " . $this->httpsqs_host . "\r\n";           
  100. $out .= "Content-Length: " . strlen($body) . "\r\n";           
  101. $out .= "Connection: close\r\n";           
  102. $out .= "\r\n";           
  103. $out .= $body;           
  104. fwrite($socket, $out);           
  105. $line = trim(fgets($socket));    
  106. $header="";  
  107. $header .= $line;           
  108. list($proto, $rcode, $result) = explode(" ", $line);           
  109. $len = -1;           
  110. while (($line = trim(fgets($socket))) != "")           
  111. {               
  112. $header .= $line;               
  113. if (strstr($line, "Content-Length:"))               
  114. {                   
  115. list($cl, $len) = explode(" ", $line);               
  116. }               
  117. if (strstr($line, "Pos:"))               
  118. {                   
  119. list($pos_key, $pos_value) = explode(" ", $line);               
  120. }                                  
  121. if (strstr($line, "Connection: close"))               
  122. {                   
  123. $close = true;               
  124. }           
  125. }           
  126. if ($len < 0)           
  127. {               
  128. return false;           
  129. }           
  130. $body = @fread($socket, $len);           
  131. //if ($close) fclose($socket);                   
  132. fclose($socket);                   
  133. $result_array["pos"] = (int)$pos_value;                   
  134. $result_array["data"] = $body;           
  135. return $result_array;       
  136. }      
  137.            
  138. public function put($queue_name, $queue_data)       
  139. {           
  140. $result = $this->http_post("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=put", $queue_data);                   
  141. if ($result["data"] == "HTTPSQS_PUT_OK")   
  142. {                           
  143. return true;                   
  144. }   
  145. else if ($result["data"] == "HTTPSQS_PUT_END")   
  146. {                           
  147. return $result["data"];                   
  148. }                   
  149. return false;       
  150. }            
  151.   
  152. public function get($queue_name)      
  153. {           
  154. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=get");                   
  155. if ($result == false || $result["data"] == "HTTPSQS_ERROR" || $result["data"] == false)   
  156. {                           
  157. return false;                   
  158. }           
  159. return $result["data"];       
  160. }          
  161.         
  162. public function gets($queue_name)       
  163. {           
  164. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=get");                   
  165. if ($result == false || $result["data"] == "HTTPSQS_ERROR" || $result["data"] == false)   
  166. {                           
  167. return false;                   
  168. }           
  169. return $result;       
  170. }              
  171.        
  172. public function status($queue_name)       
  173. {           
  174. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=status");                   
  175. if ($result == false || $result["data"] == "HTTPSQS_ERROR" || $result["data"] == false)   
  176. {                           
  177. return false;                   
  178. }           
  179. return $result["data"];       
  180. }                
  181.   
  182. public function view($queue_name, $queue_pos)       
  183. {           
  184. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=view&pos=".$pos);                   
  185. if ($result == false || $result["data"] == "HTTPSQS_ERROR" || $result["data"] == false)   
  186. {                           
  187. return false;                   
  188. }           
  189. return $result["data"];       
  190. }     
  191.              
  192. public function reset($queue_name)       
  193. {           
  194. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=reset");                   
  195. if ($result["data"] == "HTTPSQS_RESET_OK")   
  196. {                           
  197. return true;                   
  198. }           
  199. return false;       
  200. }    
  201.               
  202. public function maxqueue($queue_name, $num)       
  203. {           
  204. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=maxqueue&num=".$num);                   
  205. if ($result["data"] == "HTTPSQS_MAXQUEUE_OK")   
  206. {                           
  207. return true;                   
  208. }           
  209. return false;       
  210. }       
  211.            
  212. public function status_json($queue_name)       
  213. {           
  214. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=".$queue_name."&opt=status_json");                   
  215. if ($result == false || $result["data"] == "HTTPSQS_ERROR" || $result["data"] == false)   
  216. {                           
  217. return false;                   
  218. }           
  219. return $result["data"];       
  220. }        
  221.   
  222. public function synctime($num)       
  223. {           
  224. $result = $this->http_get("/?auth=".$this->httpsqs_auth."&charset=".$this->httpsqs_charset."&name=httpsqs_synctime&opt=synctime&num=".$num);                   
  225. if ($result["data"] == "HTTPSQS_SYNCTIME_OK")   
  226. {                           
  227. return true;                   
  228. }           
  229. return false;       
  230. }   
  231. }   
  232. ?>   

  1. <?php  
  2.   
  3. include_once("httpsqs_client.php");   
  4. $httpsqs = new httpsqs();   
  5. $result = $httpsqs->put("city", date('Y-m-d H:i:s',time()));   
  6. echo $result;  
  7. echo "</br>";  
  8. $result = $httpsqs->get("city");   
  9. echo $result;  
  10. ?>  

  1. <?php  
  2. include_once("httpsqs_client.php");   
  3. $httpsqs = new httpsqs($httpsqs_host, $httpsqs_port, $httpsqs_auth, $httpsqs_charset);   
  4.    
  5. /*  
  6. 1. 将文本信息放入一个队列(注意:如果要放入队列的PHP变量是一个数组,需要事先使用序列化、json_encode等函数转换成文本) 
  7.     如果入队列成功,返回布尔值:true  
  8.     如果入队列失败,返回布尔值:false  
  9. */   
  10. $result = $httpsqs->put($queue_name, $queue_data);   
  11.    
  12. /*  
  13. 2. 从一个队列中取出文本信息 
  14.     返回该队列的内容 
  15.     如果没有未被取出的队列,则返回文本信息:HTTPSQS_GET_END 
  16.     如果发生错误,返回布尔值:false  
  17. */   
  18. $result = $httpsqs->get($queue_name);   
  19.   
  20. /*  
  21. 3. 从一个队列中取出文本信息和当前队列读取点Pos 
  22.     返回数组示例:array("pos" => 7, "data" => "text message") 
  23.     如果没有未被取出的队列,则返回数组:array("pos" => 0, "data" => "HTTPSQS_GET_END") 
  24.     如果发生错误,返回布尔值:false 
  25. */   
  26. $result = $httpsqs->gets($queue_name);  
  27.   
  28. /*  
  29. 4. 查看队列状态(普通方式) 
  30. */   
  31. $result = $httpsqs->status($queue_name);  
  32.   
  33. /*  
  34. 5. 查看队列状态(JSON方式) 
  35.     返回示例:{"name":"queue_name","maxqueue":5000000,"putpos":130,"putlap":1,"getpos":120,"getlap":1,"unread":10} 
  36. */   
  37. $result = $httpsqs->status_json($queue_name);  
  38.    
  39. /*  
  40. 6. 查看指定队列位置点的内容 
  41.     返回指定队列位置点的内容。 
  42. */   
  43. $result = $httpsqs->view($queue_name, $queue_pos);  
  44.    
  45. /*  
  46. 7. 重置指定队列 
  47.     如果重置队列成功,返回布尔值:true  
  48.     如果重置队列失败,返回布尔值:false  
  49. */   
  50. $result = $httpsqs->reset($queue_name);  
  51.    
  52. /*  
  53. 8. 更改指定队列的最大队列数量 
  54.    如果更改成功,返回布尔值:true 
  55.    如果更改操作被取消,返回布尔值:false 
  56. */   
  57. $result = $httpsqs->maxqueue($queue_name, $num);  
  58.   
  59. /* 
  60. 9. 修改定时刷新内存缓冲区内容到磁盘的间隔时间 
  61.    如果更改成功,返回布尔值:true 
  62.    如果更改操作被取消,返回布尔值:false 
  63. */  
  64. $result = $httpsqs->synctime($num);  
  65. ?>  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
HTTPSQS(HTTP Simple Queue Service)消息队列
RabbitMQ 入门 Helloworld
python queue模块 消息队列
Laravel 5.1 LTS 速查表
Laravel 5
17.7 队列
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服