#!/usr/bin/env python
# -*- coding:utf-8 -*-python
#!/usr/bin/env python
import redis
from redis.sentinel import Sentinelredis
# 鏈接哨兵服務器(主機名也能夠用域名)
sentinel = Sentinel([('172.31.0.2', 5001),
('172.31.0.3', 5001),
('172.31.0.4', 5001),
('172.31.0.5', 5001)
],
socket_timeout=0.5)服務器
# 獲取主服務器地址
master = sentinel.discover_master('mymaster')
print(master)
# 輸出:('172.31.0.2', 5001)socket
# 獲取從服務器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)
# 輸出:[('172.31.3', 5001), ('172.31.0.4', 5001), ('172.31.0.5', 5001)]utf-8
# 獲取主服務器進行寫入
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15)
w_ret = master.set('foo', 'bar')
# 輸出:Trueget
# # 獲取從服務器進行讀取(默認是round-roubin)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15)
r_ret = slave.get('foo')
print(r_ret)
# # 輸出:bar域名