首页 / 操作系统 / Linux / 在RedHat 6.4安装Redis集群
在RedHat 6.4安装Redis集群
下载解压并安装redis
make test提示需要更高版本的tcl,跳到安装过程可能遇到的问题wget http://download.redis.io/releases/redis-3.0.7.tar.gztar xf redis-3.0.7.tar.gz cd redis-3.0.7mkdir -p /opt/redismake testmake PREFIX=/opt/redis install复制两个脚本到安装的目录cp ~/redis-3.0.7/src/redis-trib.rb /opt/redis/cp ~/redis-3.0.7/utils/create-cluster/create-cluster /opt/redis/根据实际修改/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(不改有时会报Too many Cluster redirections)下面是修改后的shell#!/bin/bash# SettingsPORT=30000TIMEOUT=2000NODES=6REPLICAS=1BASEDIR=/opt/redisBINDIR=$BASEDIR/binDATADIR=$BASEDIR/data# You may want to put the above config parameters into config.sh in order to# override the defaults without modifying this script.if [ -a config.sh ]thensource "config.sh"fi# Computed varsENDPORT=$((PORT+NODES))if [ "$1" == "start" ]thencd $DATADIRwhile [ $((PORT < ENDPORT)) != "0" ]; doPORT=$((PORT+1))echo "Starting $PORT"$BINDIR/redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yesdonecd -exit 0fiif [ "$1" == "create" ]thenHOSTS=""while [ $((PORT < ENDPORT)) != "0" ]; doPORT=$((PORT+1))HOSTS="$HOSTS 192.168.1.194:$PORT"done$BASEDIR/redis-trib.rb create --replicas $REPLICAS $HOSTSexit 0fiif [ "$1" == "stop" ]thenwhile [ $((PORT < ENDPORT)) != "0" ]; doPORT=$((PORT+1))echo "Stopping $PORT"$BINDIR/redis-cli -p $PORT shutdown nosavedoneexit 0fiif [ "$1" == "watch" ]thenPORT=$((PORT+1))while [ 1 ]; docleardate$BINDIR/redis-cli -p $PORT cluster nodes | head -30sleep 1doneexit 0fiif [ "$1" == "tail" ]thenINSTANCE=$2PORT=$((PORT+INSTANCE))tail -f ${PORT}.logexit 0fiif [ "$1" == "call" ]thenwhile [ $((PORT < ENDPORT)) != "0" ]; doPORT=$((PORT+1))$BINDIR/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9doneexit 0fiif [ "$1" == "clean" ]thencd $DATADIRrm -rf *.logrm -rf appendonly*.aofrm -rf dump*.rdbrm -rf nodes*.confcd -exit 0fiecho "Usage: $0 [start|create|stop|watch|tail|clean]"echo "start -- Launch Redis Cluster instances."echo "create-- Create a cluster using redis-trib create."echo "stop-- Stop Redis Cluster instances."echo "watch -- Show CLUSTER NODES output (first 30 lines) of first node."echo "tail <id> -- Run tail -f of instance at base port + ID."echo "clean -- Remove all instances data, logs, configs."不要忘了创建数据目录mkdir -p /opt/redis/data根据上面的参考,启动集群和停止集群
启动集群:先敲入/opt/redis/create-cluster start回车,再敲入/opt/redis/create-cluster create回车,再输入yes回车
停止集群:敲入/opt/redis/create-cluster stop回车
如果以前启动过,造成不一致数据,create时就会报错,可先/opt/redis/create-cluster clean测试
主要依赖<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.1</version></dependency>声明JedisCluster Bean @Beanpublic JedisCluster jedisCluster(){Set<HostAndPort> nodes=new HashSet<>(3);nodes.add(new HostAndPort("192.168.1.194",30001));nodes.add(new HostAndPort("192.168.1.194",30002));nodes.add(new HostAndPort("192.168.1.194",30003));return new JedisCluster(nodes,2000,5);}测试set和getAnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class);JedisCluster jedisCluster = (JedisCluster) context.getBean("jedisCluster");jedisCluster.set("xxx","123");System.out.println("jedisCluster.get = " + jedisCluster.get("xxx"));安装过程可能遇到的问题:
make test时,提醒You need tcl 8.5 or newer in order to run the Redis test.到http://www.tcl.tk/software/tcltk/download.html下载Tcl,wget http://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gztar xf tcl8.5.19-src.tar.gzcdtcl8.5.19/unix./configuremakemake testmake install因为create-cluster create会调用redis-trib.rb,它是一个ruby脚本,所以提示没有安装ruby,就先安装yum install -y ruby
如果提示加载rubygems错误,使用以下办法安装rubygems
a.https://rubygems.org/pages/download下载tgz格式的安装包(wget可能不通,在windows用旋风或迅雷下载)
b.mount -t cifs -o username=xiejx618,password=123456 //192.168.1.115/share /sharecp /share/rubygems-2.6.4.tgz ./tar xf rubygems-2.6.4.tgzcd rubygems-2.6.4ruby setup.rb如果再提示no such file to load – rdoc/rdoc,就先安装yum install -y rdoc
如果再提示 no such file to load – redis,就使用gem install redis -v 3.0.7
gem又是因为墙原因无法使用默认源,就修改为淘宝源
可能用到的几个命令
帮助:gem sources --help
查看源:gem sources -l
删除源:gem sources -r https://rubygems.org/
添加源:gem sources -a https://ruby.taobao.org/
更新源缓存:gem sources -u参考:
http://redis.io/topics/cluster-tutorial(主要是Creating a Redis Cluster using the create-cluster script部分)
https://ruby.taobao.org/下面关于Redis的文章您也可能喜欢,不妨参考下:Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htmRedis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htmRedis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htmUbuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htmRedis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htmCentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htmRedis安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htmRedis配置文件redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htmRedis 的详细介绍:请点这里
Redis 的下载地址:请点这里本文永久更新链接地址