天天看點

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高可用叢集搭建成功!

繼續閱讀