天天看点

Centos系统搭建Redis高可用集群架构

作者:强函数

Redis集群是一个由多个主从节点群组成的分布式服务器群,它具有复制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能。需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,根据官方文档称可以线性扩展到上万个节点(官方推荐不超过1000个节点)。Redis集群的性能和高可用性均优于哨兵模式,并且集群配置非常简单。Redis集群需要至少三个master节点,搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个Redis节点,用三台机器部署6个Redis实例,每台机器一主一从。

接下来我们开始搭建Redis高可用集群架构。

首先安装Redis,以Redis6.2.6为例,准备三台虚拟机,分别为

第一台IP:192.168.1.6,安装Redis节点6379、6380。

第二台IP:192.168.1.7,安装Redis节点6381、6382。

第三台IP:192.168.1.8,安装Redis节点6383、6384。

总共6个Redis节点,用三台机器部署6个Redis实例。

Centos系统搭建Redis高可用集群架构

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

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

#redis.conf配置6379节点

重写命名redis.conf配置文件

#redis-6380.conf为6380节点

mv redis.conf redis-6380.conf

8.修改配置文件

第一台IP:192.168.1.6,修改Redis实例6379、6380配置。

修改Redis: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-6379.pid

#日志文件的路径

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

#持久化数据存放的目录

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

#设置客户端登陆密码

requirepass 123456

#masterauth主节点master授权密码

masterauth 123456

#开启集群模式

cluster‐enabled yes

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6379.conf

#集群节点的超时时限

cluster‐node‐timeout 15000

修改Redis:6380实例配置。

vim redis-6380.conf

#端口号

port 6380

#pidfile的路径

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

#日志文件的路径

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

#持久化数据存放的目录

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

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6380.conf

其他配置和Redis实例6379一样。

第二台IP:192.168.1.7,修改Redis实例6381、6382配置。

修改Redis:6381实例配置。

vim redis-6381.conf

#端口号

port 6381

#pidfile的路径

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

#日志文件的路径

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

#持久化数据存放的目录

dir /usr/local/redis/data/6381/

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6381.conf

其他配置和第一台(IP:192.168.1.6)Redis实例6379一样。

修改Redis:6382实例配置。

vim redis-6382.conf

#端口号

port 6382

#pidfile的路径

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

#日志文件的路径

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

#持久化数据存放的目录

dir /usr/local/redis/data/6382/

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6382.conf

其他配置和第一台(IP:192.168.1.6)Redis实例6379一样。

第三台IP:192.168.1.8,修改Redis实例6383、6384配置。

修改Redis:6383实例配置。

vim redis-6383.conf

#端口号

port 6383

#pidfile的路径

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

#日志文件的路径

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

#持久化数据存放的目录

dir /usr/local/redis/data/6383/

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6383.conf

其他配置和第一台(IP:192.168.1.6)Redis实例6379一样。

修改Redis:6384实例配置。

vim redis-6384.conf

#端口号

port 6384

#pidfile的路径

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

#日志文件的路径

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

#持久化数据存放的目录

dir /usr/local/redis/data/6384/

#集群节点信息文件,最好与port端口对应

cluster‐config‐file nodes‐6384.conf

其他配置和第一台(IP:192.168.1.6)Redis实例6379一样。

9.分别启动6个Redis实例,然后检查是否启动成

cd /usr/local/redis/bin

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

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

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

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

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

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

10.查看是否启动成功

ps -ef|grep redis

11.用redis‐cli创建整个Redis集群

在任意一台执行以下命令创建Redis集群

cd /usr/local/redis/bin

./redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.1.6:6379 192.168.1.6:6380 192.168.1.7:6381 192.168.1.7:6382 192.168.1.8:6383 192.168.1.8:6384

备注:

./redis-cli --cluster:代表集群操作命令

-a:访问服务端密码

create:代表是创建集群

--cluster-replicas 1:1指定集群中每个master的副本个数为1。

运行后:

Centos系统搭建Redis高可用集群架构
Centos系统搭建Redis高可用集群架构
slots:[5461-10922] 分配的哈希槽位

12.验证集群,查看集群信息

cd /usr/local/redis/bin

连接任意一个客户端即可:./redis‐cli ‐c ‐h ‐p

‐a访问服务端密码

‐c表示集群模式,

指定ip地址和端口号

如:./redis-cli -a 123456 -c -h 192.168.1.6 -p 6379

进行验证:

查看集群信息

cluster info

查看集群节点列表

cluster nodes

Centos系统搭建Redis高可用集群架构
Centos系统搭建Redis高可用集群架构
Centos系统搭建Redis高可用集群架构

关闭集群则需要个进行关闭,使用命令:

cd /usr/local/redis/bin

./redis-cli -a 123456 -c -h 192.168.1.6 -p 6379 shutdown

接下来我们通过springboot项目测试Redis高可用集群架构。

项目地址:「链接」

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-cluster</artifactId>
	<version>1.0.0-SNAPSHOT</version>

	<name>redis-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

# 集群模式

cluster:

# 集群节点

nodes: 192.168.1.6:6379,192.168.1.6:6380,192.168.1.7:6381,192.168.1.7:6382,192.168.1.8:6383,192.168.1.8:6384

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月18日 下午20:12:34
 * @author: sunaiqiang [email protected]
 * @version: 1.0
 */
@RestController
public class RedisController {
	private static final Logger logger = LoggerFactory.getLogger(RedisController.class);
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * 
	 * @return
	 */
	@GetMapping("/cluster")
	public String redisCluster() {
		String result = HttpStatus.OK.getReasonPhrase();
		stringRedisTemplate.opsForValue().set("saq", "saiqiang");
		String key = stringRedisTemplate.opsForValue().get("saq");
		logger.info(key);
		return result;
	}

}           

springboot主程序启动类

package com.saq691;

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

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

}           

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

http://192.168.1.5:8080/cluster

执行结果显示OK。

至此我们redis高可用集群搭建成功!

继续阅读