redis sentinel 和 clustering玩耍总结

介绍

redis sentinel是redis的一个高可靠性解决方案,其本质是主从复制。客户端只能从主服务器写入数据,可以从各个从服务器读取数据。当主服务器挂掉了,从服务器会选择一个成为主服务器(由sentinel进行选举),其他从服务器开始从新主服务器同步数据,完成整个迁移过程。
redis clustering则是redis分布式的解决方案,数据会尽量相对平均地保存在不同的redis实例。每个实例可以附带实例副本,这样如果有一个实例挂了,其副本会自动转换成实例接受请求。实例与副本的同步、故障转移由实例本身来处理。

redis sentinel配置与使用

配置

redis sentinel配置,需要3个或以上的节点构成。当然,其实两个节点也可以,只是不推荐。初始化配置时,需要指定master和slave,在slave实例的配置文件中加上replicaof 127.0.0.1 6379。然后添加sentinel,配置文件最小只需要以下几行:

sentinel monitor mymaster 127.0.0.1 6379 2 #数字2为sentinel数量/2,向上取整。前面的redis实例地址随意,master和slave均可
sentinel down-after-milliseconds mymaster 60000 #类似检测周期,redis实例挂了多久才视为挂了,进行切换。目的是防止网络抖动引起的实例切换,这里60s显然过长
sentinel failover-timeout mymaster 180000#实例挂了后,多少时间间隔内不允许作为master
sentinel parallel-syncs mymaster 1#同一时间切换配置的slave数,目的是防止同一时间所有slave都切换master,导致短时间内服务不可用

全部启动后,sentinel会自动记录其他sentinel,以及所有redis示例,开始正常工作。
建议的redis实例、sentinel实例与服务器的部署情况,可以参考官方文档

使用

由于redis sentinel的slave节点是只读的,实际使用中需要先从sentinel获取到master的信息,再进行使用。sentinel建议安装在客户端所在服务器,或者在redis服务器使用keepalive实现高可靠性后,连接虚拟地址上的sentinel服务。下为php使用php-redis扩展连接的例子:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 26379);//连接sentinel服务
$result = $redis->rawCommand('SENTINEL', 'masters');//返回master地址,其中第三个是host,第五个是port。完整内容可以打印出来
$redis->connect($result[0][3],$result[0][5]);

python下需要先安装依赖:python3 -m pip install redis

from redis.sentinel import Sentinel
sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
sentinel.discover_master('mymaster')
# return ('127.0.0.1', 6379)

redis clustering配置与使用

配置

完整配置过程见官方文档。这里只简单介绍redis5下配置过程。

  1. 修改配置文件,配置CLUSTER部分,设置
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 2000 #超时多久当作实例不可用
cluster-replica-validity-factor 1

其他按需修改。

  1. 启动所有实例后,执行./redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1自动配置集群。--cluster-replicas 1表示所有实例都会有一个复制集。当各个实例分布在不用服务器时,它能自动优化到最佳部署情况。
  2. 部署完成,测试下即可使用。

使用

php下,php-redis扩展支持cluster模式,实例代码如下:

<?php
$redis = new RedisCluster(NULL, ['127.0.0.1:6379', '127.0.0.1:6380', '127.0.0.1:6381']);
// In the event we can't reach a master, and it has slaves, failover for read commands
$redis->setOption(RedisCluster::OPT_SLAVE_FAILOVER, RedisCluster::FAILOVER_ERROR);
$redis->set('a',time());
$redis->set('z','test');

python需要先安装redis-py-cluster:python3 -m pip install redis-py-cluster

from rediscluster import StrictRedisCluster
startup_nodes = [{"host":"127.0.0.1", "port":"6379"}]
rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
#StrictRedisCluster继承自redis
rc.set("foo", "bar")
print(rc.get('foo'))

转载于:https://www.jianshu.com/p/5028ec38a0b2

猜你喜欢

转载自blog.csdn.net/weixin_34294649/article/details/91316970