Redis使用入门(二)【Windows下Python客户端redis-py使用】

来源:互联网 发布:linux c mysql 参数化 编辑:程序博客网 时间:2024/06/01 08:20

主从集群复制,哨兵sentinel模式

RedisTest.py代码入下:

import redisfrom rediscluster import StrictRedisCluster"""普通连接redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令(比如,SET命令对应与StrictRedis.set方法)。Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py。"""#r = redis.StrictRedis(host='localhost', port=6379, db=0, password='123456', encoding='utf-8')# print(r.set('foo', "bar"))## print(r.get('foo'))from redis.sentinel import Sentinelprint("-"*100)"""连接池"""#pool = redis.ConnectionPool(host='localhost', port=6379, db=0,password='123456',encoding='utf-8')pool = redis.ConnectionPool(host='localhost', port=6379, db=0,encoding='utf-8')r = redis.Redis(connection_pool=pool)r.set('bing', 'baz')"""pipeline操作管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。"""pipe = r.pipeline()pipe.set('china', 'ungly')pipe.get('bing')print(pipe.execute())"管道的命令可以写在一起,如:"pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()print("------------------------------------------------------------------------------------")"""Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换,而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自懂切换。"""sentinel = Sentinel([('localhost', 26379)])sentinel.discover_master('master001')sentinel.discover_slaves('master001')print(sentinel.discover_master('master001'))print(sentinel.discover_slaves('master001'))"""cluster模式"""# startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]# rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)# rc.set("foo", "bar")# print(rc.get("foo"))

结果如图:

这里写图片描述


这里写图片描述

接下来讲解详细配置:

redis-master.conf内容:

此正常配置
参考Redis使用入门(一)

redis-slave.conf内容

port 6389slaveof 127.0.0.1 6379

其他配置和redis-master.conf保持一致

首先是sentinel.conf内容:

#当前Sentinel服务运行的端口port 26379# sentinel announce-ip <ip># sentinel announce-port <port>#Sentinel服务运行时使用的临时文件夹dir "D:\\Redis\\TMP"################################# master001 ##################################Sentinel去监视一个名为master001的主redis实例,这个主实例的IP地址为本机地址192.168.1.100,端口号为6379,#而将这个主实例判断为失效至少需要1个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行#在至少1个哨兵实例都认为master down后把master标记为odown(objective down客观down;相对应的存在sdown,subjective down,主观down)状态#<quorum>应该小于集群中slave的个数,只有当至少<quorum>个sentinel实例提交"master失效" 才会认为master为ODWON("客观"失效) .sentinel monitor master001 192.168.1.100 6379 1# sentinel auth-pass <master-name> <password>#指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,#那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:#只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行sentinel down-after-milliseconds master001 30000#sentinel config-epoch master001 0#sentinel leader-epoch master001 2#指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,#同步的时间越长,完成故障转移所需的时间就越长sentinel parallel-syncs master001 1 #sentinel current-epoch 2#如果在该时间(ms)内未能完成failover操作,则认为该failover失败sentinel failover-timeout master001 180000 #指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用# sentinel notification-script <master-name> <script-path># sentinel client-reconfig-script <master-name> <script-path># 可以配置多个master节点################################# master002 #################################

sentinel-slave.conf的内容:

port 26389其他配置和master的sentinel.conf保持一致

cmd运行命令:

进入D:\Redis的文件夹路径,共4个窗口

主库启动

redis-server redis-master.conf

启动另一个窗口执行:

redis-server sentinel.conf –sentinel

结果如图:

这里写图片描述


这里写图片描述

从库启动

redis-server redis-slave.conf
redis-server sentinel-slave.conf –sentinel

结果如图:

这里写图片描述


这里写图片描述

0 0
原创粉丝点击