Sentinel的客户端
如果要做到应用程序(客户端)对Redis的failover透明Transparent),客户端需要监控sentinel的频道信息,并自动连接新的主节点。官方提供了一个专门的topic来讲解这个问题:Guidelines for Redis clients with support for Redis Sentinel,而一些常用的开发语言也已经有了整合sentinel的redis driver:
Python:Redis
Java:Jedis
这里以Python为例,首先安装redis-py。
$ pip install redis
1
$pipinstallredis
简单连接
>>> import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'
1
2
3
4
5
6
>>>importredis
>>>r=redis.StrictRedis(host='localhost',port=6379,db=0)
>>>r.set('foo','bar')</