在redhat6.4安装redis集群【教程】
参考:
http://redis.io/topics/cluster-tutorial(主要是CreatingaRedisClusterusingthecreate-clusterscript部分)
https://ruby.taobao.org/
安装一款不熟悉的软件前先看INSTALL,README,这是习惯,生产上要建立普通用户并调节适当参数,下面是以root身份安装运行.
下载解压并安装redis
maketest提示需要更高版本的tcl,跳到安装过程可能遇到的问题
wgethttp://download.redis.io/releases/redis-3.0.7.tar.gz tarxfredis-3.0.7.tar.gz cdredis-3.0.7 mkdir-p/opt/redis maketest makePREFIX=/opt/redisinstall
复制两个脚本到安装的目录
cp~/redis-3.0.7/src/redis-trib.rb/opt/redis/ cp~/redis-3.0.7/utils/create-cluster/create-cluster/opt/redis/1212
根据实际修改/opt/redis/create-cluster.改动的地方有几处
a.增加了三个变量BASEDIR,BINDIR和DATADIR,
b.修改相关命令路径,
c.start前,先进入DATADIR,start后,返回原目录
d.clean前,先进入DATADIR,start后,返回原目录
e.create的host由127.0.0.1改为192.168.1.194(不改有时会报ToomanyClusterredirections)
下面是修改后的shell
#!/bin/bash #Settings PORT=30000 TIMEOUT=2000 NODES=6 REPLICAS=1 BASEDIR=/opt/redis BINDIR=$BASEDIR/bin DATADIR=$BASEDIR/data #Youmaywanttoputtheaboveconfigparametersintoconfig.shinorderto #overridethedefaultswithoutmodifyingthisscript. if[-aconfig.sh] then source"config.sh" fi #Computedvars ENDPORT=$((PORT+NODES)) if["$1"=="start"] then cd$DATADIR while[$((PORT<ENDPORT))!="0"];do PORT=$((PORT+1)) echo"Starting$PORT" $BINDIR/redis-server--port$PORT--cluster-enabledyes--cluster-config-filenodes-${PORT}.conf--cluster-node-timeout$TIMEOUT--appendonlyyes--appendfilenameappendonly-${PORT}.aof--dbfilenamedump-${PORT}.rdb--logfile${PORT}.log--daemonizeyes done cd- exit0 fi if["$1"=="create"] then HOSTS="" while[$((PORT<ENDPORT))!="0"];do PORT=$((PORT+1)) HOSTS="$HOSTS192.168.1.194:$PORT" done $BASEDIR/redis-trib.rbcreate--replicas$REPLICAS$HOSTS exit0 fi if["$1"=="stop"] then while[$((PORT<ENDPORT))!="0"];do PORT=$((PORT+1)) echo"Stopping$PORT" $BINDIR/redis-cli-p$PORTshutdownnosave done exit0 fi if["$1"=="watch"] then PORT=$((PORT+1)) while[1];do clear date $BINDIR/redis-cli-p$PORTclusternodes|head-30 sleep1 done exit0 fi if["$1"=="tail"] then INSTANCE=$2 PORT=$((PORT+INSTANCE)) tail-f${PORT}.log exit0 fi if["$1"=="call"] then while[$((PORT<ENDPORT))!="0"];do PORT=$((PORT+1)) $BINDIR/redis-cli-p$PORT$2$3$4$5$6$7$8$9 done exit0 fi if["$1"=="clean"] then cd$DATADIR rm-rf*.log rm-rfappendonly*.aof rm-rfdump*.rdb rm-rfnodes*.conf cd- exit0 fi echo"Usage:$0[start|create|stop|watch|tail|clean]" echo"start--LaunchRedisClusterinstances." echo"create--Createaclusterusingredis-tribcreate." echo"stop--StopRedisClusterinstances." echo"watch--ShowCLUSTERNODESoutput(first30lines)offirstnode." echo"tail--Runtail-fofinstanceatbaseport+ID." echo"clean--Removeallinstancesdata,logs,configs."123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
不要忘了创建数据目录mkdir-p/opt/redis/data
根据上面的参考,启动集群和停止集群
启动集群:先敲入/opt/redis/create-clusterstart回车,再敲入/opt/redis/create-clustercreate回车,再输入yes回车
停止集群:敲入/opt/redis/create-clusterstop回车
如果以前启动过,造成不一致数据,create时就会报错,可先/opt/redis/create-clusterclean
测试
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency>
声明JedisClusterBean
@Bean publicJedisClusterjedisCluster(){ Set<HostAndPort>nodes=newHashSet<>(3); nodes.add(newHostAndPort("192.168.1.194",30001)); nodes.add(newHostAndPort("192.168.1.194",30002)); nodes.add(newHostAndPort("192.168.1.194",30003)); returnnewJedisCluster(nodes,2000,5); }
测试set和get
AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(AppConfig.class); JedisClusterjedisCluster=(JedisCluster)context.getBean("jedisCluster"); jedisCluster.set("xxx","123"); System.out.println("jedisCluster.get="+jedisCluster.get("xxx"));
安装过程可能遇到的问题:
maketest时,提醒Youneedtcl8.5ornewerinordertoruntheRedistest.到http://www.tcl.tk/software/tcltk/download.html下载Tcl,
wgethttp://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gz tarxftcl8.5.19-src.tar.gz cdtcl8.5.19/unix ./configure make maketest makeinstall
因为create-clustercreate会调用redis-trib.rb,它是一个ruby脚本,所以提示没有安装ruby,就先安装yuminstall-yruby
如果提示加载rubygems错误,使用以下办法安装rubygems
a.https://rubygems.org/pages/download下载tgz格式的安装包(wget可能不通,在windows用旋风或迅雷下载)
b.mount-tcifs-ousername=xiejx618,password=123456//192.168.1.115/share/share
cp/share/rubygems-2.6.4.tgz./ tarxfrubygems-2.6.4.tgz cdrubygems-2.6.4 rubysetup.rb
如果再提示nosuchfiletoload–rdoc/rdoc,就先安装yuminstall-yrdoc
如果再提示nosuchfiletoload–redis,就使用geminstallredis-v3.0.7
gem又是因为墙原因无法使用默认源,就修改为淘宝源
可能用到的几个命令
帮助:gemsources--help
查看源:gemsources-l
删除源:gemsources-rhttps://rubygems.org/
添加源:gemsources-ahttps://ruby.taobao.org/
更新源缓存:gemsources-u