本文整理汇总了Python中redis.sentinel.Sentinel.discover_master方法的典型用法代码示例。如果您正苦于以下问题:Python Sentinel.discover_master方法的具体用法?Python Sentinel.discover_master怎么用?Python Sentinel.discover_master使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.sentinel.Sentinel
的用法示例。
在下文中一共展示了Sentinel.discover_master方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def connect(self, index=0, write=True):
"""
Creates a redis connection with connection pool.
"""
master_name, sentinel_hosts, db = self.parse_connection_string(self._connection_string)
sentinel_timeout = self._options.get('SENTINEL_TIMEOUT', 1)
sentinel = Sentinel(sentinel_hosts, socket_timeout=sentinel_timeout)
if write:
host, port = sentinel.discover_master(master_name)
else:
host, port = random.choice([sentinel.discover_master(master_name)] + sentinel.discover_slaves(master_name))
kwargs = {
"db": db,
"parser_class": self.parser_class,
"password": self._options.get('PASSWORD', None),
}
kwargs.update({'host': host, 'port': port, 'connection_class': Connection})
if 'SOCKET_TIMEOUT' in self._options:
kwargs.update({'socket_timeout': int(self._options['SOCKET_TIMEOUT'])})
kwargs.update(self._pool_cls_kwargs)
connection_pool = get_or_create_connection_pool(self._pool_cls, **kwargs)
connection = Redis(connection_pool=connection_pool)
return connection
示例2: connect
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def connect(self, index=0, write=True):
"""
Creates a redis connection with connection pool.
"""
master_name, sentinel_hosts, db = self.parse_connection_string(self._connection_string)
sentinel_timeout = self._options.get("SENTINEL_TIMEOUT", 1)
sentinel = Sentinel(sentinel_hosts, socket_timeout=sentinel_timeout)
if write:
host, port = sentinel.discover_master(master_name)
else:
host, port = random.choice([sentinel.discover_master(master_name)] + sentinel.discover_slaves(master_name))
return self.connection_factory.connect(host, port, db)
示例3: check_sso_auth
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def check_sso_auth(user_id, brand_id, trans_type ):
sentinel = Sentinel([('120.76.154.208', 26379)], socket_timeout=0.5)
log.d(sentinel.discover_master('mymaster'))
redis_obj = sentinel.master_for('mymaster', socket_timeout=0.1)
db_obj = PostgreSQLConnector()
sso_cls = SSOAuth(redis_obj,db_obj)
return sso_cls.check_auth(user_id, brand_id, trans_type)
示例4: sso_log
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def sso_log(user_id, brand_id, trans_type, data_type,data_content,log_time):
sentinel = Sentinel([('112.74.198.224', 26379)], socket_timeout=0.5)
log.d(sentinel.discover_master('mymaster'))
redis_obj = sentinel.master_for('mymaster', socket_timeout=0.1)
db_obj = PostgreSQLConnector()
sso_cls = SSOAuth(redis_obj,db_obj)
return sso_cls.send_log(user_id, brand_id, trans_type, data_type,data_content,log_time)
示例5: new_new
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def new_new(_, cls, broker_url, *args, **kwargs):
scheme = urlparse(broker_url).scheme
if scheme == "redis-sentinel":
from rpaas.tasks import app
opts = app.conf.BROKER_TRANSPORT_OPTIONS
s = Sentinel(opts["sentinels"], password=opts["password"])
host, port = s.discover_master(opts["service_name"])
return RedisBroker("redis://:{}@{}:{}".format(opts["password"], host, port))
else:
old_new(cls, broker_url, *args, **kwargs)
示例6: get_redis_url
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def get_redis_url():
if hasattr(config, 'ETCD_HOST'):
return config.HERA_REDIS_SENTINEL
sentinel_list = [tuple(i.split(':')) for i in get_etcd_setting('HERA_REDIS_SENTINEL').split(',')]
redis_db = get_etcd_setting('HERA_REDIS_DB')
sentinel = Sentinel(sentinel_list, socket_timeout=0.1)
master_name = get_etcd_setting('HERA_REDIS_MASTERNAME')
return 'redis://{}/{}'.format(
':'.join(map(str, sentinel.discover_master(master_name))),
redis_db
)
示例7: _redis
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def _redis(self):
if getattr(settings, 'EXPERIMENTS_REDIS_SENTINELS', None):
sentinel = Sentinel(settings.EXPERIMENTS_REDIS_SENTINELS, socket_timeout=settings.EXPERIMENTS_REDIS_SENTINELS_TIMEOUT)
host, port = sentinel.discover_master(settings.EXPERIMENTS_REDIS_MASTER_NAME)
else:
host = getattr(settings, 'EXPERIMENTS_REDIS_HOST', 'localhost')
port = getattr(settings, 'EXPERIMENTS_REDIS_PORT', 6379)
password = getattr(settings, 'EXPERIMENTS_REDIS_PASSWORD', None)
db = getattr(settings, 'EXPERIMENTS_REDIS_DB', 0)
return redis.Redis(host=host, port=port, password=password, db=db)
示例8: connect
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def connect(self, index=0, write=True):
"""
Creates a redis connection with connection pool.
"""
master_name, sentinel_hosts, db = self.parse_connection_string(self._connection_string)
sentinel_timeout = self._options.get('SENTINEL_TIMEOUT', 1)
sentinel = Sentinel(sentinel_hosts, socket_timeout=sentinel_timeout)
if write:
host, port = sentinel.discover_master(master_name)
else:
host, port = random.choice([sentinel.discover_master(master_name)] + sentinel.discover_slaves(master_name))
sentinel_options = self._options.copy()
sentinel_options['CONNECTION_POOL_CLASS'] = 'redis.sentinel.SentinelConnectionPool'
sentinel_options['CONNECTION_POOL_KWARGS'] = {
'service_name': master_name,
'sentinel_manager': sentinel}
self.connection_factory = get_connection_factory(options=sentinel_options)
return self.connection_factory.connect(host, port, db)
示例9: __init__
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def __init__(self, sentinels, service, **kwargs):
sentinel = Sentinel(sentinels, **kwargs)
try:
master = sentinel.discover_master(service)
except MasterNotFoundError:
raise
try:
slaves = sentinel.discover_slaves(service)
except SlaveNotFoundError:
self._slaves = None
if version_info[0] < 3:
super(AutoRedisSentinel, self).__init__(master, slaves=slaves, **kwargs)
else:
super().__init__(master, slaves=slaves, **kwargs)
示例10: run
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def run(self, terms, inject=None, **kwargs):
values = []
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
for term in terms:
params = self.parse_params(term)
try:
conn = Sentinel(params['server'], socket_timeout=1)
res = conn.discover_master(params['name'])
if res is None:
res = ""
values.append(":".join((str(v) for v in res)))
except Exception, e:
print(e)
示例11: str_to_bool
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
else:
return str_to_bool(os_var)
REDIS_MASTER_IP = None
REDIS_MASTER_PORT = None
REDIS_SENTINEL_ENABLED = bool_from_env('REDIS_SENTINEL_ENABLED', False)
if REDIS_SENTINEL_ENABLED:
from redis.sentinel import Sentinel
# REDIS_SENTINEL_HOSTS env variable format: host_1:port;host_2:port
REDIS_SENTINEL_HOSTS = os.environ['REDIS_SENTINEL_HOSTS'].split(';')
REDIS_CLUSTER_NAME = os.environ['REDIS_CLUSTER_NAME']
sentinel = Sentinel(
[tuple(s_host.split(':')) for s_host in REDIS_SENTINEL_HOSTS],
socket_timeout=float(
os.environ.get('REDIS_SENTINEL_SOCKET_TIMEOUT', 0.2)
)
)
REDIS_MASTER_IP, REDIS_MASTER_PORT = sentinel.discover_master(
REDIS_CLUSTER_NAME
)
REDIS_HOST = REDIS_MASTER_IP or os.environ.get('REDIS_HOST', '127.0.0.1')
REDIS_PORT = REDIS_MASTER_PORT or int(os.environ.get('REDIS_PORT', 6379))
REDIS_DB = int(os.environ.get('REDIS_DB', 0))
REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD', None)
REDIS_SSL = bool_from_env('REDIS_SSL', False)
示例12: agent
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
class agent(object):
def __init__(self):
pass
#by default,load the agent cnfig file
def load_config(self,config_file='./conf/agent.yml'):
self.config_file=config_file
with open(self.config_file,'r') as fd:
self.config = yaml.load(fd)
return self.config
def logger(self,path):
self.logger=logging.getLogger()
self.logger.setLevel(logging.WARNING)
fh=logging.FileHandler(path)
formatter = logging.Formatter("%(asctime)-15s %(filename)s [%(levelname)-8s] %(message)s")
fh.setFormatter(formatter)
self.logger.addHandler(fh)
return self.logger
#load the twemproxy config file by agent_conf_sn we specified
def load_twem_config(self,conf_sn):
self.config=self.load_config()
self.twem_config_file=self.config['agents'][conf_sn]['twem_config']
self.twem_config=self.load_config(self.twem_config_file)
return self.twem_config
#restart the twemproxy
def twem_restart(self,conf_sn): # conf_sn(config file session name) is 'cli1','cli2' etc. we can regard him as primary key in sql.
self.load_config()
cmd=self.config['agents'][conf_sn]['twem_cmd']
rs,rt = commands.getstatusoutput(cmd) # 'rs' means result, 'rt' mean return
self.logger.warning("%s"%rt)
return rt
def update_twem_master(self,conf_sn,sv_alias,new_ip,new_port):
self.load_twem_config(conf_sn)
self.twem_servers=self.twem_config['twem1']['servers']
self.logger.warning("NO,%s:%s twem_master != sentinel_master,Begin update twemproxy "%(conf_sn,sv_alias))
for i in range(len(self.twem_servers)):
if ( self.twem_servers[i].split(" ")[1] == sv_alias ):
self.logger.warning("twem old master:%s"%self.twem_servers[i].split(" ")[0].split(":"))
#print "[",datetime.now(),"]","old_master :",self.twem_servers[i].split(" ")[0].split(":")
weight=self.twem_servers[i].split(" ")[0].split(":")[2]
new_addr=new_ip+':'+str(new_port)+':'+weight+' '+sv_alias
self.logger.warning("twem new master:%s"%new_addr)
#print "[",datetime.now(),"]","new_master",new_addr
self.twem_servers[i]=new_addr
with open(self.twem_config_file,'w') as stream:
yaml.dump(self.twem_config,stream,allow_unicode=True,default_flow_style=False)
self.twem_restart(conf_sn)
#return the twemproxy's master addr (IP:PORT)
def get_twem_master(self,conf_sn,sv_alias):
self.load_twem_config(conf_sn)
self.twem_servers=self.twem_config['twem1']['servers']
for i in range(len(self.twem_servers)):
if( self.twem_servers[i].split(" ")[1] == sv_alias ):
self.twem_master=self.twem_servers[i].split(" ")[0].split(':')[0]+':'+self.twem_servers[i].split(" ")[0].split(':')[1]
#print 'twemproxy_master:',self.twem_master
self.logger.info("get twemproxy %s:%s master:%s"%(conf_sn,sv_alias,self.twem_master))
return self.twem_master
#retun the sentinel_master addr
def get_sentinel_master(self,conf_sn,sv_alias):
stats=1
self.load_config()
self.sentinel_host=self.config['agents'][conf_sn]['sentinel_host']
self.sentinel_port=self.config['agents'][conf_sn]['sentinel_port']
try:
self.sentinel=Sentinel([(self.sentinel_host,self.sentinel_port)],socket_timeout=1)
except :
self.logger.error('connection to Sentinel %s:%s failed '%(self.sentinel_host,self.sentinel_port))
while stats:
try:
self.sentinel_master=self.sentinel.discover_master(sv_alias)
stats=0
except:
time.sleep(0.5)
self.logger.error('get %s sentinel %s:%s failed %d times'%(conf_sn,self.sentinel_host,self.sentinel_port,stats))
stats = stats + 1
while (stats == 5):
stats=0
self.logger.error('retry failed,skip this sentinel %s:%s,please check it'%(self.sentinel_host,self.sentinel_port))
self.sentinel_master=self.sentinel_master[0]+':'+str(self.sentinel_master[1])
if (len(self.sentinel_master)<7):
return False
self.logger.error("get sentinel %s,%s master Failed"%(conf_sn,sv_alias))
else:
self.logger.info("get sentinel %s,%s master:%s"%(conf_sn,sv_alias,self.sentinel_master))
return self.sentinel_master
def __del__(self):
pass
示例13: Error
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
#.........这里部分代码省略.........
try:
pool = self._pool
except AttributeError:
pass
else:
pool.disconnect()
#self.logger.info("{} Disconnected Redis client from Redis server {host}:{port}.".format(tools.cm_name(), **connection.connection_pool.connection_kwargs))
def _raise_ConnectionError(self, exc):
"""
Raise ConnectionError exception with an error message corresponding
to the provided exception.
"""
# TODO: Implement functionality of _raise_ConnectionError directly into ConnectionError exception instead. Delete method definition.
err_msg = str(exc)
if err_msg: err_msg = ": {}".format(err_msg)
err_msg = "Redis connection error{}".format(err_msg)
# Conditionally log error
# log_interval = backend.settings.redis["Console"]["ConnectionError log interval"]
# if time.time() > (self._ConnectionError_last_log_time + log_interval):
# self._ConnectionError_last_log_time = time.time()
# logger = self.logger.info
# else:
if self.logger:
logger = self.logger.debug
logger("{}".format(err_msg))
raise self.ConnectionError(err_msg)
def discover_master(self):
"""
Return the master instance (host, ip)
"""
if self.sentinel and self.master:
attempts = 0
while attempts < ATTEMPT_LIMIT:
try:
attempts += 1
master = self.sentinel.discover_master(self.master)
break
except redis.sentinel.MasterNotFoundError as e:
# Pause for specified time
print "try %d" % attempts
time.sleep(ATTEMPT_PAUSE)
else:
self._raise_ConnectionError(e)
return master
else:
self._raise_ConnectionError("Sentinels not properly defined")
def discover_slaves(self):
"""
Return the slave instances [(host, ip),]
"""
if self.sentinel and self.master:
attempts = 0
while attempts < ATTEMPT_LIMIT:
try:
attempts += 1
示例14: Sentinel
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
#!/usr/bin/python
from redis.sentinel import Sentinel
sentinel = Sentinel([('localhost', 19001)], socket_timeout=0.1)
master = sentinel.discover_master('redis-cluster')
slaves = sentinel.discover_slaves('redis-cluster')
print ""
print "Master(" + str(master) + ")"
print ""
print "Slaves(" + str(len(slaves)) + ") Nodes(" + str(slaves) + ")"
print ""
示例15: getSentinelConnection
# 需要导入模块: from redis.sentinel import Sentinel [as 别名]
# 或者: from redis.sentinel.Sentinel import discover_master [as 别名]
def getSentinelConnection(sentinelHosts):
sentinel = Sentinel(sentinelHosts, socket_timeout=0.1)
master = sentinel.discover_master('mymaster')
r = redis.StrictRedis(master[0], port=int(master[1]), db=int(config["redisDb"]))
return r