Redis哨兵模式

因工作项目中要求将当前的项目Redis单例模式改成哨兵模式,简单的记录一下本地自己实现的过程。

一、下载安装redis
redis可以从官网或者github上下载
官网地址:https://redis.io/download/
在这里插入图片描述

github地址:https://github.com/microsoftarchive/redis/releases

在这里插入图片描述
按照自己的项目需求中的redis版本来下载

下载后之后,将zip文件解压后,里面的文件内容如下图
在这里插入图片描述
在当前redis文件中右键,点击弹出框中 “在终端中打开”,在里面输入启动命令

redis-server.exe redis.windows.conf

在这里插入图片描述
在这里插入图片描述
执行命令之后,成功则会显示如下图,也可以直接点击redis文件中的 “redis-server.exe”来启动

在这里插入图片描述
在这里插入图片描述
启动redis实例成功之后,可以在命令窗口或者在redis文件中点击“redis-cli.exe”来运行客户端

redis-cli.exe -p 6379

在这里插入图片描述
在这里插入图片描述

此时可以在客户端上进行数据读写操作

set xxxx(key) xxxx(value)

get xxxx(key)

在这里插入图片描述

二、搭建redis的主从
将上述解压后的redis文件,copy两份,并以端口号来进行区分
在这里插入图片描述

然后修改copy的两份redis中的redis.windows.conf端口号修改成对应端口号,并设置为从节点

在这里插入图片描述
在这里插入图片描述
启动主节点,再启动从节点

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
从主节点中进入到客户端,并进行查询命令

info repliaction

在这里插入图片描述
然后再运行从节点的客户端,并执行查看数据命令,可以发现从主机中的数据已经同步推送到了从机上。

keys *

在这里插入图片描述
在这里插入图片描述

三、搭建的redis哨兵
需要在各个redis文件夹中新增sentinel.conf配置文件,并写入配置端口号,分别26379、26380、26381

port 26380
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 2

配置完成后,通过命令窗口输入命令启动哨兵

redis-server.exe sentinel.conf --sentinel

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
启动成功后,现在可以来验证是否能够实现宕机后,能否自动切换master
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、Springboot项目上的使用

配置文件.properties的内容
在这里插入图片描述

package com.dh.demos.web;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author : lssffy
 * @Description : 配置类获取
 * @date : 2023/8/29 10:23
 */
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {


    private RedisProperties.Sentinel sentinel = new Sentinel();

    public Sentinel getSentinel() {
        return sentinel;
    }

    public void setSentinel(Sentinel sentinel) {
        this.sentinel = sentinel;
    }


    public static class Sentinel{
        private String master;
        private String[] nodes;

        public String getMaster() {
            return master;
        }

        public void setMaster(String master) {
            this.master = master;
        }

        public String[] getNodes() {
            return nodes;
        }

        public void setNodes(String[] nodes) {
            this.nodes = nodes;
        }
    }
}

在这里插入代码片package com.dh.demos.web;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author : lssffy
 * @Description :
 * @date : 2023/8/25 13:55
 */
@Configuration
public class RedisConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Autowired
    private Environment env;

    @Bean
    public RedissonClient redissonClient(){
        String[] nodesStr = redisProperties.getSentinel().getNodes();
        for (int i = 0; i < nodesStr.length; i++) {
            nodesStr[i] = "redis://" + nodesStr[i];
        }
        Config config = new Config();
        config.setCodec(JsonJacksonCodec.INSTANCE);
        SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
                .setMasterName(redisProperties.getSentinel().getMaster())
                .addSentinelAddress(nodesStr).setReadMode(ReadMode.MASTER);

        RedissonClient client = Redisson.create(config);
        return client;
    }

}

package com.dh.demos.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

/**
 */
@Controller
public class BasicController {


    @RequestMapping("redis/example")
    @ResponseBody
    public String hello(@RequestParam(name = "value", defaultValue = "unknown user") String value) {
        Jedis jedis = new Jedis();
        jedis.set("redisDemo:basic:" + value, value);
        return "Hello " + value;
    }

}

在这里插入图片描述

启动run服务后,访问 http://localhost:8080/redis/example?value=8

在这里插入图片描述

此时可以到redis里面去查看是否写入成功,然后再次执行主机shutdown操作,再次执行接口看是否还能正常写入和读取数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CopyLower

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

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

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

打赏作者

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

抵扣说明:

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

余额充值