Centos系统配置Redis哨兵高可用架构

我们现在开始搭建Redis哨兵集群配置,一主二存+三个哨兵组成Redis哨兵集群。

Redis sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点。哨兵架构下client端第一次从哨兵找出redis的主节点,以后就直接访问redis服务的主节点,不会每次都通过sentinel代理访问redis的主节点,当redis的主节点发生变化,哨兵会第一时间感知到,并且将新的redis主节点通知给client端。

接下来开始搭建Redis哨兵高可用架构:

首先安装Redis,以Redis6.2.6为例。

1.下载redis安装包

wget https://download.redis.io/releases/redis-6.2.6.tar.gz

2.进行解压

tar -zxvf redis-6.2.6.tar.gz

3.移动到/usr/local/目录下,命名为redis

mv redis-6.2.6 /usr/local/redis

4.切换到/usr/local/redis目录并编译

cd /usr/local/redis

make

5.安装到目录/usr/local/redis

make PREFIX=/usr/local/redis install

6. 创建目录,为了方便后期维护

cd /usr/local/redis

mkdir etc #配置文件redis.conf

mkdir logs #日志文件

mkdir data #数据

7.复制配置文件

cd /usr/local/redis

复制主节点master:6379,

存节点slave:6380;存节点slave:6381

#redis.conf为主节点master

cp redis.conf /usr/local/redis/etc/

重写命名redis.conf配置文件

#redis-6380.conf为从节点slave

mv redis.conf redis-6380.conf

#redis-6381.conf为从节点slave

mv redis.conf redis-6381.conf

复制三个哨兵配置文件

cp sentinel.conf /usr/local/redis/etc/

mv sentinel.conf sentinel-26380.conf

mv sentinel.conf sentinel-26381.conf

8.修改配置文件

修改主节点master:6379的配置。

vim redis.conf

#appendonly默认是no,修改为yes开启AOF持久化

appendonly yes

#设置哪些IP可以连接Redis,直接注释掉

#bind 127.0.0.1 -::1

#我们在redis的配置文件中会遇到protected-mode,它直译为保护模式。

#如果设置为yes,那么只允许我们在本机的回环连接,其他机器无法连接。

#线上Redis服务,为了安全,我们建议将protected-mode设置为yes。

#protected-mode设置为yes的情况下,为了我们的应用服务可以正常访问Redis,我们需要#设置Redis的bind参数或者密码参数requirepass

protected-mode yes

#设置Redis启动为后台守护进程

daemonize yes

#pidfile的路径

pidfile /usr/local/redis/logs/redis.pid

#日志文件的路径

logfile /usr/local/redis/logs/redis.log

#持久化数据存放的目录

dir /usr/local/redis/data/6379/

#设置客户端登陆密码

requirepass 123456

#masterauth主节点master授权密码

masterauth 123456

修改存节点slave:6380的配置。

vim redis-6380.conf

#pidfile的路径

pidfile /usr/local/redis/logs/redis-6380.pid

#日志文件的路径

logfile /usr/local/redis/logs/redis-6380.log

#持久化数据存放的目录

dir /usr/local/redis/data/6380/

#master密码,与master 6379配置的密码一致

masterauth 123456

# replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点。

#192.168.1.7根据自己master IP配置,6379是master端口号

replicaof 192.168.1.7 6379

#配置从节点slave为只读

replica‐read‐only yes

其他配置和master一样

存节点6381配置同上。

9.修改哨兵配置

cd /usr/local/redis/etc/

vim sentinel.conf

# 端口

port 26379

# 修改为后台启动方式

daemonize yes

# 密码

requirepass "123456"

# 关闭保护

protected-mode no

# pid

pidfile "/usr/local/redis/logs/redis-sentinel.pid"

# 日志文件

logfile "/usr/local/redis/logs/redis-sentinel.log"

# 数据路径

dir "/usr/local/redis/data"

# 监控地址,为对应的主redis库的内网地址,mymaster为集群名字可随意修改

sentinel monitor mymaster 192.168.1.3 6379 2

# 主节点密码,需要将配置放在sentinel monitor mymaster 192.168.1.3 6379 2下面

sentinel auth-pass mymaster 123456

# 指定了Sentinel认为服务器已经断线所需的毫秒数。

sentinel down-after-milliseconds mymaster 5000

# 设置集群从判断节点挂掉,到执行故障转移(即重新选举master节点)的时间

sentinel failover-timeout mymaster 5000

cd /usr/local/redis/etc/

vim sentinel-26380.conf

# 端口

port 26380

# pid

pidfile "/usr/local/redis/logs/redis-sentinel-26380.pid"

# 日志文件

logfile "/usr/local/redis/logs/redis-sentinel-26380.log"

其他配置sentinel.conf文件一致

cd /usr/local/redis/etc/

vim sentinel-26381.conf

# 端口

port 26381

# pid

pidfile "/usr/local/redis/logs/redis-sentinel-26381.pid"

# 日志文件

logfile "/usr/local/redis/logs/redis-sentinel-26381.log"

其他配置sentinel.conf文件一致

10.启动redis服务

cd /usr/local/redis/bin

# 启动主节点master:6379

./redis-server /usr/local/redis/etc/redis.conf

# 启动存节点slave:6380

./redis-server /usr/local/redis/etc/redis-6380.conf

# 启动存节点slave:6381

./redis-server /usr/local/redis/etc/redis-6381.conf

# 启动哨兵节点

./redis-sentinel /usr/local/redis/etc/sentinel.conf

./redis-sentinel /usr/local/redis/etc/sentinel-26380.conf

./redis-sentinel /usr/local/redis/etc/sentinel-26381.conf

接下来通过springboot项目来测试Redis哨兵高可用架构:redis-sentinel-cluster

项目地址:「链接」

pom.xml配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.saq691</groupId>
	<artifactId>redis-master-slave</artifactId>
	<version>1.0.0-SNAPSHOT</version>

	<name>redis-sentinel-cluster</name>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- Spring boot Web容器undertow -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-undertow</artifactId>
		</dependency>
		<dependency>
			<groupId>io.undertow</groupId>
			<artifactId>undertow-core</artifactId>
		</dependency>
		<dependency>
			<groupId>io.undertow</groupId>
			<artifactId>undertow-servlet</artifactId>
		</dependency>
		<!--Spring boot 测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- spring boot 缓存 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

		<!--Spring boot Redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!--spring boot 集成redis所需common-pool2 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- 打包时跳过测试 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

application.yml配置文件

server:
port: 8080
servlet:
context-path: /
http2:
enabled: true
undertow:
io-threads: 8
worker-threads: 256
buffer-size: 1024
buffers-per-region: 1024
direct-buffers: true

spring:
redis:
# Redis数据库索引(默认为 0)
database: 0
# 连接超时时间(毫秒)
timeout: 5000
# Redis 密码
password: 123456
# 哨兵模式
sentinel:
# 主服务器所在集群名称
master: mymaster
# 哨兵节点
nodes: 192.168.1.3:26379,192.168.1.3:26380,192.168.1.3:26381
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 8
# 连接池中的最大空闲连接
max-idle: 500
# 连接池最大连接数(使用负值表示没有限制)
max-active: 2000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 10000

RedisController类

package com.saq691.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * 
 *
 * @datetime: 2022年11月07日 下午20:12:34
 * @author: sunaiqiang saq691@126.com
 * @version: 1.0
 */
@RestController
public class RedisController {
	private static final Logger logger = LoggerFactory.getLogger(RedisController.class);
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * 
	 * @return
	 */
	@GetMapping("/sentinelCluster")
	public String masterSlave() {
		String result = HttpStatus.OK.getReasonPhrase();
		try {
			for (int i = 0; i < 1000; i++) {
				stringRedisTemplate.opsForValue().set("saq" + i, "saiqiang" + i);
				Thread.sleep(2000);
				logger.info(result + ",key:" + "saq" + i);
			}
		} catch (InterruptedException e) {
			logger.error(e.getMessage());
		}
		return result;
	}
}

springboot主程序启动类

package com.saq691;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 * 
 *
 * @datetime: 2022年11月10日 下午14:11:34
 * @author: sunaiqiang saq691@126.com
 * @version: 1.0
 */
@SpringBootApplication(scanBasePackages = { "com.saq691.*" })
public class RedisSentinelClusterApplication {
	public static void main(String[] args) {
		SpringApplication.run(RedisSentinelClusterApplication.class, args);
	}

}

进行测试,启动程序,在浏览器地址输入URL

http://192.168.1.4:8080/sentinelCluster

接下来把主节点master:6379下线,看看能不能重新选举master主节点。

kill -9 16998

重新选举master:6381成功!说明搭建Redis哨兵高可用架构成功!

原文链接:,转发请注明来源!