Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

redis主从, java客户端使用jedis连接master,读请求会被路由到slave吗?

redis主从,实现类似mysql的读写分离效果。在代码层面需要执行slave host吗?
目前是通过jedis客户端JedisSentinelPool连接哨兵集群,查看日志输出应该连接的是master的host
读请求会被自动路由到slave吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

主的配置好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();

`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...