Redis在windows和Alibaba Cloud linux下配置主从和哨兵

window环境

主从配置和启动

  1. 复制redis文件夹
    复制文件夹
  2. 修改从库的配置文件“redis.windows.conf”,并添加“slaveof”属性

#端口号
port 6380
#是否为守护进程
daemonize no
#从库是否为只读
slave-read-only yes
#是谁的从库
slaveof {masterHost} {masterPort}

  1. 使用 redis-server.exe redis.windows.conf 启动

哨兵配置和启动

  1. 在redis主库和从库的文件夹中分别创建一个新的配置文件“sentinel.conf”
    新建文件夹
  2. 在配置文件“sentinel.conf”中添加配置

#端口号
port 26379
#sentinel monitor 主库名称 主库地址:主库端口号 客观下线数
sentinel monitor mymaster 127.0.0.1 6379 2
#是否为守护进程
daemonize yes
#日志文件地址
logfile “./sentinel_log.log”

  1. 使用“redis-server.exe sentinel.conf --sentinel”命令启动
    启动哨兵

注:每个redis-server可以同时启动库和哨兵,哨兵启动后无日志打印,并不是进程卡主。
配置文件中的主库地址,只是一个默认的主库(即redis启动时的主库地址)。在后续的运行中,无论哪个从库变成主库,都会继续被哨兵监听。

例如,默认情况下主库为6379,从库为6380和6381。
当主库下线后6380升为主库,6381改为6380的从库。当6379重新上线后,会变为6380的从库;
6381作为主库下线后,哨兵还是会监听到,继续在从库中推选一个变为主库,继续上述的行动。

Alibaba Cloud Linux中使用systemctl管理哨兵

我是使用"yum"来安装的redis。windows和linux配置方式基本一致,这里主要讲的是如何配置来使用“systemctl”来更方便的控制redis

yum install redis

主从配置和启动

推荐使用“find”命令来找redis相关的文件,会方便很多
在这里插入图片描述

  1. 找到“redis.conf”文件后,使用“cp”命令来复制从库配置

cp redis.conf redis_6380.conf

  1. 然后使用“vim”编辑文件

vim redis_6380.conf

  1. 修改配置文件

#端口号
port 6381
#日志文件
logfile “/var/log/redis/redis_6381.log”
#数据库文件名称
dbfilename “dump_6381.rdb”
#数据库地址
dir “/var/lib/redis_6381”

  1. 修改后到对应的路径创建缺少的文件(如:redis_6381.log、dump_6381.rdb)
  2. systemctl真正使用的文件是“/usr/lib/systemd/system/redis.service”
  3. 使用vim编辑该文件
    在这里插入图片描述
  4. 使用“systemctl daemon-reload”来更新配置
  5. 然后使用“systemctl start 你配置的启动名称”来启动redis
  6. “systemctl status 启动名称”来查看启动情况
    在这里插入图片描述

哨兵配置和启动

  1. 在redis配置文件的路径下,新建哨兵配置文件(当目标文件不存在时,vim会新建

vim redis_26379.conf

  1. 添加配置

port 26379
daemonize yes
logfile “/var/log/redis/sentinel/redis_26379.log”
sentinel monitor mymaster 127.0.0.1 6379 2

  1. 在“redis.service”目录下,复制“redis.service”文件

cp redis.service redis_26379.service

  1. 修改service文件,主要修改启动程序、配置文件、启动时服务名称
    在这里插入图片描述
  2. 使用“systemctl start 启动名称”来启动服务
  3. 到你配置的日志文件地址查看日志
    在这里插入图片描述

springboot整合redisTeamplate主从+哨兵

  1. 添加引用

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

  1. 添加配置

spring:
     redis:
         #哨兵配置
         sentinel:
             #默认情况下的主库名称
             master: mymaster
             nodes:
                 - 127.0.0.1:26379
                 - 127.0.0.1:26380
                 - 127.0.0.1:26381

  1. 直接在需要的地方引用实例即可
    @Autowired
    private StringRedisTemplate redis;

	@GetMapping("redis")
    public String redis(){
//        redis.opsForValue().set("name123","test_2021");
        return redis.opsForValue().get("name123");
    }

注:主从配置中没有体现,但是sentinel会包含主从关系,在项目运行时依旧会有主从关系

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值