利用Tokyo Tyrant构建高并发的分布式key-value数据库系统

时间: 2010-07-21 / 分类: 技术相关 / 浏览次数: 3,878 views / 3个评论 发表评论

[文章作者:叶歆昊   最后修改:2010-07-21   转载请注明原文链接:http://littz.com/install-tokyo-tyrant-tokyo-cabinet.html]

Overview

Tokyo Cabinet is a library of routines for managing a database. The database is a simple data file containing records, each is a pair of a key and a value. Every key and value is serial bytes with variable length. Both binary data and character string can be used as a key and a value. There is neither concept of data tables nor data types. Records are organized in hash table, B+ tree, or fixed-length array.

Tokyo Cabinet is developed as the successor of GDBM and QDBM on the following purposes. They are achieved and Tokyo Cabinet replaces conventional DBM products.

  • improves space efficiency : smaller size of database file.
  • improves time efficiency : faster processing speed.
  • improves parallelism : higher performance in multi-thread environment.
  • improves usability : simplified API.
  • improves robustness : database file is not corrupted even under catastrophic situation.
  • supports 64-bit architecture : enormous memory space and database file are available.

Tokyo Cabinet is written in the C language, and provided as API of C, Perl, Ruby, Java, and Lua. Tokyo Cabinet is available on platforms which have API conforming to C99 and POSIX. Tokyo Cabinet is a free software licensed under the GNU Lesser General Public License.

Tokyo Cabinet是一种兼容Memcached协议、支持故障转移、高并发的分布式key-value持久存储系统,由日本人 平林幹雄 开发,数据库读写非常快,哈希模式写入100万条数据只需0.6x秒,读取100万条数据只需0.7x秒,相对于其他DMB性能表现出众。详见官方网站 http://1978th.net/tokyocabinet/

而Tokyo Tyrant是同一作者书写的,基于网络层封装依据HTTP标准的Tokyo Cabinet数据库接口,同时也兼容memcache协议,详见官方网站 http://1978th.net/tokyotyrant/

TC/TT的特性:

1、故障转移:Tokyo Tyrant支持双机互为主辅模式,主辅库均可读写,服务器端保持同步;也可以实现类似MySQL主辅库同步的方式实现读写分离,主服务器写,多个从服务器读。

2、兼容Memcache客户端,直接用php的 new memcache()就完全可以操作。如果需要memcache客户端的故障转移,使用$memcache->addServer()连接而不是$memcache->Connect()

3、数据存储占用小,同步日志文件体积小,同步日志约为数据本身的1.3倍

4、性能表现稳定出色:
TCHBD速度最快,但与memcache类似需要完全内存映射。
TCBDB。B+Tree索引,缓存800万条记录缓存,5G数据量,在只开128M内存,并且已经有1000的并发连接数的情况下:首次查询0.1秒级别完成,二次查询0.00x秒,约为TCHBD的85%
TCTDB表结构的数据读写性能约为TCHDB的45%

安装Tokyo Cabinet

wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.45.tar.gz
tar zxvf tokyocabinet-1.4.45.tar.gz
cd tokyocabinet-1.4.45/
./configure -prefix=/usr/local/webserver/tokyocabinet
# 注:在32位Linux操作系统上编译Tokyo cabinet,请使用./configure --enable-off64代替./configure,可以使数据库文件突破2GB的限制。
#./configure -prefix=/usr/local/webserver/tokyocabinet --enable-off64
make
make install
cd ../

安装Tokyo Tyrant

wget http://www.1978th.net/tokyotyrant/tokyotyrant-1.1.40.tar.gz
tar zxvf tokyotyrant-1.1.40.tar.gz
cd tokyotyrant-1.1.40/
./configure --prefix=/usr/local/webserver/tokyotyrant --with-tc=/usr/local/webserver/tokyocabinet/
make
make install
cd ../

添加lib

vi /etc/ld.so.conf
#增加
/usr/local/webserver/tokyocabinet/lib/
/usr/local/webserver/tokyotyrant/lib/

ldconfig

配置

创建数据存放目录

mkdir -p /opt/tt/ulog

配置文件句柄数

vi /etc/rc.d/rc.local
ulimit -SHn 65535

vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

启动ttserver(TokyoTyrant)

1.单机方式

/usr/local/webserver/tokyotyrant/bin/ttserver -host 127.0.0.1 -port 1978 -thnum 8 -dmn -pid /opt/tt/tt.pid -log /opt/tt/tt.log -ld -ulog /opt/tt/ulog -ulim 128m -sid 1 -rts /opt/tt/tt.rts /opt/tt.tcb#lmemb=1024#nmemb=2048#bnum=5000000

2.双机互为主辅

#服务器192.168.1.1:
/usr/local/webserver/tokyotyrant/bin/ttserver -host 192.168.1.1 -port 1978 -thnum 8 -dmn -pid /opt/tt/tt.pid -log /opt/tt/tt.log -ld -ulog /opt/tt/ -ulim 128m -sid 1 -mhost 192.168.1.2 -mport 1978 -rts /opt/tt1/tt1.rts /opt/tt/tt.tcb#lmemb=1024#nmemb=2048#bnum=5000000

#服务器192.168.1.2:
/usr/local/webserver/tokyotyrant/bin/ttserver -host 192.168.1.2 -port 1978 -thnum 8 -dmn -pid /opt/tt/tt.pid -log /opt/tt/tt.log -ld -ulog /opt/tt/ -ulim 128m -sid 2 -mhost 192.168.1.1 -mport 1978 -rts /opt/tt/tt.rts /opt/tt/tt.tcb#lmemb=1024#nmemb=2048#bnum=5000000

#注意 host为监听IP,mhost为另一个服务器的IP,如果本机模拟测试,可以在一个服务器上开多个端口。详细配置参见http://1978th.net/tokyotyrant/spex.html#serverprog

停止ttserver服务

kill -TERM `cat /opt/tt/tt.pid`

TokyoTyrant客户端

1.支持HTTP协议:

#(1)、写数据,将数据"value"写入到"key"中:
curl -X PUT http://127.0.0.1:1978/key -d "value"
#(2)、读数据,读取"key"中数据:
curl http://127.0.0.1:1978/key
#(3)、删数据,删除"key":
curl -X DELETE http://127.0.0.1:1978/key

2.TokyoTyrant(PECL)客户端:


$tt = new TokyoTyrant("localhost",  1978);//连接
$tt->put("key", "value");//写入
$tt->put(array("key1" => "value1", "key" => "value2"));//覆盖写入
$tt->out("key1");//删除
echo $tt->get("key");//读取

3.Key-Value结构的可以直接使用memcache客户端

4.查看状态

/usr/local/webserver/tokyotyrant/bin/tcrmgr inform -port 1978 127.0.0.1

3个评论

  1. 彭吉吉
    2010/07/21 于 15:14:37

    我只知道Tokyo Hot 怎么办。。。

发表评论

您的昵称 *

您的邮箱 * (绝对保密)

您的网站