15.9,python操做redis集羣

 

 

上代碼node

1、對redis的單實例進行鏈接操做

python3
>>>import redis
>>>r = redis.StrictRedis(host='localhost', port=6379, db=0,password='root')
>>>r.set('lufei', 'guojialei')
True
>>>r.get('lufei')
'bar'


--------------------

2、sentinel集羣鏈接並操做


[root@db01 ~]# redis-server /data/6380/redis.conf
[root@db01 ~]# redis-server /data/6381/redis.conf
[root@db01 ~]# redis-server /data/6382/redis.conf 
[root@db01 ~]# redis-sentinel /data/26380/sentinel.conf &



--------------------------------
## 導入redis sentinel包
>>> from redis.sentinel import Sentinel  
##指定sentinel的地址和端口號
>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)  
##測試,獲取如下主庫和從庫的信息
>>> sentinel.discover_master('mymaster')  
>>> sentinel.discover_slaves('mymaster')  
##配置讀寫分離
#寫節點
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)  
#讀節點
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)  
###讀寫分離測試   key     
>>> master.set('oldboy', '123')  
>>> slave.get('oldboy')  
'123'


----------------------
redis cluster的鏈接並操做(python2.7.2以上版本才支持redis cluster,咱們選擇的是3.5)
https://github.com/Grokzen/redis-py-cluster


3、python鏈接rediscluster集羣測試
使用

python3
>>> from rediscluster import StrictRedisCluster  
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]  
### Note: decode_responses must be set to True when used with python3  
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)  
>>> rc.set("foo", "bar")  
True  
>>>   
'bar'
----------------------
相關文章
相關標籤/搜索