Redis高可用集群搭建

1、Redis集群方案比较

1.1 哨兵模式

image.png

在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具来监控master节点的状态,
如果master节点异常,则会做主从切换,将某一台slave作为master,哨兵的配置略微复
杂,并且性能和高可用性等各方面表现一般,特别是在主从切换的瞬间存在访问瞬断的情
况,而且哨兵模式只有一个主节点对外提供服务,没法支持很高的并发,且单个主节点内存
也不宜设置得过大,否则会导致持久化文件过大,影响数据恢复或主从同步的效率

1.2 高可用集群模式

image.png

redis集群是一个由多个主从节点群组成的分布式服务器群,它具有复制、高可用和分片特
性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能。需要将每个节点
设置成集群模式,这种集群模式没有中心节点,可水平扩展,据官方文档称可以线性扩展到
上万个节点(官方推荐不超过1000个节点)。redis集群的性能和高可用性均优于之前版本的
哨兵模式,且集群配置非常简单

2、Redis高可用集群搭建(三主三从)

redis官网:https://redis.io/download

2.1 redis安装

1.安装gcc redis是c语言编写的
yum install gcc-c++
2.wget http://download.redis.io/releases/redis-5.0.8.tar.gz
3.tar xzf redis-5.0.8.tar.gz
4.cd redis-5.0.8
5.make
6.src/redis-server
7.$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

在redis 安装目录下新建一个redis-cluster 目录


image.png

至此一台虚拟机上的r单点redis 配置做完了,然后在虚拟机上clone 出一份CentOs


image.png

配置一下第二份centos 的ip信息。
如果不配置会出现[RTNETLINK answers: File exists 这个错误
参考: https://blog.csdn.net/the_victory/article/details/79604586

[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33

配置的ip 和原始CentOs 的ip 在同一网段上就行。
我第一台配置的ip 是:92.168.124.10
第二台配置的ip 是:92.168.124.200
第三台配置的ip 是:92.168.124.201
进入到redis 的安装目录


image.png

分别创建2个文件夾如下


image.png

1、把之前的redis.conf配置文件copy到新建的不同目录下,修改如下内容:


image.png
[root@localhost redisinstall]# cd 8001
[root@localhost 8001]# ll
总用量 64
-rw-r--r-- 1 root root 61796 3月  18 10:46 redis.conf
[root@localhost 8001]# vim redis.conf 

(1)daemonize yes 让redis 改为后台启动
(2)port 8001(分别对每个机器的端口号进行设置)
(3)cluster-enabled yes(启动集群模式)
(4)cluster-config-file nodes-8001.conf(集群节点信息文件,这里800x最好和port对
应上)
(5)cluster-node-timeout 5000 (集群节点之间连接的超时时间)
(6) # bind 127.0.0.1(去掉bind绑定访问ip信息)
(7) protected-mode no (关闭保护模式)
(8)appendonly yes (日志持久化方式 aof)
如果要设置密码需要增加如下配置:
(9)requirepass 123456 (设置redis访问密码)
(10)masterauth 123456 (设置集群节点间访问密码,跟上面一致)

接下来吧配置文件分别拷贝到8002,8003,8004,8005,8007 修改端口和 cluster-config-file nodes-xxxx.conf 分别为8002,8003,8004,8005,8007既可。

启动四个实例

 redis-5.0.8/src/redis-server 8001/redis.conf 
 redis-5.0.8/src/redis-server 8002/redis.conf 
 redis-5.0.8/src/redis-server 8003/redis.conf 
 redis-5.0.8/src/redis-server 8004/redis.conf 
 redis-5.0.8/src/redis-server 8005/redis.conf 
 redis-5.0.8/src/redis-server 8007/redis.conf 

第一台机器启动结果

[root@localhost redisinstall]# ps -ef| grep redis
root      17792      1  0 12:15 ?        00:00:00 redis-5.0.8/src/redis-server *:8001 [cluster]
root      17876      1  0 12:15 ?        00:00:00 redis-5.0.8/src/redis-server *:8003 [cluster]
root      17890   3615  0 12:15 pts/1    00:00:00 grep --color=auto redis
[root@localhost redisinstall]# 

第二台机器启动结果

[root@localhost redisinstall]# ps -ef |grep redis
root      20041      1  0 12:17 ?        00:00:00 redis-5.0.8/src/redis-server *:8002 [cluster]
root      20081      1  0 12:17 ?        00:00:00 redis-5.0.8/src/redis-server *:8004 [cluster]
root      20088   5654  0 12:17 pts/1    00:00:00 grep --color=auto redis
[root@localhost redisinstall]# 

第三台机器启动效果

[root@localhost redisinstall]# ps -ef |grep redis
root       5744      1  0 13:16 ?        00:00:00 redis-5.0.8/src/redis-server *:8005 [cluster]
root       5762      1  0 13:16 ?        00:00:00 redis-5.0.8/src/redis-server *:8007 [cluster]
root       5788   5007  0 13:16 pts/1    00:00:00 grep --color=auto redis
[root@localhost redisinstall]# 

image.png

用redis-cli创建整个redis集群(redis5以前的版本集群是依靠ruby脚本redis-
trib.rb实现)
redis-5.0.8/src/redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.124.10:8001 192.168.124.200:8002 192.168.124.10:8003 192.168.124.200:8004 192.168.124.201:8005 192.168.124.201:8007

[root@localhost redisinstall]# redis-5.0.8/src/redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.124.10:8001 192.168.124.200:8002 192.168.124.10:8003 192.168.124.200:8004 192.168.124.201:8005 192.168.124.201:8007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.124.200:8004 to 192.168.124.10:8001
Adding replica 192.168.124.201:8007 to 192.168.124.200:8002
Adding replica 192.168.124.10:8003 to 192.168.124.201:8005
M: ce7489144547ed452ed4c02410beb2b824335d07 192.168.124.10:8001
   slots:[0-5460] (5461 slots) master
M: 233ac655aa5c5159a6f4742961b1ca7fa7251a71 192.168.124.200:8002
   slots:[5461-10922] (5462 slots) master
S: 8b1bfeda300981bb521cc96f38878d66ff89ed30 192.168.124.10:8003
   replicates 5cbb7bfc0d507c05b206938d43cc744dd9b75a94
S: fecd8676a890258ebf46ee47914a3b2658dc1f53 192.168.124.200:8004
   replicates ce7489144547ed452ed4c02410beb2b824335d07
M: 5cbb7bfc0d507c05b206938d43cc744dd9b75a94 192.168.124.201:8005
   slots:[10923-16383] (5461 slots) master
S: 72b5c8b207d5e6e50fbac2f54a91e80e245113b6 192.168.124.201:8007
   replicates 233ac655aa5c5159a6f4742961b1ca7fa7251a71
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.124.10:8001)
M: ce7489144547ed452ed4c02410beb2b824335d07 192.168.124.10:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 233ac655aa5c5159a6f4742961b1ca7fa7251a71 192.168.124.200:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: fecd8676a890258ebf46ee47914a3b2658dc1f53 192.168.124.200:8004
   slots: (0 slots) slave
   replicates ce7489144547ed452ed4c02410beb2b824335d07
S: 8b1bfeda300981bb521cc96f38878d66ff89ed30 192.168.124.10:8003
   slots: (0 slots) slave
   replicates 5cbb7bfc0d507c05b206938d43cc744dd9b75a94
S: 72b5c8b207d5e6e50fbac2f54a91e80e245113b6 192.168.124.201:8007
   slots: (0 slots) slave
   replicates 233ac655aa5c5159a6f4742961b1ca7fa7251a71
M: 5cbb7bfc0d507c05b206938d43cc744dd9b75a94 192.168.124.201:8005
   slots:[10923-16383] (5461 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.
[root@localhost redisinstall]# 

image.png

出现上面的信息就说明redis 集群已经搭建成功了。

验证

(1)连接任意一个客户端即可:./redis-cli -c -h -p (-a访问服务端密码,-c表示集群模
式,指定ip地址和端口号。

[root@localhost redisinstall]# redis-5.0.8/src/redis-cli -a 123456 -c -h 192.168.124.10 -p 8001
192.168.124.10:8001> 

进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)


image.png

个主从信息


image.png

关闭集群则需要逐个进行关闭,使用命令:
redis-cli -a 123456 -c -h 192.168.124.10 -p 8001 shutdown

Java操作redis集群


    redis.clients
    jedis
    2.9.0

mport java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;


public class RedisCluster 
{
    public static void main(String[] args) throws IOException
    {
        Set jedisClusterNode = new HashSet();
        jedisClusterNode.add(new HostAndPort("192.168.124.10", 8001));
        jedisClusterNode.add(new HostAndPort("192.168.124.200", 8002));
        jedisClusterNode.add(new HostAndPort("192.168.124.10", 8003));
        jedisClusterNode.add(new HostAndPort("192.168.124.200", 8004));
        jedisClusterNode.add(new HostAndPort("192.168.124.201", 8005));
        jedisClusterNode.add(new HostAndPort("192.168.124.201", 8007));
        
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(10);
        config.setTestOnBorrow(true);
        //connectionTimeout:指的是连接一个url的连接等待时间
        //soTimeout:指的是连接上一个url,获取response的返回等待时间
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNode, 6000, 5000, 10, "123456", config);
        System.out.println(jedisCluster.set("student", "zhagnsan"));
        System.out.println(jedisCluster.set("age", "20"));
        
        System.out.println(jedisCluster.get("student"));
        System.out.println(jedisCluster.get("age"));
        
        jedisCluster.close();
    }
}

你可能感兴趣的:(Redis高可用集群搭建)