redis 集群 哨兵

同一个主机:

1 先创建一个文件夹

/usr/local/myredis

在这里插入图片描述

2:把redis.conf 和 sentinel.conf 拷贝到当前文件夹下面

正常安装
redis.conf在这里
在这里插入图片描述
sentinel.conf在你上传tar.gz 解压的位置
在这里插入图片描述

在这里插入图片描述
创建对应的文件夹
在这里插入图片描述

在每个文件夹里面创建对应的配置文件

touch redis1/redis1.conf
touch redis2/redis2.conf
touch redis3/redis3.conf
touch redis4/redis4.conf
touch redis5/redis5.conf
touch redis6/redis6.conf

把内容复制到配置文件里面

include ./redis.conf
port 7001
pidfile "./redis_7001.pid"
dbfilename "dump7001.rdb"
dir "/usr/local/myredis"
logfile "./redis_err_7001.log"
cluster-enabled yes
cluster-config-file "nodes-7001.conf"
cluster-node-timeout 15000

全局替换端口 :s/替换前的内容/替换后的内容/g
在这里插入图片描述

在这里插入图片描述
启动每一个redis
在这里插入图片描述
启动完成后如下
在这里插入图片描述
这里启动后,每一个redis也是单机的,执行下面的命令组成集群

redis-cli --cluster create 20.15.1.115:7001 20.15.1.115:7002 20.15.1.115:7003 20.15.1.115:7004 20.15.1.115:7005 20.15.1.115:7006 --cluster-replicas 1

在这里插入图片描述

可能出现在的问题

1:使用旧的命令

./redis-trib.rb create --replicas 1 20.15.1.115:7001 20.15.1.115:7002 20.15.1.115:7003 20.15.1.115:7004 20.15.1.115:7005 20.15.1.115:7006

这个是以前老版本的,现在不能使用了

2:

执行上面的的命令可能没有反应 ----> 这里因为centos7的gcc 版本默认是4.8的
需要升级(网上找一下资料)

3:

在这里插入图片描述
这个是因为之前启动过,集群停了再启动, 清空,重置一下,每一个节点都要操作一遍
在这里插入图片描述
但是这里又有一个问题。。。。 如果真的全部宕机了,不可能全部清了数据在启动吧

现在修改sentinel的配置

# 修改sentinel26379conf配置文件
bind 0.0.0.0
port 26379
daemonize yes
pidfile "./redis-sentinel26379.pid"
logfile "./sentinel-26379.log"
dir "/usr/local/myredis"
# 哨兵   监控	 主节点名称 IP地址  端口  判断失效的哨兵个数
sentinel monitor mymaster 20.15.1.115 7001 2
# 配置主服务的密码(如果主节点设置密码这块必须要进行配置)
#sentinel auth-pass mymaster enjoyitlife
sentinel down-after-milliseconds mymaster 1000

sentinel failover-timeout mymaster 5000

在这里插入图片描述
修改对应的端口 26379 26380 26381
启动 sentinel
在这里插入图片描述

在这里插入图片描述

最后就是
在这里插入图片描述
连接
在这里插入图片描述
查看集群信息
在这里插入图片描述
连接 sentinel 输入 info 查看哨兵信息
在这里插入图片描述
java中使用

工具

public class JedisSentinelPoolUtils {

    private static JedisSentinelPool jedisSentinelPool=null;

    public static Jedis getJedisFromSentinel(){
        if(jedisSentinelPool==null){
            Set<String> sentinelSet=new HashSet<>();
            sentinelSet.add("20.15.1.115:26379");

            JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(10); //最大可用连接数
            jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
            jedisPoolConfig.setMinIdle(5); //最小闲置连接数
            jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
            jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
            jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong

            jedisSentinelPool=new JedisSentinelPool("mymaster",sentinelSet,jedisPoolConfig);
            return jedisSentinelPool.getResource();
        }else{
            return jedisSentinelPool.getResource();
        }
    }
}

测试

public class JedisSentinelPoolTest {

    public static void main(String[] args) {

        Jedis jedisFromSentinel = JedisSentinelPoolUtils.getJedisFromSentinel();
        String set = jedisFromSentinel.set("aaaa", "bbbb");
        System.out.println(set);
        System.out.println(jedisFromSentinel.get("aaaa"));




    }
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇

)">
下一篇>>