python和集群交互

1.安装

pip install redis-py-cluster

示例:

 1 from rediscluster import *
 2 if __name__ == '__main__':
 3   try:
 4       # 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
 5       startup_nodes = [
 6           {'host': '192.168.26.128', 'port': '7000'},
 7           {'host': '192.168.26.130', 'port': '7003'},
 8           {'host': '192.168.26.128', 'port': '7001'},
 9       ]
10       # 构建StrictRedisCluster对象
11       src=StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
12       # 设置键为name、值为itheima的数据
13       result=src.set('name','itheima')
14       print(result)
15       # 获取键为name
16       name = src.get('name')
17       print(name)
18   except Exception as e:
19       print(e)

 

posted @ 2019-10-15 22:35  yangzixiongh  阅读(329)  评论(0编辑  收藏  举报