主从复制 - redis主从, java客户端使用jedis连接master,读请求会被路由到slave吗?
仅有的幸福
仅有的幸福 2017-05-16 13:20:21
[Redis讨论组]

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();

`

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号