配置 Redis
为了方便管理,我们将统一的配置放到 cluster.conf 文件中,别的文件引入该文件:
bind 127.0.0.1
protected-mode yes
port 6370
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./redis.log"
databases 100
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename master.rdb
dir ./
#slaveof 10.1.1.105 6379
#masterauth admin123
#将次 Redis 实例中启用 Redis Cluster 支持。否则,实例通常作为独立实例启动
cluster-enabled yes
#这是集群中的节点能够失联的最大时间,超过这个时间,该节点就会被认为故障。
cluster-node-timeout 15000
#此配置文件不能人工编辑,它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、他们的状态以及一些持久化参数等,方便在重启时恢复这些状态
cluster-config-file master-cluster.config
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
#密码,这里可以不设置,设置的话启动集群管理的时候会报错,下文会讲解错误的解决方法
requirepass admin123
appendonly no
appendfilename "appendonly6370.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-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.conf
port 6370
怎么样是不是很简单呢?
我们在每个端口的目录下启动各自的 redis :
[coderknock 6370]# redis-server 6370.conf
按照上面的模式我们将设置的 6 个 redis 启动。
安装 ruby
[root@iZ28glf0u1jZ ~]# sudo yum install ruby
Loaded plugins: security
Setting up Install Process
base | 3.7 kB 00:00
elrepo | 2.9 kB 00:00
epel | 4.3 kB 00:00
extras | 3.3 kB 00:00
fpco | 2.9 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 817 kB 00:00
Resolving 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 Resolution
Dependencies Resolved
============================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================
Installing:
ruby i686 1.8.7.374-5.el6 base 538 k
Installing 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 M
Transaction Summary
============================================================================================================================================
Install 3 Package(s)
Total download size: 2.3 M
Installed size: 7.8 M
Is this ok [y/N]: 也
Is this ok [y/N]: y
Downloading 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:02
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running 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/3
Installed:
ruby.i686 0:1.8.7.374-5.el6
Dependency Installed:
compat-readline5.i686 0:5.2-17.1.el6 ruby-libs.i686 0:1.8.7.374-5.el6
Complete!
安装 rubygems
[root@iZ28glf0u1jZ ~]# yum -y install ruby rubygems
Loaded plugins: security
Setting up Install Process
Package ruby-1.8.7.374-5.el6.i686 already installed and latest version
Resolving 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 Resolution
Dependencies Resolved
=======================================================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================================================
Installing:
rubygems noarch 1.3.7-5.el6 base 207 k
Installing 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 k
Transaction Summary
=======================================================================================================================================================================================================
Install 3 Package(s)
Total download size: 905 k
Installed size: 3.0 M
Downloading 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_debug
Running Transaction Test
Transaction Test Succeeded
Running 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:6370
127.0.0.1:6371
127.0.0.1:6372
Adding replica 127.0.0.1:6373 to 127.0.0.1:6370
Adding replica 127.0.0.1:6374 to 127.0.0.1:6371
Adding replica 127.0.0.1:6375 to 127.0.0.1:6372
M: f9acf4403c84c23ee9a79952e1f26a02cca40647 127.0.0.1:6370
slots:0-5460 (5461 slots) master
M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371
slots:5461-10922 (5462 slots) master
M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372
slots:10923-16383 (5461 slots) master
S: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373
replicates f9acf4403c84c23ee9a79952e1f26a02cca40647
S: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374
replicates f070ba8fedc4e9240c35b33684554080d0e2f7da
S: 429fea95c1428e6cfa39a3a0b0d67fa8e2c606d2 127.0.0.1:6375
replicates ae1502fbdfe2823dff2836ff61664da5affbd7e4
Can 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 cluster
Waiting 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) master
M: f070ba8fedc4e9240c35b33684554080d0e2f7da 127.0.0.1:6371
slots:5461-10922 (5462 slots) master
M: ae1502fbdfe2823dff2836ff61664da5affbd7e4 127.0.0.1:6372
slots:10923-16383 (5461 slots) master
M: b1c4a2eeff66c8d5f1997d3a3210120552f7bc72 127.0.0.1:6373
slots: (0 slots) master
replicates f9acf4403c84c23ee9a79952e1f26a02cca40647
M: 8d661ae944b842b21739aff41ce7005ed30ccaad 127.0.0.1:6374
slots: (0 slots) master
replicates f070ba8fedc4e9240c35b33684554080d0e2f7da
M: 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 admin123
127.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 aa123
OK
127.0.0.1:6370> get admin
"aa123"
127.0.0.1:6370> quit
[coderknock redis]# redis-cli -c -p 6370 -a admin123
127.0.0.1:6370> get admin
"aa123"
127.0.0.1:6370>
[coderknock redis]# redis-cli -c -p 6373 -a admin123
127.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 73
OK
127.0.0.1:6370> get admin
"73"
127.0.0.1:6370> quit
[coderknock redis]# redis-cli -c -p 6370 -a admin123
127.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 Set getClusterInfo(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 月份就要开始了,希望小伙伴们支持一下,现在报名有优惠噢
https://segmentfault.com/l/15...
https://segmentfault.com/l/15...