python 連接 redis cluster 集群


一. redis集群模式有多種, cluster模式只是其中的一種實現方式, 其原理請自行谷歌或者百度, 這里只舉例如何使用Python操作 redis cluster 集群

 

二. python 連接 redis cluster 集群

  第三方庫:

    redis-py-cluster: 最近還在維護

    rediscluster: 似乎很久沒有更新了

pip install redis-py-cluster
or
pip install rediscluster
from rediscluster import StrictRedisCluster

# redis cluster 集群最少三主三從 startup_nodes
= [ {"host":"192.168.3.25", "port":6379}, # 主 {"host":"192.168.3.25", "port":7001}, # 6379的從數據庫 {"host":"192.168.3.25", "port":6380}, # 主 {"host":"192.168.3.25", "port":7002}, # 6380的從數據庫 {"host":"192.168.3.25", "port":6381}, # 主 {"host":"192.168.3.25", "port":7003} # 6381的從數據庫 ]

# 連接集群 conn
= StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
conn.set(
'name', 'lowman') conn.set('kind', '屌絲')
conn.set('money', '3塊8')
print("My name is: ", conn.get('name')) print "I have money: ", conn.get('money')

其他的各項操作方法與 python 的 redis 庫保持一致. startup_nodes 參數中即使存在 錯誤節點參數 也能連接成功: 理論上, 只要保證有一個節點參數正確就可以正常連接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM