在有哨兵监控的主从结构下,使用jedis连接有以下几个步骤
- 使用JedisSentinelPool对象连接sentinelsentinel会返回给客户端当前master的地址
这个地址必须是客户端可以识别的(使用虚拟linux系统实验的情况下不能配置localhost或者127.0.0.1)
sentinel.conf
... sentinel monitor mymaster 192.168.1.8 6379 2 ...
- 客户端会拿着这个地址去请求mater节点
...
String masterName = "mymaster";
Set sentinels = new HashSet<>();
sentinels.add("192.168.1.8:25001");
sentinels.add("192.168.1.8:25001");
sentinels.add("192.168.1.8:25001");
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();//默认的配置
JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels,poolConfig);
Jedis jedis = pool.getResource();//获取jedis对象
pool.close();
...
打印日志信息如下
JedisSentinelPool - Trying to find master from available Sentinels... JedisSentinelPool - Connecting to Sentinel 192.168.1.8:25001 JedisSentinelPool - Found Redis master at 192.168.1.8:6379 JedisSentinelPool - Redis master running at 192.168.1.8:6379, starting Sentinel listeners... JedisSentinelPool - Created JedisSentinelPool to master at 192.168.1.8:6379 ... JedisSentinelPool - Shutting down listener on 192.168.1.8:25001 JedisSentinelPool - Unsubscribing from Sentinel at 192.168.1.8:25001



