Kestrel队列使用示例[配置、安装、命令]

转载自:http://javacrazyer.iteye.com/blog/1840140

消息队列在实时性web项目中应用极多,对于处理大量一部并发请求有极大的作用

kestrel基础认知

1)Kestrel是twitter开源的一个scala写的简单高效MQ,采用的协议是memcached的文本协议,但是并不完全支持所有memcached协议,也不是完全兼容现有协议。

2)标准的协议它仅支持GET、SET、FLUSH_ALL、STATS,

3)Kestrel是一个队列服务器

kestrel官方api

  1. kestrel的项目主页   http://github.com/robey/kestrel  
  2. Kestrel的安装配置   http://robey.github.io/kestrel/readme.html  
  3.                     https://github.com/robey/kestrel/blob/master/docs/guide.md  
  4.                     http://dmouse.iteye.com/blog/1746171  
  5. kestrel的wiki页     http://wiki.github.com/robey/kestrel  
  6. memcache官网        http://www.memcached.org/  
  7. xmemcached项目主页  http://code.google.com/p/xmemcached/  
  8.   
  9. --------------------有关kestrel定义,官方如下很多描述  
  10. Kestrel  
  11. Kestrel is a simple, distributed message queue written on the JVM, based on Blaine Cook's "starling".  
  12. Each server handles a set of reliable, ordered message queues, with no cross communication, resulting in a cluster of k-ordered ("loosely ordered") queues. Kestrel is fast, small, and reliable.  
  13.   
  14. Kestrel is:  
  15. 1)fast  
  16. It runs on the JVM so it can take advantage of the hard work people have put into java performance.  
  17.   
  18. 2)small  
  19. Currently about 2500 lines of scala, because it relies on Netty (a rough equivalent of Danger's ziggurat or Ruby's EventMachine) -- and because Scala is extremely expressive.  
  20.   
  21. 3)durable  
  22. Queues are stored in memory for speed, but logged into a journal on disk so that servers can be shutdown or moved without losing any data.  
  23.   
  24. 4)reliable  
  25. A client can ask to "tentatively" fetch an item from a queue, and if that client disconnects from kestrel before confirming ownership of the item, the item is handed to another client. In this way, crashing clients don't cause lost messages.  
  26.   
  27.   
  28. Kestrel is based on Blaine Cook's "starling" simple, distributed message queue, with added features and bulletproofing, as well as the scalability offered by actors and the JVM.  
  29.   
  30. Each server handles a set of reliable, ordered message queues. When you put a cluster of these servers together, with no cross communication, and pick a server at random whenever you do a set or get, you end up with a reliable, loosely ordered message queue.  
  31.   
  32. In many situations, loose ordering is sufficient. Dropping the requirement on cross communication makes it horizontally scale to infinity and beyond: no multicast, no clustering, no "elections", no coordination at all. No talking! Shhh!  
  33.   
  34. Kestrel is a very simple message queue that runs on the JVM. It supports multiple protocols:  
  35. memcache: the memcache protocol, with some extensions  
  36. thrift: Apache Thrift-based RPC  
  37. text: a simple text-based protocol  
  38.   
  39.   
  40. Features  
  41. memcache protocol  
  42. thrift protocol  
  43. journaled (durable) queues  
  44. fanout queues (one writer, many readers)  
  45. item expiration  
  46. transactional reads  
  47. ----------------------------有关kestrel定义,官方如上很多描述  
  48.   
  49. telnet 202.108.1.121 22133  
  50. 命令:  
  51. stats  
  52.   
  53. Server stats  
  54. Global stats reported by kestrel are:  
  55. 执行完stats命令,命令行上最上面的是返回值如下:  
  56.   
  57. uptime - seconds the server has been online   kestrel服务已经启动多少秒  
  58. time - current time in unix epoch     当前kestrel服务器的时间  
  59. version - version string, like "1.2"   kestrel的版本号  
  60. curr_items - total of items waiting in all queues  所有队列的ITEM条目数的总和  
  61. total_itmes - total of items that have ever been added in this server's lifetime  曾经被添加到队列的所有条目的生存时间总和  
  62. bytes - total byte size of items waiting in all queues  被写入队列的所有条目的字节数总和  
  63. curr_connections - current open connections from clients  当前客户端打开有多少个连接  
  64. total_connections - total connections that have been opened in this server's lifetime  在服务器打开的这段时间类总共的连接数  
  65. cmd_get - total GET requests    get请求的总次数  
  66. cmd_set - total SET requests    set请求的总次数  
  67. cmd_peek - total GET/peek requests peek请求总次数  
  68. get_hits - total GET requests that received an item  get请求命令次数  
  69. get_misses - total GET requests on an empty queue  get请求无效的次数,也就是请求在了空的队列上  
  70. bytes_read - total bytes read from clients    客户端总共读取字节总数  
  71. bytes_written - total bytes written to clients  写往客户端的总字节数  
  72. queue_creates - total number of queues created  当前kestrel上已经创建的队列总数  
  73. queue_deletes - total number of queues deleted (includes expires) 已经删除的队列总数  
  74. queue_expires - total number of queues expires  已经过期的队列总数  
  75.   
  76. ------例如:  
  77. STAT uptime 26327844  
  78. STAT time 1375153623  
  79. STAT version 2.1.3  
  80. STAT curr_items 7727349  
  81. STAT total_items 460922839  
  82. STAT bytes 2552974113  
  83. STAT curr_connections 146  
  84. STAT total_connections 25500291  
  85. STAT cmd_get 10776718344  
  86. STAT cmd_set 460922839  
  87. STAT cmd_peek 0  
  88. STAT get_hits 383861308  
  89. STAT get_misses 10392857037  
  90. STAT bytes_read 1957725152259  
  91. STAT bytes_written 1777370590356   
  92. ------  
  93.   
  94. 紧接着上面那几条返回值,是许多类似下面的条目:  
  95. For each queue, the following stats are also reported:  
  96.   
  97. items - items waiting in this queue    队列中当前存在的条目数  
  98. bytes - total byte size of items waiting in this queue   队列中当前条目的总字节数  
  99. total_items - total items that have been added to this queue in this server's lifetime  总共向队列中添加的条目总个数  
  100. logsize - byte size of the queue's journal file   当前队列日志的大小  
  101. expired_items - total items that have been expired from this queue in this server's lifetime 当前队列失效的条目总数  
  102. mem_items - items in this queue that are currently in memory    当前队列中存在于内存中的条目数  
  103. mem_bytes - total byte size of items in this queue that are currently in memory (will always be less than or equal to max_memory_size config for the queue)  
  104. age - time, in milliseconds, that the last item to be fetched from this queue had been waiting; that is, the time between SET and GET; if the queue is empty, this will always be zero  
  105. discarded - number of items discarded because the queue was too full  
  106. waiters - number of clients waiting for an item from this queue (using GET/t)  
  107. open_transactions - items read with /open but not yet confirmed  
  108. transactions - number of transactional get requests (irrespective of whether an item was read or not)  
  109. canceled_transactions - number of transactional get requests canceled (for any reason)  
  110. total_flushes - total number of times this queue has been flushed  
  111. age_msec - age of the last item read from the queue  
  112. create_time - the time that the queue was created (in milliseconds since epoch)  
  113.   
  114. -----例如:  
  115. STAT queue_hems_ds_news_items 0  
  116. STAT queue_hems_ds_news_bytes 0  
  117. STAT queue_hems_ds_news_total_items 27231842  
  118. STAT queue_hems_ds_news_logsize 15425969  
  119. STAT queue_hems_ds_news_expired_items 0  
  120. STAT queue_hems_ds_news_mem_items 0  
  121. STAT queue_hems_ds_news_mem_bytes 0  
  122. STAT queue_hems_ds_news_age 0  
  123. STAT queue_hems_ds_news_discarded 0  
  124. STAT queue_hems_ds_news_waiters 0  
  125. STAT queue_hems_ds_news_open_transactions 0  
  126.   
  127.   
  128. DUMP_STATS  命令会以队列分组的格式显示出来  
  129. ===========================  
  130.   
  131. 关于三种协议:  
  132. Protocols  
  133. Kestrel supports three protocols: memcache, thrift and text.   
  134. The Finagle project can be used to connect clients to a Kestrel server via the memcache or thrift protocols.  
  135. Finagle项目可以通过thrift协议或者memcache协议使客户端连接到kestrel服务器  
  136.   
  137. Thrift  
  138. The thrift protocol is documented in the thrift IDL: kestrel.thrift  
  139. Reliable reads via the thrift protocol are specified by indicating how long the server should wait before aborting the unacknowledged read.  
  140.   
  141. Memcache  
  142. kestrel遵守memcache官方标准协议,如下这个文件有描述:  
  143. The official memcache protocol is described here: protocol.txt  
  144. https://github.com/memcached/memcached/blob/master/doc/protocol.txt  
  145.   
  146.   
  147. Text protocol  
  148. kestrel支持受限制的,只是文本的协议。不过还是推荐你用memcache协议替代文本协议。因为文本协议不支持可靠的读操作。  
  149. Kestrel supports a limited, text-only protocol. You are encouraged to use the memcache protocol instead.  
  150. The text protocol does not support reliable reads.  
  151.   
  152.   
  153.   
  154.   
  155.   
  156. kestrel实现memcache协议的命令如下:  
  157. The kestrel implementation of the memcache protocol commands is described below.  
  158.   
  159. SET <queue-name> <flags (ignored)> <expiration> <# bytes>  
  160. Add an item to a queue. It may fail if the queue has a size or item limit and it's full.  
  161.   
  162. GET <queue-name>[options]  
  163. Remove an item from a queue. It will return an empty response immediately if the queue is empty. The queue name may be followed by options separated by /:  
  164.   
  165. /t=<milliseconds>  
  166. Wait up to a given time limit for a new item to arrive. If an item arrives on the queue within this timeout, it's returned as normal. Otherwise, after that timeout, an empty response is returned.  
  167.   
  168. /open  
  169. Tentatively remove an item from the queue. The item is returned as usual but is also set aside in case the client disappears before sending a "close" request. (See "Reliable Reads" below.)  
  170.   
  171. /close  
  172. Close any existing open read. (See "Reliable Reads" below.)  
  173.   
  174. /abort  
  175. Cancel any existing open read, returing that item to the head of the queue. It will be the next item fetched. (See "Reliable Reads" below.)  
  176.   
  177. /peek  
  178. Return the first available item from the queue, if there is one, but don't remove it. You can't combine this with any of the reliable read options.  
  179.   
  180. For example, to open a new read, waiting up to 500msec for an item:  
  181.     GET work/t=500/open  
  182. Or to close an existing read and open a new one:  
  183.     GET work/close/open  
  184.   
  185. DELETE <queue-name>   删除某个队列同时删除所有条目,也会删除有关联的日志文件  
  186. Drop a queue, discarding any items in it, and deleting any associated journal files.  
  187.   
  188. FLUSH <queue-name>   删除某个队列中的所有条目  
  189. Discard all items remaining in this queue. The queue remains live and new items can be added. The time it takes to flush will be linear to the current queue size, and any other activity on this queue will block while it's being flushed.  
  190.   
  191. FLUSH_ALL  删除所有队列中的所有条目,就好像是每个队列都接受到了FLUSH命令一样  
  192. Discard all items remaining in all queues. The queues are flushed one at a time, as if kestrel received a FLUSH command for each queue.  
  193.   
  194. VERSION   查询KESTREL的版本号  
  195. Display the kestrel version in a way compatible with memcache.  
  196.   
  197. SHUTDOWN  关闭KESTREL服务器然后退出  
  198. Cleanly shutdown the server and exit.  
  199.   
  200. RELOAD   重新加载配置文件  
  201. Reload the config file and reconfigure all queues. This should have no noticable effect on the server's responsiveness.  
  202.   
  203. STATS  跟MEMCACHE显示的格式一样显示  
  204. Display server stats in memcache style. They're described below.  
  205.   
  206. DUMP_STATS  按照队列名分组来显示  
  207. Display server stats in a more readable style, grouped by queue. They're described below.  
  208.   
  209. MONITOR <queue-name> <seconds> [max-items]  
  210. Monitor a queue for a time, fetching any new items that arrive, up to an optional maximum number of items. Clients are queued in a fair fashion, per-item, so many clients may monitor a queue at once. After the given timeout, a separate END response will signal the end of the monitor period. Any fetched items are open transactions (see "Reliable Reads" below), and should be closed with CONFIRM.  
  211.   
  212. CONFIRM <queue-name> <count>  
  213. Confirm receipt of count items from a queue. Usually this is the response to a MONITOR command, to confirm the items that arrived during the monitor period.  
  214.   
  215. STATUS  
  216. Displays the kestrel server's current status (see section on Server Status, below).  
  217.   
  218. STATUS <new-status>  
  219. Switches the kestrel server's current status to the given status (see section on Server Status, below).  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Kestrel创建TCP服务器,您需要使用`Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets` NuGet包。以下是一个示例,演示如何使用Kestrel创建TCP服务器: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; class Program { static async Task Main(string[] args) { var host = new WebHostBuilder() .UseKestrel(options => { options.Listen(IPAddress.Any, 1234, listenOptions => { listenOptions.UseConnectionHandler<TcpConnectionHandler>(); }); }) .ConfigureServices(services => services.AddSingleton<TcpConnectionHandler>()) .Configure(app => app.UseRouting()) .Build(); await host.RunAsync(); } } class TcpConnectionHandler : ConnectionHandler { public override async Task OnConnectedAsync(ConnectionContext connection) { Console.WriteLine($"New client connected: {connection.ConnectionId}"); while (true) { var buffer = new byte[1024]; var result = await connection.Transport.Input.ReadAsync(buffer); var input = Encoding.UTF8.GetString(buffer.Slice(0, result.Buffer.Length)); Console.WriteLine($"Received from client {connection.ConnectionId}: {input}"); if (result.IsCompleted) { break; } } } } ``` 在此示例中,`Listen`方法用于配置Kestrel服务器以侦听TCP连接。使用`UseConnectionHandler`方法指定连接处理程序,该处理程序将处理所有传入的连接。在此示例中,我们使用`TcpConnectionHandler`类作为连接处理程序。在`TcpConnectionHandler`的`OnConnectedAsync`方法中,我们读取来自客户端的数据并打印到控制台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值