#配置 Redis
为了方便管理,我们将统一的配置放到 cluster.conf 文件中,别的文件引入该文件:
bind 127.0.0.1protected-mode yesport 6370tcp-backlog 511timeout 0tcp-keepalive 0loglevel noticelogfile "./redis.log"databases 100save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename master.rdbdir ./#slaveof 10.1.1.105 6379#masterauth admin123#将次 Redis 实例中启用 Redis Cluster 支持。否则,实例通常作为独立实例启动 cluster-enabled yes #这是集群中的节点能够失联的最大时间,超过这个时间,该节点就会被认为故障。 cluster-node-timeout 15000 #此配置文件不能人工编辑,它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、他们的状态以及一些持久化参数等,方便在重启时恢复这些状态cluster-config-file master-cluster.configslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100#密码,这里可以不设置,设置的话启动集群管理的时候会报错,下文会讲解错误的解决方法requirepass admin123appendonly noappendfilename "appendonly6370.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
文件目录如下:
└── cluster ├── 6370 │ ├── 6370.conf │ ├── master-cluster.config │ ├── master.rdb │ └── redis.log ├── 6371 │ ├── 6371.conf │ ├── master-cluster.config │ ├── master.rdb │ └── redis.log ├── 6372 │ ├── 6372.conf │ ├── master-cluster.config │ └── redis.log ├── 6373 │ ├── 6373.conf │ ├── master-cluster.config │ └── redis.log ├── 6374 │ ├── 6374.conf │ ├── master-cluster.config │ └── redis.log ├── 6375 │ ├── 6375.conf │ ├── master-cluster.config │ └── redis.log └── cluster.conf
其中文件夹名为端口号,我们只拿 6370 看一眼看(别的只是修改一下端口号):
include /root/redis/cluster/cluster.confport 6370
怎么样是不是很简单呢?
我们在每个端口的目录下启动各自的 redis :
[coderknock 6370]# redis-server 6370.conf
按照上面的模式我们将设置的 6 个 redis 启动。
安装 ruby
[root@iZ28glf0u1jZ ~]# sudo yum install rubyLoaded plugins: securitySetting up Install Processbase | 3.7 kB 00:00elrepo | 2.9 kB 00:00epel | 4.3 kB 00:00extras | 3.3 kB 00:00fpco | 2.9 kB 00:00updates | 3.4 kB 00:00updates/primary_db | 817 kB 00:00Resolving Dependencies--> Running transaction check---> Package ruby.i686 0:1.8.7.374-5.el6 will be installed--> Processing Dependency: ruby-libs = 1.8.7.374-5.el6 for package: ruby-1.8.7.374-5.el6.i686--> Processing Dependency: libruby.so.1.8 for package: ruby-1.8.7.374-5.el6.i686--> Running transaction check---> Package ruby-libs.i686 0:1.8.7.374-5.el6 will be installed--> Processing Dependency: libreadline.so.5 for package: ruby-libs-1.8.7.374-5.el6.i686--> Running transaction check---> Package compat-readline5.i686 0:5.2-17.1.el6 will be installed--> Finished Dependency ResolutionDependencies Resolved============================================================================================================================================ Package Arch Version Repository Size============================================================================================================================================Installing: ruby i686 1.8.7.374-5.el6 base 538 kInstalling for dependencies: compat-readline5 i686 5.2-17.1.el6 base 128 k ruby-libs i686 1.8.7.374-5.el6 base 1.6 MTransaction Summary============================================================================================================================================Install 3 Package(s)Total download size: 2.3 MInstalled size: 7.8 MIs this ok [y/N]: 也Is this ok [y/N]: yDownloading Packages:(1/3): compat-readline5-5.2-17.1.el6.i686.rpm | 128 kB 00:00(2/3): ruby-1.8.7.374-5.el6.i686.rpm | 538 kB 00:00(3/3): ruby-libs-1.8.7.374-5.el6.i686.rpm | 1.6 MB 00:01--------------------------------------------------------------------------------------------------------------------------------------------Total 1.1 MB/s | 2.3 MB 00:02Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction Installing : compat-readline5-5.2-17.1.el6.i686 1/3 Installing : ruby-libs-1.8.7.374-5.el6.i686 2/3 Installing : ruby-1.8.7.374-5.el6.i686 3/3 Verifying : compat-readline5-5.2-17.1.el6.i686 1/3 Verifying : ruby-1.8.7.374-5.el6.i686 2/3 Verifying : ruby-libs-1.8.7.374-5.el6.i686 3/3Installed: ruby.i686 0:1.8.7.374-5.el6Dependency Installed: compat-readline5.i686 0:5.2-17.1.el6 ruby-libs.i686 0:1.8.7.374-5.el6Complete!
安装 rubygems
[root@iZ28glf0u1jZ ~]# yum -y install ruby rubygemsLoaded plugins: securitySetting up Install ProcessPackage ruby-1.8.7.374-5.el6.i686 already installed and latest versionResolving Dependencies--> Running transaction check---> Package rubygems.noarch 0:1.3.7-5.el6 will be installed--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch--> Running transaction check---> Package ruby-rdoc.i686 0:1.8.7.374-5.el6 will be installed--> Processing Dependency: ruby-irb = 1.8.7.374-5.el6 for package: ruby-rdoc-1.8.7.374-5.el6.i686--> Running transaction check---> Package ruby-irb.i686 0:1.8.7.374-5.el6 will be installed--> Finished Dependency ResolutionDependencies Resolved======================================================================================================================================================================================================= Package Arch Version Repository Size=======================================================================================================================================================================================================Installing: rubygems noarch 1.3.7-5.el6 base 207 kInstalling for dependencies: ruby-irb i686 1.8.7.374-5.el6 base 318 k ruby-rdoc i686 1.8.7.374-5.el6 base 381 kTransaction Summary=======================================================================================================================================================================================================Install 3 Package(s)Total download size: 905 kInstalled size: 3.0 MDownloading Packages:(1/3): ruby-irb-1.8.7.374-5.el6.i686.rpm | 318 kB 00:00 (2/3): ruby-rdoc-1.8.7.374-5.el6.i686.rpm | 381 kB 00:00 (3/3): rubygems-1.3.7-5.el6.noarch.rpm | 207 kB 00:00 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total 1.7 MB/s | 905 kB 00:00 Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction Installing : ruby-irb-1.8.7.374-5.el6.i686 1/3 Installing : ruby-rdoc-1.8.7.374-5.el6.i686 2/3 Installing : rubygems-1.3.7-5.el6.noarch 3/3 Verifying : ruby-rdoc-1.8.7.374-5.el6.i686 1/3 Verifying : rubygems-1.3.7-5.el6.noarch 2/3 Verifying : ruby-irb-1.8.7.374-5.el6.i686 3/3 Installed: rubygems.noarch 0:1.3.7-5.el6 Dependency Installed: ruby-irb.i686 0:1.8.7.374-5.el6 ruby-rdoc.i686 0:1.8.7.374-5.el6 Complete!
安装配置集群管理
安装 ruby 下的 redis 管理工具
gem install redis
启动集群管理
找到 redis 源码包中 src 下的 redis-trib.rb 或者下载一个该文件也可以
[coderknock redis]# /root/redis-3.2.0/src/redis-trib.rb create --replicas 1 127.0.0.1:6370 127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373 127.0.0.1:6374 127.0.0.1:6375>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:6370127.0.0.1:6371127.0.0.1:6372Adding replica 127.0.0.1:6373 to 127.0.0.1:6370Adding replica 127.0.0.1:6374 to 127.0.0.1:6371Adding replica 127.0.0.1:6375 to 127.0.0.1:6372M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370 slots:0-5460 (5461 slots) masterM: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371 slots:5461-10922 (5462 slots) masterM: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372 slots:10923-16383 (5461 slots) masterS: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373 replicates f9acf4403c84c23ee9a79952e1f26a02cca40647S: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374 replicates f070ba8fedc4e9240c35b33684554080d0e2f7daS: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375 replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4Can I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join.....>>> Performing Cluster Check (using node 127.0.0.1:6370)M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370 slots:0-5460 (5461 slots) masterM: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371 slots:5461-10922 (5462 slots) masterM: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372 slots:10923-16383 (5461 slots) masterM: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373 slots: (0 slots) master replicates f9acf4403c84c23ee9a79952e1f26a02cca40647M: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374 slots: (0 slots) master replicates f070ba8fedc4e9240c35b33684554080d0e2f7daM: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375 slots: (0 slots) master replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
这样就启动成功了。
[ERR] Sorry, can't connect to node 127.0.0.1:6370
出现这个可能的原因是 ruby 版本太低,升级之前安装的那几个东西。还有可能就是设置了 redis 密码的缘故。
找到 usr/lib/ruby/gems/1.8/gems/redis-3.3.3/lib/redis,编辑 client.rb:
require "redis/errors"require "socket"require "cgi"class Redis class Client DEFAULTS = { :url => lambda { ENV["REDIS_URL"] }, :scheme => "redis", :host => "127.0.0.1", :port => 6379, :path => nil, :timeout => 5.0, :password => "admin123",//这里设置你的密码 :db => 0, :driver => nil, :id => nil, :tcp_keepalive => 0, :reconnect_attempts => 1, :inherit_socket => false } def options Marshal.load(Marshal.dump(@options)) end def scheme @options[:scheme] end def host @options[:host] end def port @options[:port] end def path @options[:path]
然后就可以正常启动了。
测试
集群中使用 redis-cli 应该 使用 -c 参数启动,并且不支持 select 等操作(具体可以查看官网)
[coderknock redis]# redis-cli -c -p 6371 -a admin123127.0.0.1:6371> keys *(empty list or set)127.0.0.1:6371> get admin-> Redirected to slot [1358] located at 127.0.0.1:6370"123"127.0.0.1:6370> set admin aa123OK127.0.0.1:6370> get admin"aa123"127.0.0.1:6370> quit[coderknock redis]# redis-cli -c -p 6370 -a admin123127.0.0.1:6370> get admin"aa123"127.0.0.1:6370> [coderknock redis]# redis-cli -c -p 6373 -a admin123127.0.0.1:6373> get admin-> Redirected to slot [1358] located at 127.0.0.1:6370"aa123"127.0.0.1:6370> set admin 73OK127.0.0.1:6370> get admin"73"127.0.0.1:6370> quit[coderknock redis]# redis-cli -c -p 6370 -a admin123127.0.0.1:6370> get admin"73"
#Java Jedis 访问
import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPoolConfig;import java.util.HashSet;import java.util.Set;/** * * * @author 三产 * @version 1.0 * @date 2017-05-25 * @QQGroup 213732117 * @website http://www.coderknock.com * @copyright Copyright 2017 拿客 coderknock.com All rights reserved. * @since JDK 1.8 */public class Test { /** * 获取配置的主从 Redis * * @param serverInfo * @return */ private static SetgetClusterInfo(String serverInfo) { Set set = new HashSet (); if (serverInfo == null || "".equals(serverInfo.length())) { throw new RuntimeException("The serverInfo can not be empty"); } String ipPort[] = serverInfo.split(","); int len = ipPort.length; for (int i = 0; i < len; i++) { String server[] = ipPort[i].split(":"); set.add(new HostAndPort(server[0], Integer.parseInt(server[1]))); } return set; } public static void main(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(20); String redis_url = "127.0.0.1:6370,127.0.0.1:6371,127.0.0.1:6372,127.0.0.1:6373"; String auth = "admin123"; int timeout = 1000; Set jedisClusterNodes = getClusterInfo(redis_url); JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, timeout, config, auth); System.out.println(jedisCluster.get(1, "admin")); } //Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled //Exception in thread "main" redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster}
本人的直播课程在 7 月份就要开始了,希望小伙伴们支持一下,现在报名有优惠噢