主從復制 - redis主從, java客戶端使用jedis連接master,讀請求會被路由到slave嗎?
問題描述
redis主從,實現類似mysql的讀寫分離效果。在代碼層面需要執行slave host嗎?目前是通過jedis客戶端JedisSentinelPool連接哨兵集群,查看日志輸出應該連接的是master的host讀請求會被自動路由到slave嗎?
問題解答
回答1:主的配置好ip和端口,從的配置好Slaveof的master地址和端口號,哨兵監控master的ip和端口號,java代碼直接master的name和密碼就行了。`
public static void main(String[] args) { Set<String> sentinels = new HashSet<String>(); String hostAndPort1 = '127.0.0.1:26379'; String hostAndPort2 = '127.0.0.1:26380'; sentinels.add(hostAndPort1); sentinels.add(hostAndPort2); String clusterName = 'mymaster'; String password = '123456'; JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels,password); Jedis jedis = null; try { jedis = redisSentinelJedisPool.getResource(); System.out.println(jedis.get('key')); } catch (Exception e) { e.printStackTrace(); } finally { redisSentinelJedisPool.returnBrokenResource(jedis); } redisSentinelJedisPool.close();
`
