安装系统是在centOS上面时行的。有不足之处还记指正。
系统版本centOS6.5.
redis集群配置如下:
节点1
=============================================
主192.168.135.128 :1000
/usr/local/redis-cluster-test/cluster1/master
从 192.168.135.128 :1001
/usr/local/redis-cluster-test/cluster1/slave
节点1主配置
pidfile /usr/local/redis-cluster-test/cluster1/master/redis_1000.pid
logfile /usr/local/redis-cluster-test/cluster1/master/redist-1000.log
port 1000
bind 192.168.135.128
appendonly yes
daemonize yes
dir /usr/local/redis-cluster-test/cluster1/master/
cluster-enabled yes
cluster-config-file nodes-1000.conf
节点1从配置
pidfile /usr/local/redis-cluster-test/cluster1/slave/redis_1001.pid
logfile /usr/local/redis-cluster-test/cluster1/salve/redist-1001.log
port 1001
bind 192.168.135.128
appendonly yes
daemonize yes
dir /usr/local/redis-cluster-test/cluster1/slave/
cluster-enabled yes
cluster-config-file nodes-1001.conf
节点2
=============================================
节点2主192.168.135.128 :2000
/usr/local/redis-cluster-test/cluster2/master
/usr/local/redis-cluster-test/cluster2/slave
pidfile /usr/local/redis-cluster-test/cluster2/slave/redis_2001.pid
logfile /usr/local/redis-cluster-test/cluster2/salve/redist-2001.log
port 2001
bind 192.168.135.128
appendonly yes
daemonize yes
cluster-enabled yes
dir /usr/local/redis-cluster-test/cluster1/slave/
cluster-config-file nodes-2000.conf
节点2从 192.168.135.128 :2001
/usr/local/redis-cluster-test/cluster2/slave
pidfile /usr/local/redis-cluster-test/cluster2/slave/redis_2001.pid
logfile /usr/local/redis-cluster-test/cluster2/salve/redist-2001.log
port 2001
bind 192.168.135.128
appendonly yes
daemonize yes
dir /usr/local/redis-cluster-test/cluster1/slave/
cluster-enabled yes
cluster-config-file nodes-2001.conf
节点3
=============================================
节点3主192.168.135.128 :3000
/usr/local/redis-cluster-test/cluster3/master
/usr/local/redis-cluster-test/cluster2/slave
pidfile /usr/local/redis-cluster-test/cluster3/slave/redis_3000.pid
logfile /usr/local/redis-cluster-test/cluster3/salve/redist-3000.log
port 3000
bind 192.168.135.128
appendonly yes
daemonize yes
cluster-enabled yes
dir /usr/local/redis-cluster-test/cluster1/slave/
cluster-config-file nodes-3000.conf
节点3从 192.168.135.128 :3001
redis.conf /usr/local/redis-cluster-test/cluster3/slave
pidfile /usr/local/redis-cluster-test/cluster3/slave/redis_3001.pid
logfile /usr/local/redis-cluster-test/cluster3/salve/redist-3001.log
port 3001
bind 192.168.135.128
appendonly yes
daemonize yes
dir /usr/local/redis-cluster-test/cluster1/slave/
cluster-enabled yes
cluster-config-file nodes-3001.conf
以上配置在从中设置少了一个cluster-node-timeout 15000这个配置可以自行设置。
下面就是主题了,搞下集群。
也有一些野路子配置集群的,在网上看到,试过了,也可以用,但是在删除和增加节点时不是那么灵活。官方文档上推荐使用ruby,而且redis3.2里面内置了redis-trib.rb文件,所以在centOS上面安装ruby就可以了。可是,我是纠结了一下午才搞定了ruby的运行环境,可能是我安装的centos太纯净了,有些依赖包没有找到的问题。
一些问题如下:
问题1:
执行命令:./redis-trib.rb
/usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- redis (LoadError)
from /usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from ./redis-trib.rb:25:in `<main>'
解决方法:下载安装rubygem 进入解压包中.configure make && make install
此时就可以运行gem intall redis 命令,来安装ruby执行redis的相关依赖。
问题2:
gem install redis 命令出现的问题
ERROR: Loading command: install (LoadError)
cannot load such file -- openssl
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass手动安装opensll,此问题解决。
问题3:
接着执行命令:gem install redis
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
手动安装zlib包解决(这个可能是系统太纯净,没有些包的问题)。安装过程不多说,手动下载安装包安装。
问题3:
接着执行命令:gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
网上搜索到的解决方法: gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
还是问题以旧。
解决方法:
gem sources -a https://gems.ruby-china.org/ 把https地址改成 gem sources -a http://gems.ruby-china.org/
执行成功,查看gem source -l 镜像地址修改成功。
再次执行:gem install redis成功。
通过命令查看:
gem list 已经成功安装了redis的依赖。
执行redis 目录下的ruby-trib.rb
命令格式如下:
./redis-trib.rb create --replicas 1 192.168.135.128:1000 192.168.135.128:2000
192.168.135.128:3000 192.168.135.128:1001 192.168.135.128:2001 192.168.135.128:3001
注意:--replicas 1 代表每个master有一个slave,还有就是前面三个是主服务,后面三个从服务地址,集群配置成功。
如图所示:
以上是在个人配置集群的时候碰到的问题。
java 程序测试集群:
JedisPoolConfig poolConfig=new JedisPoolConfig(); Set<HostAndPort> nodeList=new HashSet<HostAndPort>(); nodeList.add(new HostAndPort(host,1000)); nodeList.add(new HostAndPort(host,1001)); nodeList.add(new HostAndPort(host,2000)); nodeList.add(new HostAndPort(host,2001)); nodeList.add(new HostAndPort(host,3000)); nodeList.add(new HostAndPort(host,3001)); JedisCluster cluster=new JedisCluster(nodeList,poolConfig); String name=cluster.get("name"); System.out.println("args = [" + name + "]");程序运行效果如下: