redis 高可用主从+哨兵模式配置以及整合springboot使用

7 篇文章 0 订阅
6 篇文章 0 订阅

首先准备三台redis服务。高可用基本上都是选用大于3台服务

先设置主从关系:

先选用一个节点为主节点。我这里用6380作为主。在其他两个服务redis.conf中增加slaveof 127.0.0.1:6380

因为是要做哨兵模式。所以每个节点都要设置可以写。在三个服务中redis.conf更改 slave-read-only为no。

并且找到masterauth XXXXX 修改。这个密码主节点的密码。最好对于这三个redis服务密码都设置一样。

这样主从就搞好了。接下来做哨兵

在三个redis服务中增加这个文件

sentinel.conf这样配置 每个文件的端口需要自行更改

====================================================================

port 26370 #
bind 0.0.0.0
# 禁止保护模式
protected-mode no
# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,192.168.11.128代表监控的主服务器,6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel myid 4bb87fdc9adb28a64463d734ef7afee092fad9bb
# sentinel author-pass定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
# sentinel auth-pass <master-name> <password>
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 7000
# Generated by CONFIG REWRITE
sentinel failover-timeout mymaster 15000
sentinel auth-pass mymaster root123456abc
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-slave mymaster 127.0.0.1 6370

daemonize yes  #守护进程
 

=======================================================================================

配置好后。如果启动。关掉其中主服务。有的不会自动切换。在redis.conf和sentinel.con 修改bind 0.0.0.0 和protected-mode no

然后就是把服务启动。

redis服务启动命令 :redis-server.exe redis.windows.conf

哨兵启动命令:redis-server.exe sentinel.conf --sentinel

关掉redis主服务。就会看到哨兵会自动切换。但是切换会有点几秒的延迟。 

 

接下来就是springboot整合了在yml中增加redis配置

第一步:

spring:
  redis:
    host: localhost # Redis服务器地址
    database: 0 # Redis数据库索引(默认为0)
    port: 6379 # Redis服务器连接端口
    password: root123456abc # Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接
    timeout: 10000
    sentinel:
      master: mymaster
      nodes: localhost:26379,localhost:26380,localhost:26370

 

第二步增加此类

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.net.UnknownHostException;

@Configuration
public class RedisSentinelConfig 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哨兵配置
    @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);
        configuration.setPassword("root123456abc");
        return configuration;
    }
    @Bean
    @ConfigurationProperties(prefix = "redis.jedis")
    public JedisPoolConfig jedisPoolConfig1(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setTestOnBorrow(true);
        return jedisPoolConfig;
    }
    //连接redis的工厂类
    @Autowired
    private JedisPoolConfig jedisPoolConfig;

    @Autowired
    private RedisSentinelConfiguration redisSentinelConfiguration;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory =
                new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig);
        return jedisConnectionFactory;
    }

    //配置RedisTemplate,设置添加序列化器,key 使用string序列化器,value 使用Json序列化器,还有一种简答的设置方式,改变defaultSerializer对象的实现。
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @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;
    }
    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setEnableTransactionSupport(true);
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}

这样就大功告成了。实现了高可用。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
Redis主从复制是常用的数据备份和负载均衡方案之一。在主从复制中,主节点负责写操作并将数据同步到从节点,从节点负责读操作。 要配置Redis主从复制,需要进行以下步骤: 1. 配置主节点: - 打开主节点的配置文件 `redis.conf`。 - 将 `bind` 设置为主节点的 IP 地址。 - 将 `port` 设置为主节点的端口号。 - 将 `daemonize` 设置为 `yes`,以使 Redis 以守护进程模式运行。 - 取消注释并设置 `replicaof`,指定从节点的 IP 地址和端口号。 2. 配置从节点: - 复制主节点的配置文件 `redis.conf` 到从节点,并重命名为 `redis.conf`。 - 打开从节点的配置文件 `redis.conf`。 - 将 `bind` 设置为从节点的 IP 地址。 - 将 `port` 设置为从节点的端口号。 - 将 `daemonize` 设置为 `yes`。 - 取消注释并设置 `replicaof`,指定主节点的 IP 地址和端口号。 3. 启动主从节点: - 分别启动主节点和从节点的 Redis 服务器。 4. 验证主从复制: - 使用命令 `INFO replication` 在主节点和从节点上检查复制信息。 - 在主节点上执行写操作,然后在从节点上执行读操作,验证数据同步是否正常。 对于哨兵模式,它在主从复制的基础上提供了故障转移和自动故障恢复的功能。在哨兵模式中,有一个或多个哨兵节点负责监控主节点和从节点的状态,并在主节点出现故障时自动将一个从节点升级为新的主节点。 要配置Redis哨兵模式,需要进行以下步骤: 1. 配置哨兵节点: - 复制主节点的配置文件 `redis.conf` 到哨兵节点,并重命名为 `redis.conf`。 - 打开哨兵节点的配置文件 `redis.conf`。 - 将 `sentinel monitor` 设置为监视的主节点名称、主节点 IP 地址、主节点端口号和需要的从节点数量。 - 可以设置其他选项,如 `sentinel down-after-milliseconds`、`sentinel failover-timeout` 等。 2. 启动哨兵节点: - 启动所有哨兵节点的 Redis 服务器。 3. 验证哨兵模式: - 使用命令 `redis-cli -p <哨兵节点端口号>` 连接到哨兵节点。 - 使用命令 `SENTINEL get-master-addr-by-name <主节点名称>` 检查当前主节点的 IP 地址和端口号。 通过以上步骤,你将成功配置Redis主从复制和哨兵模式。这将提供数据备份、负载均衡和故障转移的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王威振的csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值