Redis--集群

  redis集群是一个无中心的分布式Redis存储架构,可以在多个节点之间进行数据共享,解决了Redis高可用、可扩展等问题。redis集群提供了以下两个好处:

  1. 将数据自动切分(split)到多个节点
  2. 当集群中的某一个节点故障时,redis还可以继续处理客户端的请求。
  3. 一个 Redis 集群包含 16384 个哈希槽(hash slot),数据库中的每个数据都属于这16384个哈希槽中的一个。集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽。集群中的每一个节点负责处理一部分哈希槽
  • 集群中的主从复制:
    集群中的每个节点都有1个至N个复制品,其中一个为主节点,其余的为从节点如果主节点下线了,集群就会把这个主节点的一个从节点设置为新的主节点,继续工作这样集群就不会因为一个主节点的下线而无法正常工作

  • 注意:
    如果某一个主节点和他所有的从节点都下线的话,redis集群就会停止工作了
    redis集群不保证数据的强一致性,在特定的情况下redis集群会丢失已经被执行过的写命令使用异步复制(asynchronous replication)是 Redis 集群可能会丢失写命令的其中一个原因,有时候由于网络原因,如果网络断开时间太长,redis集群就会启用新的主节点,之前发给主节点的数据就会丢失

redis集群安装配置

  1. 修改配置文件redis.conf

    1. daemonize yes
    2. port 7000
    3. cluster-enabled yes
    4. cluster-config-file nodes.conf
    5. cluster-node-timeout 5000
      要让集群正常运作至少需要三个主节点
  2. 创建集群命令
    ./redis-trib.rb create –replicas 1 192.168.1.160:7000 192.168.1.160:7001 192.168.1.160:7002 192.168.1.160:7003 192.168.1.160:7004 192.168.1.160:7005

  • 示例代码 : 这里我们在单机上模拟集群搭建
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    ########0. 准备服务器################################
    ip: 这里我们使用192.168.33.88搭建六个redis
    port:使用7000、7001、7002、7003、7004、7005

    ########1. 在/usr/local/创建一个cluster目录###########
    ########2. 复制配置文件到cluster/目录##################
    ########3. 创建7001-7005目录##########################
    [root@momo1 cluster]# ll
    总用量 68
    drwxr-xr-x. 2 root root 4096 4月 29 01:27 7000
    drwxr-xr-x. 2 root root 4096 4月 29 01:30 7001
    drwxr-xr-x. 2 root root 4096 4月 29 01:29 7002
    drwxr-xr-x. 2 root root 4096 4月 29 01:28 7003
    drwxr-xr-x. 2 root root 4096 4月 29 01:28 7004
    drwxr-xr-x. 2 root root 4096 4月 29 01:28 7005
    -rw-r--r--. 1 root root 41550 4月 29 01:27 redis.conf

    ########4. 复制redis.conf文件到每个目录################
    [root@momo1 cluster]# cp ./redis.conf 7000
    [root@momo1 cluster]# cp ./redis.conf 7001
    [root@momo1 cluster]# cp ./redis.conf 7002
    [root@momo1 cluster]# cp ./redis.conf 7003
    [root@momo1 cluster]# cp ./redis.conf 7004
    [root@momo1 cluster]# cp ./redis.conf 7005

    ########4. 修改配置文件端口号##########################
    [root@momo1 cluster]# vim ./redis.conf ##查看行号
    49 # If port 0 is specified Redis will not listen on a TCP socket.
    50 port 7000
    51
    52 # TCP listen() backlog.

    查看到端口号在50行于是使用sed命令修改
    [root@momo1 cluster]# sed -i "50s/7000/7001/" 7001/redis.conf
    [root@momo1 cluster]# sed -i "50s/7000/7002/" 7002/redis.conf
    [root@momo1 cluster]# sed -i "50s/7000/7003/" 7003/redis.conf
    [root@momo1 cluster]# sed -i "50s/7000/7004/" 7004/redis.conf
    [root@momo1 cluster]# sed -i "50s/7000/7005/" 7005/redis.conf

    ########5. 分别启动这6个redis实例######################
    [root@momo1 cluster]# cd 7000/
    [root@momo1 7000]# redis-server ./redis.conf
    [root@momo1 7000]# cd ../7001/
    [root@momo1 7001]# redis-server ./redis.conf
    [root@momo1 7001]# cd ../7002/
    [root@momo1 7002]# redis-server ./redis.conf
    [root@momo1 7002]# cd ../7003/
    [root@momo1 7003]# redis-server ./redis.conf
    [root@momo1 7003]# cd ../7004/
    [root@momo1 7004]# redis-server ./redis.conf
    [root@momo1 7004]# cd ../7005/
    [root@momo1 7005]# redis-server ./redis.conf
    [root@momo1 7005]# ps -ef|grep redis
    root 7694 1 0 06:30 ? 00:00:00 redis-server *:7000 [cluster]
    root 7698 1 0 06:31 ? 00:00:00 redis-server *:7001 [cluster]
    root 7702 1 0 06:31 ? 00:00:00 redis-server *:7002 [cluster]
    root 7712 1 0 06:31 ? 00:00:00 redis-server *:7003 [cluster]
    root 7716 1 0 06:31 ? 00:00:00 redis-server *:7004 [cluster]
    root 7720 1 0 06:31 ? 00:00:00 redis-server *:7005 [cluster]
    root 7726 5794 0 06:32 pts/2 00:00:00 grep redis

    ########6. 执行redis的创建集群命令创建集群################
    [root@momo1 src]# ./redis-trib.rb create --replicas 1 192.168.33.88:7000 192.168.33.88:7001 192.168.33.88:7002 192.168.33.88:7003 192.168.33.88:7004 192.168.33.88:7005

    如报错:/usr/bin/env: ruby: 没有那个文件或目录 ,安装ruby:yum -y install ruby
    如报错:./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
    from ./redis-trib.rb:24 安装ruby:yum -y install rubygems
    错误内容:
    /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)

    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from ./redis-trib.rb:25 使用gem 安装:gem install redis

    #####正常运行,输入yes
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    192.168.33.88:7000
    192.168.33.88:7001
    192.168.33.88:7002
    Adding replica 192.168.33.88:7003 to 192.168.33.88:7000
    Adding replica 192.168.33.88:7004 to 192.168.33.88:7001
    Adding replica 192.168.33.88:7005 to 192.168.33.88:7002
    M: 7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000
    slots:0-5460 (5461 slots) master
    M: 433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001
    slots:5461-10922 (5462 slots) master
    M: c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002
    slots:10923-16383 (5461 slots) master
    S: 054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003
    replicates 7a869b7e7b55094949c060bd5f2f99174c79be59
    S: fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004
    replicates 433d5052db72f7915bfe491f3afefcfd636da285
    S: 7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005
    replicates c7820416485d4af315146266bb42abd171099934
    Can I set the above configuration? (type 'yes' to accept): yes
    //......
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    [root@momo1 ~]# ps -ef|grep redis
    root 7694 1 0 06:30 ? 00:00:01 redis-server *:7000 [cluster]
    root 7698 1 0 06:31 ? 00:00:01 redis-server *:7001 [cluster]
    root 7702 1 0 06:31 ? 00:00:01 redis-server *:7002 [cluster]
    root 7712 1 0 06:31 ? 00:00:01 redis-server *:7003 [cluster]
    root 7716 1 0 06:31 ? 00:00:01 redis-server *:7004 [cluster]
    root 7720 1 0 06:31 ? 00:00:01 redis-server *:7005 [cluster]
    root 8377 8355 0 06:52 pts/1 00:00:00 grep redis

    ########7. redis-cli命令进入集群环境################
    redis-cli -c -p 7000 ##可任意进入其他节点
    redis-cli -c -p 7000 cluster nodes [| grep master] ##查看
    [root@momo1 src]# redis-cli -c -p 7000 cluster nodes
    fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004 slave 433d5052db72f7915bfe491f3afefcfd636da285 0 1461883595276 5 connected
    c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002 master - 0 1461883592754 3 connected 10923-16383
    7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005 slave c7820416485d4af315146266bb42abd171099934 0 1461883596285 6 connected
    054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003 slave 7a869b7e7b55094949c060bd5f2f99174c79be59 0 1461883593258 4 connected
    433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001 master - 0 1461883594266 2 connected 5461-10922
    7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000 myself,master - 0 0 1 connected 0-5460
    ====>可以看见id ip:端口 主/从 主id 哈希槽区域

    127.0.0.1:7000> set ab momo
    -> Redirected to slot [13567] located at 192.168.33.88:7002
    OK
    192.168.33.88:7002> get ab ##key通过哈希槽分配到7002中
    "momo"

redis集群操作

添加节点

根据添加节点类型的不同,有两种方法来添加新节点

  • 主节点:如果添加的是主节点,那么我们需要创建一个空节点,然后将某些哈希槽移动到这个空节点里面
  • 从节点:如果添加的是从节点,我们也需要创建一个空节点,然后把这个新节点设置成集群中某个主节点的复制品。
    命令

  • 添加节点到集群:redis-trib.rb add-node 192.168.33.88:7006 192.168.33.88:7000

  • (添加主节点)重新分片:重新分片操作基本上就是将某些节点上的哈希槽移动到另外一些节点上面.

  1. 使用命令./redis-trib.rb reshard 192.168.1.160:7000
  2. 执行 redis-trib 的第一步就是设定你打算移动的哈希槽的数量:
  3. 第二部是输入接收solts的节点ID
  4. redis-trib 会向你询问重新分片的源节点(source node),也就是需要从哪个节点中转移哈希槽(可以使用all)最后输入 yes 并使用按下回车之后, redis-trib 就会正式开始执行重新分片操作
  • (添加从节点)把集群中的某个节点设置为从节点
  1. redis-cli -c -p 7007 #进入7007为要修改为从节点的端口号
  2. cluster replicate 0b00721a509444db793d28448d8f02168b94bd38 #后面为主节点id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

########一. 添加一个主节点########################
########1. 首先把需要添加的节点启动################
[root@momo1 cluster]# mkdir 7006
[root@momo1 cluster]# cp redis.conf 7006/
[root@momo1 cluster]# sed -i "50s/7000/7006/" 7006/redis.conf
[root@momo1 cluster]# cd 7006/
[root@momo1 7006]# redis-server ./redis.conf

########2. 将这个新节点添加到集群中################
[root@momo1 7007]# cd /usr/local/redis/src/
[root@momo1 src]# redis-
redis-benchmark redis-check-dump redis-sentinel redis-trib.rb
redis-check-aof redis-cli redis-server
[root@momo1 src]# redis-trib.rb add-node 192.168.33.88:7006 192.168.33.88:7000
#最后这个ip:port六选一都行
>>> Adding node 192.168.33.88:7006 to cluster 192.168.33.88:7000
>>> Performing Cluster Check (using node 192.168.33.88:7000)
M: 7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004
slots: (0 slots) slave
replicates 433d5052db72f7915bfe491f3afefcfd636da285
M: c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005
slots: (0 slots) slave
replicates c7820416485d4af315146266bb42abd171099934
S: 054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003
slots: (0 slots) slave
replicates 7a869b7e7b55094949c060bd5f2f99174c79be59
M: 433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.33.88:7006 to make it join the cluster.
[OK] New node added correctly.

[root@momo1 src]# redis-cli -c -p 7000 cluster nodes #查看7006为master
fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004 slave 433d5052db72f7915bfe491f3afefcfd636da285 0 1461884624785 5 connected
c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002 master - 0 1461884620748 3 connected 10923-16383
7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005 slave c7820416485d4af315146266bb42abd171099934 0 1461884625810 6 connected
054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003 slave 7a869b7e7b55094949c060bd5f2f99174c79be59 0 1461884623775 4 connected
ea2b724ca7603e0a2036cf97781553dec1b0fad2 192.168.33.88:7006 master - 0 1461884621758 0 connected
433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001 master - 0 1461884622768 2 connected 5461-10922
7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000 myself,master - 0 0 1 connected 0-5460


########3. 这个新的节点可以成为主节点或者是从节点################
把这个节点变成主节点,使用redis-trib程序,将集群中的某些哈希槽移动到新节点里面, 这个新节点就成为真正的主节点了。
执行下面的命令对集群中的哈希槽进行移动:
[root@momo1 src]# cd /usr/local/redis/src/
[root@momo1 src]# ./redis-trib.rb reshard 192.168.33.88:7006
>>> Performing Cluster Check (using node 192.168.33.88:7006)
M: ea2b724ca7603e0a2036cf97781553dec1b0fad2 192.168.33.88:7006
slots: (0 slots) master
0 additional replica(s)
S: fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004
slots: (0 slots) slave
replicates 433d5052db72f7915bfe491f3afefcfd636da285
M: 7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005
slots: (0 slots) slave
replicates c7820416485d4af315146266bb42abd171099934
M: c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003
slots: (0 slots) slave
replicates 7a869b7e7b55094949c060bd5f2f99174c79be59
M: 433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 2000 #提示移动多少哈希槽,这移动2000
What is the receiving node ID?ea2b724ca7603e0a203..........#移到那个节点,这里为新增的
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:all #输入all 表示从所有的主节点中随机转移,凑够2000个哈希槽

....然后输入:yes

//######这样我们就分配好了,然后查看一下
[root@momo1 src]# redis-cli -c -p 7000 cluster nodes
fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004 slave 433d5052db72f7915bfe491f3afefcfd636da285 0 1461885154660 5 connected
c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002 master - 0 1461885150120 3 connected 11589-16383
7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005 slave c7820416485d4af315146266bb42abd171099934 0 1461885154157 6 connected
054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003 slave 7a869b7e7b55094949c060bd5f2f99174c79be59 0 1461885155165 4 connected
ea2b724ca7603e0a2036cf97781553dec1b0fad2 192.168.33.88:7006 master - 0 1461885156172 7 connected 0-665 5461-6127 10923-11588
433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001 master - 0 1461885152140 2 connected 6128-10922
7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000 myself,master - 0 0 1 connected 666-5460


########二. 添加一个从节点########################
########1. 首先把需要添加的节点启动################
[root@momo1 cluster]# mkdir 7007
[root@momo1 cluster]# cp redis.conf 7007/
[root@momo1 cluster]# sed -i "50s/7000/7007/" 7007/redis.conf
[root@momo1 cluster]# cd 7007/
[root@momo1 7007]# redis-server ./redis.conf

########2. 将这个新节点添加到集群中################
[root@momo1 7007]# cd /usr/local/redis/src/
[root@momo1 src]# redis-trib.rb add-node 192.168.33.88:7007 192.168.33.88:7000 //添加
[root@momo1 src]# redis-cli -c -p 7000 cluster nodes
fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004 slave 433d5052db72f7915bfe491f3afefcfd636da285 0 1461886668520 5 connected
c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002 master - 0 1461886665997 3 connected 11589-16383
7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005 slave c7820416485d4af315146266bb42abd171099934 0 1461886666503 6 connected
08908d24194bcbeeb7a6bca3024eb20d2f43b6cb 192.168.33.88:7007 master - 0 1461886667511 0 connected
054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003 slave 7a869b7e7b55094949c060bd5f2f99174c79be59 0 1461886665494 4 connected
ea2b724ca7603e0a2036cf97781553dec1b0fad2 192.168.33.88:7006 master - 0 1461886667007 7 connected 0-665 5461-6127 10923-11588
433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001 master - 0 1461886664487 2 connected 6128-10922
7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000 myself,master - 0 0 1 connected 666-5460

[root@momo1 ~]# redis-cli -c -p 7007
127.0.0.1:7007> cluster replicate ea2b724ca7603e0a2036cf97781553dec1b0fad2
OK

删除节点

  • 如果是主节点,先把主节点中的哈希槽转移到其他节点中,然后删除这个节点。
    redis-trib.rb reshard 192.168.33.88:7000
    redis-trib.rb del-node 192.168.33.88:7000 08908d24194bcbeeb7a6bca3024eb20d2f43b6cb

  • 如果是从节点,直接删除即可。
    redis-trib.rb del-node 192.168.33.88:7000 7002e2b66b9d3b384981bcbe7d09954638ed82fb

  • 示例代码:删除主从节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    ##############这里演示删除主节点7006############################
    [root@momo1 src]#redis-trib.rb reshard 192.168.33.88:7000 ##进入分配哈希槽
    [OK] All 16384 slots covered.
    How many slots do you want to move (from 1 to 16384)? 4000 ##这里之前7006的4000哈希
    What is the receiving node ID? 433d5052db72f7915bfe491f3afefcfd636da285 ##这里一到7001指定它的id
    Please enter all the source node IDs.
    Type 'all' to use all the nodes as source nodes for the hash slots.
    Type 'done' once you entered all the source nodes IDs.
    Source node #1:ea2b724ca7603e0a2036cf97781553dec1b0fad2 ##我们要删除7006这里一定是7006
    Source node #2:done

    //......yes
    #####删除
    ./redis-trib.rb del-node 192.168.33.88:7006 ea2b724ca7603e0a2036cf97781553dec1b0fad2 #7006ID

    注意:分配哈希槽顺序,先是指定分给某节点,然后是被分配的节点id(博主顺序就错错过,所以上面为4000)

    ###############删除从节点,7007##################
    ./redis-trib.rb del-node 192.168.33.88:7007 ea2b724ca7603e0a2036cf97781553dec1b0fad2 #7007ID

    [root@momo1 src]# redis-cli -c -p 7000 cluster nodes ####查看
    fcd2e21a9bd0785a27f6c356d670f378df679547 192.168.33.88:7004 slave 433d5052db72f7915bfe491f3afefcfd636da285 0 1461888018505 8 connected
    c7820416485d4af315146266bb42abd171099934 192.168.33.88:7002 master - 0 1461888017497 3 connected 12589-16383
    7e1d4056265d38b9ce419bcd6017534356bfcec0 192.168.33.88:7005 slave c7820416485d4af315146266bb42abd171099934 0 1461888017497 6 connected
    054f970416643f34a5c9472348097ff186629230 192.168.33.88:7003 slave 7a869b7e7b55094949c060bd5f2f99174c79be59 0 1461888019513 4 connected
    433d5052db72f7915bfe491f3afefcfd636da285 192.168.33.88:7001 master - 0 1461888016486 8 connected 0-665 5461-12588
    7a869b7e7b55094949c060bd5f2f99174c79be59 192.168.33.88:7000 myself,master - 0 0 1 connected 666-5460

java操作redis集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 /**
* 集群操作
* @throws Exception
*/

@Test
public void cluster() throws Exception {
JedisPoolConfig poolConfig = new JedisPoolConfig();
//TODO--
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
nodes.add(new HostAndPort("192.168.33.88", 7000));
nodes.add(new HostAndPort("192.168.33.88", 7001));
nodes.add(new HostAndPort("192.168.33.88", 7002));
nodes.add(new HostAndPort("192.168.33.88", 7003));
nodes.add(new HostAndPort("192.168.33.88", 7004));
nodes.add(new HostAndPort("192.168.33.88", 7005));

//1. 创建JedisCluster对象
JedisCluster jedisCluster = new JedisCluster(nodes , poolConfig );

//2. 直接可以操作reidsi了
jedisCluster.set("blog", "blog.xiaoxiaomo.com");
System.out.println( jedisCluster.get("blog") );

//3. 不需要关闭
//这个close表示把jedisCluster给关闭了
//jedisCluster.close();
}
  • 查看结果 :
1
2
3
4
[root@momo1 home]# redis-cli -c -p 7000
127.0.0.1:7000> get blog
-> Redirected to slot [7653] located at 192.168.33.88:7001
"blog.xiaoxiaomo.com"

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器