轻蜗牛直租平台-redis sentinel伪集群搭建过程

背景:做一个创业项目或者大型项目肯定会依赖各种中间件,因此涉及到各个中间件的安装运行,接入。因此本系列先不讲业务方面的东西,方便其他开发小伙伴快速融入到业务开发中。因此先介绍一下目前本地已经在跑的中间件系统。后续会逐步集成其他微服务组件,敬请期待。

1. redis sentinel伪集群搭建过程

  1. elasticsearch.6.4.3 单机搭建过程
  2. nacos1.3.1 单机搭建过程
  3. rocketmq单机搭建过程
  4. xxl-job平台搭建过程
  5. shardingjdbc集成springboot的过程

redis版本号:Redis-x64-3.2.100.zip
操作系统:win10系统

1.解压Redis-x64-3.2.100.zip

2.复制文件三份

3.配置redis.windows.conf文件&配置哨兵文件sentinel.conf

这里配置redis.windows.conf内容就不展开了,只配与哨兵相关的配置即可,后续考虑优化配置。

# ------------------------------------------------------------------------------------ #
# 这个是Redis-6379的配置内容,其它两个Redis-6380、Redis-6381同理新增然后改一下端口即可 #
# ------------------------------------------------------------------------------------ #
 
# 当前Sentinel服务运行的端口
# 在默认情况下,Sentinel 使用 TCP 端口 26379(普通 Redis 服务器使用的是 6379 )
port 26379
 
# 哨兵监听的主节点mymaster;最后面的数字 3 表示最低通过票数;# 默认值 2
# 如果投票通过,则哨兵群体认为该主节点客观下线(odowm)
sentinel monitor mymaster 127.0.0.1 6379 3
 
# 哨兵认定当前主节点mymaster失效的判别间隔时间
# 如果在设置的时间内(毫秒),当前主节点没有响应或者响应错误代码,则当前哨兵认为该主节点主主观下线(sdown)
# 3s内mymaster无响应,则认为mymaster宕机了
sentinel down-after-milliseconds mymaster 3000
 
# 执行故障转移时,最多有1个从节点同时对新的主节点进行同步
# 当新的master上位时,允许从节点同时对新主节点进行同步的从节点个数;默认是1,建议保持默认值
# 在故障转移期间,将会终止客户端的请求
# 如果此值较大,则意味着"集群"终止客户端请求的时间总和比较大
# 反之此值较小,则意味着"集群"在故障转移期间,多个从节点仍可以提供服务给客户端
sentinel parallel-syncs mymaster 1
 
# 故障转移超时时间。
# 当故障转移开始后,但是在此时间内仍然没有触发任何故障转移操作,则当前哨兵会认为此次故障转移失败
sentinel failover-timeout mymaster 10000

4.配置redis server的启动脚本

redis-6379-master-server.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6379\
START %DIR%redis-server.exe %DIR%redis.windows.conf


redis-6380-slave-server.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6380\
START %DIR%redis-server.exe %DIR%redis.windows.conf

redis-6381-slave-server.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6381\
START %DIR%redis-server.exe %DIR%redis.windows.conf

5.配置redis-sentinel 启动脚本

redis-6379-sentinel.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6379\
START %DIR%redis-server.exe %DIR%sentinel.conf --sentinel

redis-6380-sentinel.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6380\
START %DIR%redis-server.exe %DIR%sentinel.conf --sentinel

redis-6381-sentinel.bat文件内容如下:
@echo off
SET DIR=%~dp0\Redis6381\
START %DIR%redis-server.exe %DIR%sentinel.conf --sentinel

6.配置redis client启动脚本

redis-6379-mater-client.bat文件内容如下:
@echo off
START %~dp0\Redis6379\redis-cli.exe -p 6379

redis-6380-slave-client.bat文件内容如下:
@echo off
START %~dp0\Redis6380\redis-cli.exe -p 6380

redis-6381-slave-client.bat文件内容如下:
@echo off
START %~dp0\Redis6381\redis-cli.exe -p 6381

启动顺序:
1.先启动三个server节点,通过server.bat双击即可
2.启动三个sentinel节点,通过
sentinel.bat双击即可
3.启动三个client节点校验集群是否搭建成功,通过 **client.bat双击即可。

本地配置效果:
在这里插入图片描述

集成springboot:
在resources目录中新建application-redis.yml文件,内容如下:

spring:
  redis:
    password: light_snail_app&.@188
    jedis:
      pool:
        #最大连接数
        max-active: 500
        #最大阻塞等待时间(负数表示没限制)
        max-wait: 20000
        #最大空闲
        max-idle: 100
        #最小空闲
        min-idle: 10
    sentinel:
      master: mymaster
      #这里链接的是redis sentinel 主节点,因为会往从节点写数据导致报错
      #READONLY You can’t write against a read only slave.
      #解决方法:进入redis.conf配置文件,修改配置文件的slave-read-only为no
      #这样的解决方法其实也有问题
      nodes: localIP:26379

通过application.yml文件激活配置:

spring:
  profiles:
    active: redis

引入pom.xml依赖

    <!-- redis start -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <exclusions>
        <exclusion>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.lettuce</groupId>
          <artifactId>lettuce-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.5.0</version>
    </dependency>
    <!-- redis end -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值