• Redis哨兵模式高可用部署和配置


    一、Redis 安装配置

    1.下载redis安装包 wget http://download.redis.io/releases/redis-4.0.9.tar.gz

    2.解压安装包 tar -zxvf redis-4.0.9.tar.gz -C /usr/local/

    3.安装gcc依赖

    先通过gcc -v是否有安装gcc,如果没有安装,执行命令sudo yum install -y gcc

    4.cd到redis的解压目录下,并执行

    cd /usr/local/redis-4.0.9/ 此处目录根据下载的redis版本及解压路径调整

    5.编译安装

    make MALLOC=libc

    将/usr/local/redis-4.0.9/src目录下的文件加到/usr/local/bin目录

    cd src && make install

    6.测试是否安装成功

    cd /usr/local/redis-4.0.9/src/

    ./redis-server

    二、Redis主从复制配置

    master的redis.conf文件(其余是默认设置)

    port 6379
    daemonize yes
    # 这个文件夹要改成自己的目录
    dir "/home/redis/redis_master_s"
    

    slaver1和slaver2的redis.conf文件

    port 6378
    # 主服务器端口为6379
    slaveof 127.0.0.1 6379
    dir "/home/redis/redis_slaver1_s"
    port 6377
    # 主服务器端口为6379
    slaveof 127.0.0.1 6379
    dir "/home/redis/redis_slaver2_s"
    

    这个主从服务器就配置好了。

    启动服务

    ./redis-server redis.conf
    

     此时Redis的主从复制配置完毕,主从复制的功能: 
    1. 主服务器写入,从服务器可以读取到 
    2. 从服务器不能写入

    三、Redis的哨兵模式配置

    sentinel是哨兵,用于监视主从服务器的运行状况,如果主服务器挂掉,会在从服务器中选举一个作为主服务器。 
    配置文件如下 
    master的sentinel.conf

    port 26379
    # 初次配置时的状态,这个sentinel会自动更新
    sentinel monitor mymaster 127.0.0.1 6379 2
    daemonize yes
    logfile "./sentinel_log.log"
    sentinel down-after-milliseconds mymaster 10000
    sentinel failover-timeout mymaster 10000
    sentinel config-epoch mymaster 2
    bind 192.168.171.128 127.0.0.1
    sentinel leader-epoch mymaster 2
    

    slaver1和slaver2的sentinel.conf

    port 26378
    # 初次配置时的状态,这个sentinel会自动更新
    sentinel monitor mymaster 127.0.0.1 6379 2
    daemonize yes
    logfile "./sentinel_log.log"
    sentinel down-after-milliseconds mymaster 10000
    sentinel failover-timeout mymaster 10000
    sentinel config-epoch mymaster 2
    bind 192.168.171.128 127.0.0.1
    sentinel leader-epoch mymaster 2
    

    再次启动redis所有的服务端

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

    分别开启redis的客户端

    ./redis-cli
    ./redis-cli -h 127.0.0.1 -p 6378
    ./redis-cli -h 127.0.0.1 -p 6377
    

    使用一下命令查看三个redis服务的状态

    info replication  

     四、代码支持

    RedisCacheConfig

    package com.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisNode;
    import org.springframework.data.redis.connection.RedisSentinelConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import java.lang.reflect.Method;
    
    /**
     * @author David
     */
    @Configuration
    @EnableAutoConfiguration
    @EnableCaching //加上这个注解是的支持缓存注解
    public class RedisCacheConfig extends CachingConfigurerSupport {
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Value("${spring.redis.database}")
        private int database;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Value("${spring.redis.sentinel.nodes}")
        private String redisNodes;
    
        @Value("${spring.redis.sentinel.master}")
        private String master;
    
    
        /**
         * redis哨兵配置
         * @return
         */
        @Bean
        public RedisSentinelConfiguration redisSentinelConfiguration(){
            RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
            String[] host = redisNodes.split(",");
            for(String redisHost : host){
                String[] item = redisHost.split(":");
                String ip = item[0];
                String port = item[1];
                configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
            }
            configuration.setMaster(master);
            return configuration;
        }
    
        /**
         * 连接redis的工厂类
         *
         * @return
         */
        @Bean
        public JedisConnectionFactory jedisConnectionFactory() {
            JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration());
            factory.setHostName(host);
            factory.setPort(port);
            factory.setTimeout(timeout);
            factory.setPassword(password);
            factory.setDatabase(database);
            return factory;
        }
    
        /**
         * 配置RedisTemplate
         * 设置添加序列化器
         * key 使用string序列化器
         * value 使用Json序列化器
         * 还有一种简答的设置方式,改变defaultSerializer对象的实现。
         *
         * @return
         */
        @Bean
        public RedisTemplate<Object, Object> redisTemplate() {
            //StringRedisTemplate的构造方法中默认设置了stringSerializer
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            //设置开启事务
            template.setEnableTransactionSupport(true);
            //set key serializer
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            template.setKeySerializer(stringRedisSerializer);
            template.setHashKeySerializer(stringRedisSerializer);
    
            template.setConnectionFactory(jedisConnectionFactory());
            template.afterPropertiesSet();
            return template;
        }
    
        /**
         * 设置RedisCacheManager
         * 使用cache注解管理redis缓存
         *
         * @return
         */
        @Override
        @Bean
        public RedisCacheManager cacheManager() {
            RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
            return redisCacheManager;
        }
    
        /**
         * 自定义生成redis-key
         *
         * @return
         */
        @Override
        public KeyGenerator keyGenerator() {
            return new KeyGenerator() {
                @Override
                public Object generate(Object o, Method method, Object... objects) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(o.getClass().getName()).append(".");
                    sb.append(method.getName()).append(".");
                    for (Object obj : objects) {
                        sb.append(obj.toString());
                    }
                    System.out.println("keyGenerator=" + sb.toString());
                    return sb.toString();
                }
            };
        }
    }
    

    application.properties

    #========================redis u914Du7F6E=============================
    # Redisu6570u636Eu5E93u7D22u5F15uFF08u9ED8u8BA4u4E3A0uFF09,u5982u679Cu8BBEu7F6Eu4E3A1uFF0Cu90A3u4E48u5B58u5165u7684key-valueu90FDu5B58u653Eu5728select 1u4E2D
    spring.redis.database=0 
    # Redisu670Du52A1u5668u5730u5740
    spring.redis.host=localhost
    # Redisu670Du52A1u5668u8FDEu63A5u7AEFu53E3
    spring.redis.port=6379 
    # Redisu670Du52A1u5668u8FDEu63A5u5BC6u7801uFF08u9ED8u8BA4u4E3Au7A7AuFF09
    spring.redis.password=
    #u8FDEu63A5u6C60u6700u5927u8FDEu63A5u6570uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09
    spring.redis.pool.max-active=8 
    # u8FDEu63A5u6C60u6700u5927u963Bu585Eu7B49u5F85u65F6u95F4uFF08u4F7Fu7528u8D1Fu503Cu8868u793Au6CA1u6709u9650u5236uFF09
    spring.redis.pool.max-wait=-1 
    # u8FDEu63A5u6C60u4E2Du7684u6700u5927u7A7Au95F2u8FDEu63A5
    spring.redis.pool.max-idle=8 
    # u8FDEu63A5u6C60u4E2Du7684u6700u5C0Fu7A7Au95F2u8FDEu63A5
    spring.redis.pool.min-idle=0 
    # u8FDEu63A5u8D85u65F6u65F6u95F4uFF08u6BEBu79D2uFF09
    spring.redis.timeout=0
    ### u4E3Bu4ECEu914Du7F6E
    # name of Redis server  u54E8u5175u76D1u542Cu7684Redis serveru7684u540Du79F0
    spring.redis.sentinel.master=mymaster
    # comma-separated list of host:port pairs  u54E8u5175u7684u914Du7F6Eu5217u8868
    spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26378,127.0.0.1:26377
    

      

  • 相关阅读:
    设计模式之里氏替换原则
    设计模式之依赖倒置原则讲解
    条款10 若不想使用编译器自动生成的函数,就该明确拒绝
    Django---常用字段和参数
    Python中abc
    Python中鸭子类型
    Python多继承的正确打开方式:mixins机制
    python新式类和经典类的区别
    Django---drf权限、频率、过滤、排序、异常处理
    删库跑路技巧 删库跑路命令
  • 原文地址:https://www.cnblogs.com/it-davidchen/p/11276166.html
Copyright © 2020-2023  润新知