網站首頁 編程語言 正文
目錄
Redis配置
redis.conf配置
由于服務器資源有限,我這里通過配置不同端口,模擬Redis集群,redis-server占用6379、6380、6381端口,redis-sentinel占用26379、26380、26381端口。
首先準備3份redis配置文件,分別取名redis6379.conf,redis6380.conf,redis6381.conf
- redis6379.conf,6379端口作為master,需要修改的配置如下
protected-mode no #設置為NO,其他IP才可以訪問
port 6379 #端口
daemonize yes
pidfile "/var/run/redis_6379.pid"
logfile "6379.log" #日志,若redis分布在不同服務器,不用修改
dbfilename "dump6379.rdb" #數據,若redis分布在不同服務器,不用修改
masterauth "admin123/*-" #從節點訪問主節點時需要的密碼
requirepass "admin123/*-" #redis密碼,應用訪問redis時需要
- redis6380.conf,6380端口作為slave,需要設置主節點ip和port,需要修改的配置如下:
protected-mode no #設置為NO,其他IP才可以訪問
port 6380 #端口
daemonize yes
pidfile "/var/run/redis_6380.pid"
logfile "6380.log" #日志,若redis分布在不同服務器,不用修改
dbfilename "dump6380.rdb" #數據,若redis分布在不同服務器,不用修改
replicaof 192.168.1.1 6379 #標記主節點ip+端口,IP設置為自己服務器IP地址即可
masterauth "admin123/*-" #從節點訪問主節點時需要的密碼
requirepass "admin123/*-" #redis密碼,應用訪問redis時需要
備注:slaveof 192.168.1.1 6379,這里需要設置成你自己的IP,可實現Redis 復制功能
- redis6381.conf,6381端口作為slave,同上,需要修改的配置如下:
protected-mode no #設置為NO,其他IP才可以訪問
port 6381 #端口
daemonize yes
pidfile "/var/run/redis_6381.pid"
logfile "6381.log" #日志,若redis分布在不同服務器,不用修改
dbfilename "dump6381.rdb" #數據,若redis分布在不同服務器,不用修改
replicaof 192.168.1.1 6379 #標記主節點ip+端口,IP設置為自己服務器IP地址即可
masterauth "admin123/*-" #從節點訪問主節點時需要的密碼
requirepass "admin123/*-" #redis密碼,應用訪問redis時需要
sentinel.conf配置
準備3分sentinel.conf配置文件,分別為sentinel26379.conf、sentinel26380.conf、sentinel26381.conf。
- sentinel26379.conf,26379端口作為哨兵1,需要修改配置如下
port 26379 #端口
daemonize yes
pidfile "/var/run/redis-sentinel26379.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26379.log"
#配置指示 Sentinel 去監視一個名為 mymaster 的主節點, 主節點的 IP 地址為 127.0.0.1 , 端口號為 6379 ,
#而將這個主服務器判斷為失效至少需要 2 個 Sentinel 同意 (只要同意 Sentinel 的數量不達標,自動故障遷移就不會執行)
sentinel monitor mymaster 192.168.1.1 6379 2
sentinel auth-pass mymaster admin123/*- #哨兵訪問主節點密碼
protected-mode no
- sentinel26380.conf,26380端口作為哨兵2,需要修改配置如下
port 26380 #端口
daemonize yes
pidfile "/var/run/redis-sentinel26380.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26380.log"
#配置指示 Sentinel 去監視一個名為 mymaster 的主節點, 主節點的 IP 地址為 127.0.0.1 , 端口號為 6379 ,
#而將這個主服務器判斷為失效至少需要 2 個 Sentinel 同意 (只要同意 Sentinel 的數量不達標,自動故障遷移就不會執行)
sentinel monitor mymaster 192.168.1.1 6379 2
sentinel auth-pass mymaster admin123/*- #哨兵訪問主節點密碼
protected-mode no
- sentinel26381.conf,26381端口作為哨兵3,需要修改配置如下
port 26381 #端口
daemonize yes
pidfile "/var/run/redis-sentinel26381.pid"
logfile "/usr/local/bin/sentinelLogs/sentinel_26381.log"
#配置指示 Sentinel 去監視一個名為 mymaster 的主節點, 主節點的 IP 地址為 127.0.0.1 , 端口號為 6379 ,
#而將這個主服務器判斷為失效至少需要 2 個 Sentinel 同意 (只要同意 Sentinel 的數量不達標,自動故障遷移就不會執行)
sentinel monitor mymaster 192.168.1.1 6379 2
sentinel auth-pass mymaster admin123/*- #哨兵訪問主節點密碼
protected-mode no
- 啟動redis-server,redis-sentinel
[root@VM-20-5-centos bin]# redis-server redis6379.conf
[root@VM-20-5-centos bin]# redis-server redis6380.conf
[root@VM-20-5-centos bin]# redis-server redis6381.conf
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26379.conf
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26380.conf
[root@VM-20-5-centos bin]# redis-sentinel sentinel_26381.conf
查看狀態,如下圖所示,即為啟動成功
[root@VM-20-5-centos mconfig]# redis-cli -p 26379
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.1.1"
5) "port"
6) "6379"
7) "runid"
127.0.0.1:26379> sentinel slaves mymaster
1) 1) "name"
2) "192.168.1.1:6381"
3) "ip"
4) "192.168.1.1"
5) "port"
6) "6381"
7) "runid"
8) ""
2) 1) "name"
2) "192.75.218.240:6380"
3) "ip"
4) "192.75.218.240"
5) "port"
127.0.0.1:26379>
Springboot整合
導入包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
properties配置
spring.redis.host=192.168.1.1 spring.redis.port=6379 spring.redis.password=admin123/*- spring.redis.sentinel.master=mymaster spring.redis.sentinel.password=admin123/*- spring.redis.sentinel.nodes=192.168.1.1:26379,192.168.1.1:26380,192.168.1.1:26381
測試
代碼示例
package com.mx.learnredis.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Date;
@SpringBootTest
class UserControllerTest {
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;
@Test
public void test() throws InterruptedException {
redisTemplate.opsForValue().set("user", "xx");
while (true)
{
Thread.sleep(2000);
System.out.println("time "+new Date()+":" +redisTemplate.opsForValue().get("user"));
}
}
}
模擬redis宕機
- idea控制臺輸出:
time Sat Jan 08 20:09:12 CST 2022:xx
time Sat Jan 08 20:09:14 CST 2022:xx
time Sat Jan 08 20:09:16 CST 2022:xx
time Sat Jan 08 20:09:18 CST 2022:xx
time Sat Jan 08 20:09:20 CST 2022:xx
time Sat Jan 08 20:09:22 CST 2022:xx
- 關閉掉主節點,登錄linux服務器
[root@VM-20-5-centos mconfig]# ps -ef|grep redis
root???? 27881???? 1? 0 17:19 ???????? 00:00:12 redis-server *:6380
root???? 27997???? 1? 0 17:20 ???????? 00:00:17 redis-sentinel *:26379 [sentinel]
root???? 28017???? 1? 0 17:20 ???????? 00:00:17 redis-sentinel *:26380 [sentinel]
root???? 28030???? 1? 0 17:20 ???????? 00:00:17 redis-sentinel *:26381 [sentinel]
root???? 28471???? 1? 0 20:13 ???????? 00:00:00 redis-server *:6381
root???? 28664???? 1? 0 17:23 ???????? 00:00:12 redis-server *:6379
root???? 28753 11813? 0 20:14 pts/1??? 00:00:00 grep --color=auto redis
[root@VM-20-5-centos mconfig]# kill -9 28664
[root@VM-20-5-centos mconfig]#
- ?idea控制臺輸出,可看到重連日志,Success
time Sat Jan 08 20:15:02 CST 2022:xx
time Sat Jan 08 20:15:04 CST 2022:xx
2022-01-08 20:15:05.010? INFO 7216 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was /192.168.1.1:6379
2022-01-08 20:15:07.211? WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog???? : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:11.920? INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:14.081? WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog???? : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:18.617? INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:20.767? WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog???? : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:26.119? INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:28.278? WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog???? : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:33.720? INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:35.880? WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog???? : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:40.020? INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog???? : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:40.160? INFO 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ReconnectionHandler??? : Reconnected to 192.168.1.1:6381
time Sat Jan 08 20:15:06 CST 2022:xx
原文鏈接:https://blog.csdn.net/qq_34616001/article/details/122383914
- 上一篇:記一次線上sql優化從9s到400ms的過程
- 下一篇:C++四種強制類型轉換
相關推薦
- 2022-08-06 pythonfor循環中range與len區別_python
- 2022-09-22 Windows MongoDB服務無法啟動,錯誤 1053的一種情況和解決辦法
- 2022-12-25 go?slice不同初始化方式性能及數組比較詳解_Golang
- 2022-02-09 C++解決輸出鏈表中倒數k個結點的問題_C 語言
- 2022-09-03 Python實現計算AUC的示例代碼_python
- 2022-09-22 繼承關系下構造方法的訪問特點
- 2024-07-13 Spring AOP 基于注解的方式實現切面遍程
- 2022-06-26 Docker上部署Nginx的方法步驟_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支