征服 Kestrel

因为要面对高并发PUSH需求,考虑将其按队列方式实现,最终选型Kestrel。
至于Kestrel:
[list]
[*]基于Scala语言的Twitter开源消息中间件
[*]高性能(TPS 6000不成问题)、小巧(2K行代码)、持久存储(记录日志到journal)并且可靠(支持可靠获取)
[*][b]Kestrel[/b]的前身是[b]Ruby[/b]写的[b]Starling[/b]项目,后来twitter的开发人员尝试用Scala重新实现。
[/list]
可支持的标准协议:
[list]
[*]SET 存
[*]GET 取
[*]FLUSH_ALL 清理
[*]STATS 状态
[/list]
扩展协议:
[list]
[*]SHUTDOWN 关闭kestrel server,如果执行该操作,需强制重启Kestrel
[*]RELOAD 动态重新加载配置文件
[*]DUMP_CONFIG dump配置文件
[*]FLUSH queueName flush某个队列
[/list]
经测试,支持[b]DELETE[/b]协议! :D

PS:[b]XMemcached-1.2[/b]及其以上版本已对其协议完全支持,注意使用[b]KestrelCommandFactory[/b]。 :D

当然,[b]Redis[/b]也可以做消息队列,但[b]Redis[/b]目前只是[b]Master-Slave[/b]模式,还不能像[b]Kestrel[/b]做到[b]Cluster[/b]。所以,如果只是考虑队列服务,还是纯粹一点,直接用[b]Kestrel[/b],配合[b]XMemcached[/b]作为客户端,保持一致性哈希,用起来更放心。因为,高可用嘛!呵呵! :D

想要消化Kestrel,需要做些准备工作:
[list]
[*]kestrel,必须的!这里用[url=http://javabloger-mini-books.googlecode.com/files/kestrel-1.2.7-SNAPSHOT.zip]kestrel-2.1.7-SNAPSHOT.jar[/url]
[*]daemon,Linux守护进程,这里用[url=http://libslack.org/daemon/download/daemon-0.6.4.tar.gz]daemon-0.6.4[/url]
[/list]
本想Git下来,逐个编译一把,但始终未果,只好找兄弟copy一份来运行! :D
我会在附件中,追加相应的配置文件,以及kestrel-2.1.5.jar。 :D

如果你的Server还没有安装Daemon,参考如下操作:

wget http://libslack.org/daemon/download/daemon-0.6.4.tar.gz
tar zxvf daemon-0.6.4.tar.gz
cd daemon-0.6.4
./configure && make && make install


[b][size=large]一、Kestrel目录结构[/size][/b]
Kestrel目录结构如下:
Kestrel
|-kestrel-1.2.7-SNAPSHOT.jar
|-kestrel-1.2.7-SNAPSHOT.pom
|-config
|-development.conf
|-production.conf
|-libs
|-scripts
|-devel.sh
|-kestrel.sh
|-qdump.sh

libs中的jar列表:
[list]
[*]configgy-1.6.4.jar
[*]naggati_2.7.7-0.7.4.jar
[*]slf4j-jdk14-1.5.2.jar
[*]twitteractors_2.7.7-2.0.0.jar
[*]json-1.1.3.jar
[*]scala-library.jar
[*]specs-1.6.2.1.jar
[*]vscaladoc-1.1-md-3.jar
[*]mina-core-2.0.0-M6.jar
[*]slf4j-api-1.5.2.jar
[*]twitteractors-1.1.0.jar
[*]xrayspecs-1.0.7.jar
[/list]
由于附件体积限制,可能需要另行下载(Maven是个好帮手! :D )
我们只需要关注以下几个文件:
适用于开发环境:
[list]
[*][b]script/devel.sh[/b]用于验证服务配置是否可用
[*][b]config/development.conf[/b]配合[b]devel.sh[/b]进行操作的配置文件
[/list]

适用于生产环境:
[list]
[*][b]scripts/kestrel.sh[/b]核心执行文件
[*][b]config/production.conf[/b]核心配置文件
[/list]

[b][size=large]二、Kestrel脚本&配置说明[/size][/b]
这里将Kestrel安装至[b]/opt/servers/kestrel[/b]路径下,你可能需要对应修改路径配置。
先说用于开发环境的脚本&配置文件:
[b]devel.sh[/b]
[quote]
#!/bin/bash
APP_NAME="kestrel"
#应用路径
APP_PATH="/opt/servers/kestrel"
#版本
VERSION="1.2.7-SNAPSHOT"

echo "Starting kestrel in development mode..."
java -server -Xmx1024m -Dstage=development -jar $APP_PATH/$APP_NAME-$VERSION.jar
[/quote]
[color=red]注意修改[b]APP_PATH[/b]![/color]

[b]development.conf[/b]
[quote]
# kestrel config for a production system

# where to listen for connections:
port = 22133
host = "0.0.0.0"

log {
#日志路径
filename = "/var/logs/kestrel_development.log"
roll = "daily"
level = "info"
}

queue_path = "/var/spool/kestrel"
[/quote]

做一个简单的测试:
[quote]./scripts/devel.sh
Starting kestrel in development mode...[/quote]
进行如下操作:
[quote]
telnet localhost 22133
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
set x 0 0 5
12345
STORED
[/quote]
在另一个终端上获得该消息:
[quote]
telnet localhost 22133
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
get x
VALUE x 0 5
12345
END
get x
END
[/quote]
如上操作,说明配置已成功。

如法炮制生产环境配置:
[b]kestrel.sh[/b]
[quote]
APP_NAME="kestrel"
VERSION="1.2.7-SNAPSHOT"
#Kestrel路径
APP_HOME="/opt/servers/$APP_NAME"
AS_USER="daemon"
DAEMON="/usr/local/bin/daemon"
QUEUE_PATH="/var/spool/kestrel"

HEAP_OPTS="-Xmx2048m -Xms1024m -XX:NewSize=256m"
JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=22134 -Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false"
# add JMX_OPTS below if you want jmx support.
#如果需要控制字符集,使用-Dfile.encoding=UTF8
JAVA_OPTS="-server -verbosegc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+
UseConcMarkSweepGC -XX:+UseParNewGC $HEAP_OPTS"
[/quote]
[b][color=red]你可能需要修改APP_HOME变量[/color][/b]


[b]production.conf[/b]
[quote]
# kestrel config for a production system

# where to listen for connections:
port = 22133
#建议绑定主机IP
host = "0.0.0.0"

log {
filename = "/var/logs/kestrel.log"
roll = "daily"
level = "info"
}

#队列存储路径,用于存储/恢复队列消息,建议存放在磁盘较大的区域
queue_path = "/var/spool/kestrel"
[/quote]
[color=red]建议绑定[b]host[/b],确保服务器安全[/color]

可以重复上述测试操作,测试服务是否可用! :D
或者,直接查看服务状态——STATS! :D
[quote]telnet localhost 22133
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
stats
STAT uptime 52568
STAT time 1343093076
STAT version 1.2.7-SNAPSHOT
STAT curr_items 0
STAT total_items 1
STAT bytes 0
STAT curr_connections 1
STAT total_connections 9
STAT cmd_get 2
STAT cmd_set 1
STAT cmd_peek 0
STAT get_hits 1
STAT get_misses 1
STAT bytes_read 91
STAT bytes_written 151
STAT queue_test_items 0
STAT queue_test_bytes 0
STAT queue_test_total_items 1
STAT queue_test_logsize 27
STAT queue_test_expired_items 0
STAT queue_test_mem_items 0
STAT queue_test_mem_bytes 0
STAT queue_test_age 0
STAT queue_test_discarded 0
STAT queue_test_waiters 0
STAT queue_test_open_transactions 0
END
[/quote]
最后,拷贝[b]kestrel.sh[/b]文件到[b]/etc/init.d/[/b]路径下,并赋予执行权限:

cp kestrel.sh /etc/init.d/kestrel
chmod +x /etc/init.d/kestrel


后续,我们就可以通过服务方式,调用kestrel了! :D
[quote]
service kestrel {start|stop|restart|status}
[/quote]

由于jar文件较大,未在附件内上传外,其余配置文件相见附件! :D

[color=red][b][size=large]PS:重点说明一点,队列名称/缓存键名称,一定不要始终“-”作为连接符,请使用“_”作为连接符,避免意想不到的错误。![/size][/b][/color]

[b]
相关链接:
[url=http://snowolf.iteye.com/blog/1604531]征服 Kestrel[/url]
[url=http://snowolf.iteye.com/blog/1605229]征服 Kestrel + XMemcached[/url]
[url=http://snowolf.iteye.com/blog/1612207]征服 Kestrel + XMemcached + Spring TaskExecutor[/url]
[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值