想了好久还是自己总结下这次安装redis-cluster遇到的各种问题和安装步骤,以便下次进行安装不会出错
其中是在虚拟机上进行的,UBUNTU是16.04service版,redis是3.2.9版本。
一、redis
1、关于redis的介绍自己参看别的博文吧,网上很多关于说了redis的优点,记得又一次面试了一家公司问及redis和memcached的区别,当时竟然无从开口,现在感觉好惭愧;
2、我此次搭建redis-cluster主要参考了博文 http://blog.csdn.net/zh520qx/article/details/54577753,由于博主要求就没有转载了,但有的地方 可能并不适用于任何系统,所以自己才写了这个随笔;
二、redis-cluster搭建的准备步骤
1、安装redis
我是安装在 /opt 路径下 所以先进入/opt下进行以下操作
下载 $ wget http://download.redis.io/releases/redis-3.2.9.tar.gz
解压 $ tar xzf redis-3.2.9.tar.gz
进入目录 $ cd /opt/redis-3.2.9
编译 $ make
编译成功之后在 /opt/redis-3.2.9/src 下有redis-server 可以启动试下是否安装redis成功 ./redis-server
网上其实还有别的安装redis方法这里就不多说了
2、安装集群的一些依赖
2.1)、安装zlib
$ sudo apt-get install zlib1g-dev 或者 $ sudo apt-get install zlib1g2.2)、安装ruby环境
$ sudo apt-get install ruby 其实这篇文章不错 介绍了ubuntu多种安装ruby环境的方法 http://blog.csdn.net/chszs/article/details/42462517
2.3)、安装ruby 的redis库
gem install redis
如果没有gem,就按照提示安装再执行上面命令就行
3、创建redis实例
3.1)创建实例目录
$ mkdir /usr/local/redis-cluster/redis{7001,7002,7003,7004,7005,7006,7007,7008}
3.2) 复制redis.conf redis-server redis-cli 到实例目录下
for dir in /usr/local/redis-cluster/redis700*; do cp -v /opt/redis-3.2.9/redis.conf $dir ; done
for dir in /usr/local/redis-cluster/redis700*; do cp -v /opt/redis-3.2.9/src/redis-server $dir ; done
for dir in /usr/local/redis-cluster/redis700*; do cp -v /opt/redis-3.2.9/src/redis-cli $dir ; done
这个复制详细说明庆参考博文 http://blog.csdn.net/maowenbin/article/details/7758367
也可以单个复制
cp /usr/local/redis-cluster/redis7001 /opt/redis-3.2.9/src/redis-server
cp /usr/local/redis-cluster/redis7002 /opt/redis-3.2.9/src/redis-server
cp /usr/local/redis-cluster/redis7003 /opt/redis-3.2.9/src/redis-server
cp /usr/local/redis-cluster/redis7004 /opt/redis-3.2.9/src/redis-servercp /usr/local/redis-cluster/redis7005 /opt/redis-3.2.9/src/redis-server
cp /usr/local/redis-cluster/redis7006 /opt/redis-3.2.9/src/redis-servercp /usr/local/redis-cluster/redis7007 /opt/redis-3.2.9/src/redis-server
cp /usr/local/redis-cluster/redis7008 /opt/redis-3.2.9/src/redis-servercp /usr/local/redis-cluster/redis7001 /opt/redis-3.2.9/src/redis-cli
cp /usr/local/redis-cluster/redis7002 /opt/redis-3.2.9/src/redis-cli
cp /usr/local/redis-cluster/redis7003 /opt/redis-3.2.9/src/redis-clicp /usr/local/redis-cluster/redis7004 /opt/redis-3.2.9/src/redis-clicp /usr/local/redis-cluster/redis7005 /opt/redis-3.2.9/src/redis-clicp /usr/local/redis-cluster/redis7006 /opt/redis-3.2.9/src/redis-cli
cp /usr/local/redis-cluster/redis7007 /opt/redis-3.2.9/src/redis-cli
cp /usr/local/redis-cluster/redis7008 /opt/redis-3.2.9/src/redis-cli
3.3)修改配置参数
for dir in /usr/local/redis-cluster/redis700*; domyport=`basename $dir|sed 's/[a-zA-Z]*//g'`sed -r -i "s@^\s*port.*@port $myport@;s@^\s*daemonize.*@daemonize yes@;s@^\s*pidfile.*@pidfile /var/run/redis_$myport.pid@;s@^\s*logfile.*@logfile /var/log/redis_$myport.log@;s@^\s*#?\s*cluster-enabled.*@cluster-enabled yes@;s@^\s*#?\s*cluster-config-file.*@cluster-config-file $dir/nodes-$myport.conf@;s@^\s*#?\s*cluster-node-timeout.*@cluster-node-timeout 15000@;s@^\s*dir.*@dir $dir@;" $dir/redis.confdone
执行上面命令之后发现配置文件redis.conf里的一项 bing 127.0.0.1这个在创建集群后会导致失败,因此就手动改成 bind 192.168.194
3.4)启动所有实例
for dir in /usr/local/redis-cluster/redis700*; do /opt/redis-3.2.9/src/redis-server $dir/redis.conf ; done
查看实例是否启动成功
$ ps -ef|grep redis
3.5)通过redis-trib.rb创建集群
由于我的redis-trib.rb在/opt/redis-3.2.9/src/,你也可以复制到别的目录下方便启动
./opt/redis-3.2.9/src/redis-trib.rb create --replicas 1 192.168.194.130:7001 192.168.194.130:7002 192.168.194.130:7003 192.168.194.130:7004 192.168.194.130:7005 192.168.194.130:7006
3.6)启动redis集群服务
我是分别进入 /usr/local/redis-cluster/redis700* 每个目录下,分别执行 ./redis-server redis.conf
现在写了个脚本,每次执行脚本就行了
每次执行 $ ./redis.sh就可以了,内容如下
#!/bin/bash
cd /usr/local/redis-cluster/redis7001./redis-server redis.confcd /usr/local/redis-cluster/redis7002./redis-server redis.confcd /usr/local/redis-cluster/redis7003./redis-server redis.confcd /usr/local/redis-cluster/redis7004./redis-server redis.confcd /usr/local/redis-cluster/redis7005./redis-server redis.confcd /usr/local/redis-cluster/redis7006./redis-server redis.confcd /usr/local/redis-cluster/redis7007./redis-server redis.confcd /usr/local/redis-cluster/redis7008./redis-server redis.conf
3.6)连接redis集群
命令连接:./redis-cli -h 192.168.194.130 -p 7001 -c
java 连接
public class TestRedisCluster {
/** * @param args */ public static void main(String[] args) { Set<HostAndPort> nodes = new HashSet<HostAndPort>(); nodes.add(new HostAndPort("192.168.194.130", 7001)); nodes.add(new HostAndPort("192.168.194.130", 7002)); nodes.add(new HostAndPort("192.168.194.130", 7003)); JedisCluster cluster = new JedisCluster(nodes ); cluster.set("key1", "100wwwww0"); System.out.println("key1="+cluster.get("key1")); }}至此redis集群搭建结束,至于动态添加节点,删除节点之类的可以参考别的博客,后续会完善笔记..............................................................
最后要关闭redis 的命令 ./redis-cli -h 192.168.194.130 -p 7003 shutdown