一:搭建redis5.0集群 redis-cluster 三主三从
规划:linux121,linux122,linux123,各安装一主一从(注意集群创建时候需要redis中没有数据)
第一步:创建目录,拷贝redis,并将slave中配置文件的port该为6380
cd /opt/lagou/software/
make install PREFIX=/opt/lagou/servers/redis-cluster/master
cp -r /opt/lagou/servers/redis/bin/redis.conf /opt/lagou/servers/redis-cluster/slave/bin
cp -r /opt/lagou/servers/redis/bin/redis.conf /opt/lagou/servers/redis-cluster/master/bin
vim /opt/lagou/servers/redis-cluster/slave/redis.conf
##开启后台启启动
# 将`daemonize`由`no`改为`yes`
daemonize yes
# 默认绑定的是回环地址,默认不能被其他机器访问
# bind 127.0.0.1
# 是否开启保护模式,由yes该为no
protected-mode no
port 6380
第二步:将redis.conf中的集群打开cluster-enable yes
vim /opt/lagou/servers/redis-cluster/slave/redis.conf
cluster-enable yes
vim /opt/lagou/servers/redis-cluster/master/redis.conf
cluster-enable yes
第三步:将redis-cluster 拷贝到linux122和linux123上
使用之前写好的脚本 rsync-script
#!/bin/bash
#1 获取命令输⼊参数的个数,如果个数为0,直接退出命令
paramnum=$#
if((paramnum==0)); then
echo no params;
exit;
fi
#2 根据传⼊参数获取⽂件名称
p1=$1
file_name=`basename $p1`
echo fname=$file_name
#3 获取输⼊参数的绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 获取⽤户名称
user=`whoami`
#5 循环执⾏rsync
for((host=121; host<124; host++)); do
echo ------------------- linux$host --------------
rsync -rvl $pdir/$file_name $user@linux$host:$pdir
done
第四部:依次启动三台服务器上的master和slave
写好得脚本
./redis-start.sh
第五步:创建Redis集群(创建时Redis里不要有数据)
# cluster-replicas : 1 1从机 前三个为主
# 采用机器ip而不采用127.0.0.1 不然外界无法访问 也不能采用linux121这样的域名映射,这样会报错,redis对这个支持不友好
./redis-cli --cluster create 192.168.111.121:6379 192.168.111.121:6381
192.168.111.122:6379 192.168.111.122:6381 192.168.111.123:6379 192.168.111.123:6381
--cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.111.122:6381 to 192.168.111.121:6379
Adding replica 192.168.111.123:6381 to 192.168.111.122:6379
Adding replica 192.168.111.121:6381 to 192.168.111.123:6379
M: 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379
slots:[0-5460] (5461 slots) master
S: 169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381
replicates e21966f5d1c3f884728a564b6a59abca4ed155f1
M: 541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379
slots:[5461-10922] (5462 slots) master
S: fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381
replicates 29f0fa78ff5033e7e41fbe70f05fbb35b922e603
M: e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379
slots:[10923-16383] (5461 slots) master
S: c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381
replicates 541ae6e749a0717e179500141594dbb8c0764834
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 192.168.111.121:6379)
M: 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381
slots: (0 slots) slave
replicates e21966f5d1c3f884728a564b6a59abca4ed155f1
S: fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381
slots: (0 slots) slave
replicates 29f0fa78ff5033e7e41fbe70f05fbb35b922e603
S: c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381
slots: (0 slots) slave
replicates 541ae6e749a0717e179500141594dbb8c0764834
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
第六步:命令客户端连接集群 -c 表示是以redis集群方式进行连接
./redis-cli -h linux121 -p 6379 -c
linux121:6379> set name zh
-> Redirected to slot [5798] located at 192.168.111.122:6379
OK
192.168.111.122:6379>
像这样如果slot不在本机上 则会重定向到对应的机器上
二:添加一主一从(Master4和Slaver4)
1.添加节点
##1.进入redis5.0的src目录下,创建需要添加的redis节点
cd redis-5.0.5/src
make install PREFIX=/opt/lagou/servers/redis-cluster/redis-addMaster
## 拷贝redis.conf 并且配置
cp /opt/lagou/software/redis-5.0.5/redis.conf ./
2.修改配置文件
vim redis.conf
##开启后台启启动
# 将`daemonize`由`no`改为`yes`
daemonize yes
# 默认绑定的是回环地址,默认不能被其他机器访问
# bind 127.0.0.1
# 是否开启保护模式,由yes该为no
protected-mode no
#开启集群
cluster-enable yes
port 6382
3.将文件拷贝成为从节点,并将从节点的port改为6383
cp -r redis-addMaster/ redis-addSlave
vim redis.conf
port 6383
4.在对应的目录下启动需要添加的两个节点
./redis-server redis.conf
5.将6382的节点添加到集群中
./redis-cli --cluster add-node 192.168.111.121:6382 192.168.111.121:6379
>>> Adding node 192.168.111.121:6382 to cluster 192.168.111.121:6379
>>> Performing Cluster Check (using node 192.168.111.121:6379)
M: 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381
slots: (0 slots) slave
replicates e21966f5d1c3f884728a564b6a59abca4ed155f1
S: fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381
slots: (0 slots) slave
replicates 29f0fa78ff5033e7e41fbe70f05fbb35b922e603
S: c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381
slots: (0 slots) slave
replicates 541ae6e749a0717e179500141594dbb8c0764834
[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.111.121:6382 to make it join the cluster.
[OK] New node added correctly.
##查看集群状态
192.168.111.123:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_sent:926
cluster_stats_messages_received:926
6.hash槽重新分配(数据迁移)添加完主节点需要对主节点进行hash槽分配,这样该主节才可以存储数据
##查看集群中槽占用情况
192.168.111.123:6379> cluster nodes
169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381@16381 slave e21966f5d1c3f884728a564b6a59abca4ed155f1 0 1606832809000 5 connected
717487219f2522077b342a06296a96337a2cb756 192.168.111.121:6382@16382 master - 0 1606832809145 0 connected
c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381@16381 slave 541ae6e749a0717e179500141594dbb8c0764834 0 1606832811000 6 connected
541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379@16379 master - 0 1606832813170 3 connected 5461-10922
e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379@16379 myself,master - 0 1606832810000 5 connected 10923-16383
29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379@16379 master - 0 1606832811157 1 connected 0-5460
fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381@16381 slave 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 0 1606832812165 4 connected
下面开始操作
第一步:将6382节点连上集群(连接集群中任意一个可用结点都行)
./redis-cli --cluster reshard 192.168.111.121:6379
第二步:输入要分配的槽数量(输入:3000,表示要给目标节点分配3000个槽)
How many slots do you want to move (from 1 to 16384)? 3000
第三步:输入接收槽的结点id
What is the receiving node ID?
输入:717487219f2522077b342a06296a96337a2cb756
PS:这里准备给6382分配槽,通过cluster nodes查看6382结点id为上面写的(或者nodes.conf)
第四步:输入源结点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.
输入:all
第五步:输入yes开始移动槽到目标结点id
Moving slot 11616 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11617 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11618 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11619 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11620 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11621 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11622 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11623 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11624 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11625 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11626 from 192.168.111.123:6379 to 192.168.111.121:6382:
Moving slot 11627 from 192.168.111.123:6379 to 192.168.111.121:6382:
第六步:查看结果
192.168.111.123:6379> cluster nodes
169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381@16381 slave e21966f5d1c3f884728a564b6a59abca4ed155f1 0 1606833183643 5 connected
717487219f2522077b342a06296a96337a2cb756 192.168.111.121:6382@16382 master - 0 1606833181632 7 connected 0-998 5461-6461 10923-11921
c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381@16381 slave 541ae6e749a0717e179500141594dbb8c0764834 0 1606833182637 6 connected
541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379@16379 master - 0 1606833180625 3 connected 6462-10922
e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379@16379 myself,master - 0 1606833182000 5 connected 11922-16383
29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379@16379 master - 0 1606833179000 1 connected 999-5460
fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381@16381 slave 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 0 1606833179619 4 connected
7.添加从节点,添加6383从结点,将6383作为6382的从结点
./redis-cli --cluster add-node 新节点的ip和端口 旧节点ip和端口 --cluster-slave -- cluster-master-id 主节点id
##执行命令如下
[root@linux121 bin]# ./redis-cli --cluster add-node 192.168.111.121:6383
192.168.111.121:6382 --cluster-slave --cluster-master-id 717487219f2522077b342a06296a96337a2cb756
>>> Adding node 192.168.111.121:6383 to cluster 192.168.111.121:6382
>>> Performing Cluster Check (using node 192.168.111.121:6382)
M: 717487219f2522077b342a06296a96337a2cb756 192.168.111.121:6382
slots:[0-998],[5461-6461],[10923-11921] (2999 slots) master
S: c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381
slots: (0 slots) slave
replicates 541ae6e749a0717e179500141594dbb8c0764834
M: 541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379
slots:[6462-10922] (4461 slots) master
1 additional replica(s)
M: e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379
slots:[11922-16383] (4462 slots) master
1 additional replica(s)
S: fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381
slots: (0 slots) slave
replicates 29f0fa78ff5033e7e41fbe70f05fbb35b922e603
M: 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379
slots:[999-5460] (4462 slots) master
1 additional replica(s)
S: 169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381
slots: (0 slots) slave
replicates e21966f5d1c3f884728a564b6a59abca4ed155f1
[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.111.121:6383 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.111.121:6382.
[OK] New node added correctly.
8.查看节点信息,会多出6083的节点
192.168.111.123:6379> cluster nodes
169ec49719611a7bf92e8eaf47f99949bf394244 192.168.111.121:6381@16381 slave e21966f5d1c3f884728a564b6a59abca4ed155f1 0 1606833987040 5 connected
717487219f2522077b342a06296a96337a2cb756 192.168.111.121:6382@16382 master - 0 1606833986000 7 connected 0-998 5461-6461 10923-11921
c2553e0a62ba0b81ec14adbf7d97efb7929ec733 192.168.111.123:6381@16381 slave 541ae6e749a0717e179500141594dbb8c0764834 0 1606833984000 6 connected
541ae6e749a0717e179500141594dbb8c0764834 192.168.111.122:6379@16379 master - 0 1606833986537 3 connected 6462-10922
e21966f5d1c3f884728a564b6a59abca4ed155f1 192.168.111.123:6379@16379 myself,master - 0 1606833984000 5 connected 11922-16383
29f0fa78ff5033e7e41fbe70f05fbb35b922e603 192.168.111.121:6379@16379 master - 0 1606833985000 1 connected 999-5460
76f3a416c64016df5ea15093d79862141765775e 192.168.111.121:6383@16383 slave 717487219f2522077b342a06296a96337a2cb756 0 1606833986034 7 connected
fd34f3d0af392d1f155a01ebb3c3a588911d731b 192.168.111.122:6381@16381 slave 29f0fa78ff5033e7e41fbe70f05fbb35b922e603 0 1606833988048 4 connected
到此节点添加结束
三:通过JedisCluster向RedisCluster添加数据和取出数据(没有添加节点前操作的)
1.连接redis工具类
@Component
public class RedisClusterUtil {
@Value("${redis.nodes}")
private String clusterNodes;
public JedisCluster getJedisCluster() {
// Jedis连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(10);
// 最大连接数, 默认8个
jedisPoolConfig.setMaxTotal(10);
//最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(0);
// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
jedisPoolConfig.setMaxWaitMillis(2000);
String[] serverArray = clusterNodes.split(",");
Set<HostAndPort> jedisClusterNode = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
jedisClusterNode.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(jedisClusterNode, jedisPoolConfig);
}
}
2.测试类
/**
* @功能描述: 单元测试超类
* 单元测试不能使用org.junit.jupiter.api.Test
* 要使用 org.junit.Test;
* 类和方法都必须使用public
*/
@RunWith(SpringRunner.class)
//@SpringBootTest(classes={Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest
@EnableSwagger2
public class ApplicationTests {
@Test
public void contextLoads() {
System.out.println(1);
}
}
//测试类启动Spring
public class RedisTest extends ApplicationTests{
@Autowired
RedisClusterUtil redisClusterUtil;
@Test
public void getJedisCluster() {
JedisCluster jedisCluster = redisClusterUtil.getJedisCluster();
jedisCluster.set("mykey","key");
String name = jedisCluster.get("mykey");
System.out.println(name);
}
}
3.配置文件 application.yml
#redis配置
redis:
nodes: 192.168.111.121:6379,192.168.111.121:6381,192.168.111.122:6379,192.168.111.122:6381,192.168.111.123:6379,192.168.111.123:6381
timeout: 2000ms
4.pom依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
5. 运行结果
源码地址:https://github.com/GdHuni/springboot-study.git