Redis使用入门(二)【Windows下Python客户端redis-py使用】
2016-09-22 22:53
561 查看
主从集群复制,哨兵sentinel模式
RedisTest.py代码入下:
import redis from 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 Sentinel print("-"*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 6389 slaveof 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.confredis-server sentinel-slave.conf –sentinel
结果如图:
相关文章推荐
- Redis的Python客户端redis-py的初步使用
- Redis的Python客户端redis-py安装使用说明文档
- 输入$ python text.py出错的原因——在windows系统使用idle的情况下
- Redis的Python客户端redis-py
- 【神经网络与深度学习】【python开发】caffe-windows使能python接口使用draw_net.py绘制网络结构图过程
- Redis的Python客户端redis-py说明文档(转)
- Redis的Python客户端redis-py
- 使用Python开发windows GUI程序入门实例
- [Redis]Windows下Redis的安装、权限设置和在Python中使用
- Redis入门很简单之七【使用Jedis实现客户端Sharding】
- windows下使用redis,Redis入门使用,Redis基础命令
- Redis入门很简单之七【使用Jedis实现客户端Sharding】
- Gitlab 与 Git Windows 客户端一起使用的入门流程
- Windows下安装Redis及使用Python操作Redis
- Windows下python 安装 redis客户端库
- windows下使用redis,Redis入门使用,Redis基础命令
- C#中使用Redis学习一 windows安装redis服务器端和客户端
- 使用Python开发windows GUI程序入门实例
- 在windows上部署redis服务,以及使用jedis客户端。
- Redis入门很简单之七【使用Jedis实现客户端Sharding】