• springboot2.0.4对接redis3.2.12版本哨兵模式


    redis 哨兵模式的创建

    1. 下载redis3.2.12版本。https://codeload.github.com/antirez/redis/zip/3.2.12

    2.  解压后放到/usr/local/src/目录下面。

    3.  拷贝三份 cp -R redis-3.2.13 redis1,cp -R redis-3.2.13 redis2,cp -R redis-3.2.13 redis3

    4.  进入redis1目录

    5.  make & make install

    6. 配置redis配置文件。

    redis.conf配置如下:

    daemonize yes #在后台运行,

    masterauth "myredis"

    requirepass "myredis"

    其他的保持默认。

    7. cd src目录 启动redis  ./redis-server ../redis.conf

    8. ps -ef | grep redis 查看redis 进程。

    9. 配置从服务器配置文件

    port 6380
    daemonize yes
    slaveof 127.0.0.1 6379

    启动服务。

    10. 配置从服务配置文件

    port 6381
    daemonize yes
    slaveof 127.0.0.1 6379

    启动服务

    11. Redis sentinel配置

    # 配置文件信息
    // Sentinel节点的端口
    port 26379

    sentinel auth-pass mymaster myredis

    // 当前Sentinel节点监控 127.0.0.1:6379 这个主节点
    // 2代表判断主节点失败至少需要2个Sentinel节点节点同意
    // mymaster是主节点的别名
    sentinel monitor mymaster 127.0.0.1 6379 2

    //每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒30s且没有回复,则判定不可达
    sentinel down-after-milliseconds mymaster 30000

    //当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,
    //原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
    sentinel parallel-syncs mymaster 1

    //故障转移超时时间为180000毫秒
    sentinel failover-timeout mymaster 180000

    12. 启动哨兵

    ./redis-sentinel ../sentinel.conf --sentinel

    13. 同样的配置其他的从服务器的哨兵并启动

    14. springboot配置

    redis:
    sentinel:
    master: mymaster
    nodes: 10.118.239.78:26379,10.118.239.78:26380,10.118.239.78:26381
    password: myredis

    15.  redis配置文件设置

    @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")
    private List<String> nodes;

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisPoolConfig getRedisConfig() {
    JedisPoolConfig config = new JedisPoolConfig();
    return config;
    }

    @Bean
    public RedisSentinelConfiguration sentinelConfiguration() {
    RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
    //配置matser的名称
    redisSentinelConfiguration.master("mymaster");
    redisSentinelConfiguration.setPassword(RedisPassword.of(password));
    //配置redis的哨兵sentinel
    Set<RedisNode> redisNodeSet = new HashSet<>();
    nodes.forEach(x -> {
    redisNodeSet.add(new RedisNode(x.split(":")[0], Integer.parseInt(x.split(":")[1])));
    });
    redisSentinelConfiguration.setSentinels(redisNodeSet);
    return redisSentinelConfiguration;
    }

    16. 启动项目测试。

  • 相关阅读:
    Python3 input() 函数
    Python3 enumerate() 函数
    Python3 ascii() 函数
    Python3 sorted() 函数
    css sprite
    Java 理论与实践: 并发集合类
    关于 Java Collections API 您不知道的 5 件事,第 1 部分
    Treasure! Free Mobile Design Resources
    Indigo Studio
    ionic:Build mobile apps faster with the web technologies you know and love
  • 原文地址:https://www.cnblogs.com/baoyi/p/springboot_redis.html
Copyright © 2020-2023  润新知