日期:2014-05-16  浏览次数:20433 次

ttserver安装 + ttserver起停脚本(双主备) + java代码
by:alex
1,下载tokyocabinet-1.4.46.tar.gz
     下载tokyotyrant-1.1.41.tar.gz
      要在本地连接可以选择下载tokyocabinet-java-1.22.tar.gz
详细安装文档http://www.162cm.com/p/tokyotyrant.html

2,解压缩后进入目录:先安装tokyocabinet-1.4.46.tar.gz,再安装tokyotyrant-1.1.41.tar.gz
   
     安装方法:
          cd进入对应目录
          ./configure
          make
          make install

3,写个脚本来启动,这里假设192.168.0.2和192.168.0.3互为主备机
192.168.0.2
     

#author:Alex
case $1 in
    "start" )
        echo "startng ttservice......"
        ttserver -port 11211 -dmn -pid /opt/ttserver/pid -log /opt/ttserver/log -ulog /opt/ttserver/ul                                                                           og -ulim 128m -sid 60 -mhost 192.168.0.3 -mport 11212 -rcc -dmn -rts /opt/ttserver/rts /opt/ttserver/                                                                           casket.tch#bnum=1000000
        echo "ttserver started.";;
    "stop" )
        echo "stoping ttservice......"
        killall -9 ttserver
        rm -f /opt/ttserver/pid
#cat /opt/ttserver/pid | kill -9
        echo "ttservice stoped.";;
    "restart")
        echo "shutdown ttserver...."
        killall -9 ttserver
        rm -f /opt/ttserver/pid
        sleep 5
        echo "restart ttserver..."
        ttserver -port 11211 -dmn -pid /opt/ttserver/pid -log /opt/ttserver/log -ulog /opt/ttserver/ul                                                                           og -ulim 128m -sid 60 -mhost 192.168.0.3 -mport 11212 -rcc -dmn -rts     /opt/ttserver/rts /opt/ttser                                                                           ver/casket.tch#bnum=1000000
        echo "ttserver started.";;

    "clean")
        echo "clean db"
        rm -f /opt/ttserver/casket.tch;;
    *) echo "unknown command.[usage:start|stop|restart]";;
esac

   

192.168.0.3
  
#author Alex
case $1 in
    "start" )
        echo "startng ttservice......"
        ttserver -port 11212 -dmn -pid /opt/ttserver/pid -log /opt/ttserver/log -ulog /opt/ttserver/ulog -ulim 128m -sid 117 -mhost 192.168.0.2 -mport 11211 -rcc -dmn -rts /opt/ttserver/rts /opt/ttserver/casket.tch#bnum=1000000
        echo "ttserver started.";;
    "stop" )
        echo "stoping ttservice......"
        killall -9 ttserver
        rm -f /opt/ttserver/pid
        echo "ttservice stoped.";;
    "restart")
        echo "shutdown ttserver...."
        killall -9 ttserver
        rm -f /opt/ttserver/pid
        sleep 5
        echo "restart ttserver..."
        ttserver -port 11212 -dmn -pid /opt/ttserver/pid -log /opt/ttserver/log -ulog /opt/ttserver/ulog -ulim 128m -sid 117 -mhost 192.168.0.2 -mport 11211 -rcc -dmn -rts /opt/ttserver/rts /opt/ttserver/casket.tch#bnum=1000000
        echo "ttserver started.";;

    "clean")
        echo "clean db"
        rm -f /opt/ttserver/casket.tch;;
    *) echo "unknown command.[usage:start|stop|restart]";;
esac


4,http协议访问
    curl  http://localhost:11211/key返回value
    curl -X PUT http://localhost:11211/keyname -d "strvalue"
5,java代码(节选自自己的测试代码)
需要的jar包:tokyotyrant-0.11.jar\slf4j-api-1.6.1.jar\slf4j-nop-1.6.1.jar\netty-3.2.2.Final-dist.jar
     
import tokyotyrant.MRDB;
import tokyotyrant.networking.NodeAddress;

import com.xx.xxxx.service.Cache;

public class TTCache implements Cache {
	
	private MRDB mrdb;
	private NodeAddress[] addresses;
	
	public RemoteTokyocanbinetCache( NodeAddress[] addresses) {