Redis主从库以及Redis的集群部署

Redis的主从架构:从数据作为一个备份容错机制;

 

Redis的主从库的配置:

  1.配置命令

    redis.conf文件(windows是redis.windows.conf文件)    

   slaveof<masterip><masterport>    //主库ip和端口

   masterauth<master-password>    //主库的密码

   slave-read-only yes         //从库只读

  2.SLAVEOF命令

    slaveof host port                              //将当前库设置为host/port对应的实例从库

    slaveof no one          //取消当前库的从库身份

 

配置成功后配置当masterdown掉之后自动切换到从数据库上,通过Sentinel(哨兵)实现;

  安装新的redis服务,并在redis.conf的目录下新建文件sentinel.conf文件内容如下;

  

  port 26386         //哨兵的端口        

  protected-model no    //是否开启保护模式(开启保护模式只接受本地连接)

  dir "d:\\Redis-sentinel"    //安装目录

  sentinel minitor redis01 127.0.0.1 6379 1   //监控的节点,服务名称 ip地址,端口和有几个哨兵判断该节点下线

  sentinel  auth-pass redis01 123456      //密码

  sentinel down-after-millisencods redis01 5000   //配置当主节点多久没有反应判定为下线

  sentinel failover-timeout redis01 15000      //故障迁移的超时时间

  启动该哨兵服务的redis,通过命令:redis-server.exe sentinel.conf --sentinel

  

    哨兵会自动根据主节点找到主节点的从节点信息;

 

 

java连接有哨兵的服务:通过哨兵池连接    

    

public class JedisSentinel {
    public static void main(String[] args) {
        Set<String> ips = new HashSet<>();
        ips.add("127.0.0.1:26386");
        ips.add("127.0.0.1:26387");
        ips.add("127.0.0.1:26388");

        /**
         * 主节点服务名称,哨兵的ip的set集合
         */
        JedisSentinelPool sentinelPool = new JedisSentinelPool("redis001", ips);
        Jedis jedis = sentinelPool.getResource();
        jedis.auth("123456");

        //有对象后即可操作
    }
}

 

posted @ 2019-03-18 22:29  zlAdmin  阅读(601)  评论(0)    收藏  举报