網站首頁 編程語言 正文
本文演示一主二從。
先說一下遇到的問題。我看網上說想配置哨兵,必須讓啟動redis的docker網絡模式為host,否則無法訪問到從還是什么的。我指定--network host
后無法外網訪問,redis desktop manager連不上redis了,后來發現我這個新克隆的機器沒關防火墻,關上就好了。
1.配置主從
docker pull一下redis
master
mkdir -p /mydata/redis/6379/conf mkdir -p /mydata/redis/6379/data touch /mydata/redis/6379/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6379/conf/redis.conf docker run --network host --name redis6379 -v /mydata/redis/6379/data:/data -v /mydata/redis/6379/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6379
slave
#slave1 mkdir -p /mydata/redis/6380/conf mkdir -p /mydata/redis/6380/data touch /mydata/redis/6380/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6380/conf/redis.conf docker run --network host --name redis6380 -v /mydata/redis/6380/data:/data -v /mydata/redis/6380/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6380 --slaveof 192.168.243.4 6379 #slave2 mkdir -p /mydata/redis/6381/conf mkdir -p /mydata/redis/6381/data touch /mydata/redis/6381/conf/redis.conf echo "appendonly yes" >> /mydata/redis/6381/conf/redis.conf docker run --network host --name redis6381 -v /mydata/redis/6381/data:/data -v /mydata/redis/6381/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6381 --slaveof 192.168.243.4 6379
查看配置:
#主 docker exec -it redis6379 redis-cli >info replication #從 docker exec -it redis6380 redis-cli -p 6380 >info replication
2. 配置哨兵
master
mkdir /mydata/redis/6379/sentinel mkdir /mydata/redis/6379/sentinel/log vi /mydata/redis/6379/sentinel/sentinel.conf #輸入 port 26379 dir "/var/log/sentinel" logfile "/var/log/sentinel/26379.log" sentinel monitor mymaster 192.168.243.4 6379 1 #這里暫時設置成1  #方便看一下主掛掉是什么效果 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26379 -v /mydata/redis/6379/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6379/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf
先進行測試:
docker exec -it redis6379 redis-cli > SHUTDOWN > exit cat /mydata/redis/6379/sentinel/log/26379.log
此時redis6381已成為新的master
再啟動docker start redis6379
6379是slave了
測試成功,修改上面配置文件的sentinel monitor mymaster 192.168.243.4 6379 2
數字配置為2,代表至少有2個Sentinel節點認為主節點不可達,那么這個不可達的判定才是客觀的。為了防止票數相同,sentinel啟動奇數個。
slave
#slave1 mkdir /mydata/redis/6380/sentinel mkdir /mydata/redis/6380/sentinel/log vi /mydata/redis/6380/sentinel/sentinel.conf #輸入 port 26380 dir "/var/log/sentinel" logfile "/var/log/sentinel/26380.log" sentinel monitor mymaster 192.168.243.4 6380 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26380 -v /mydata/redis/6380/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6380/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf #slave2 mkdir /mydata/redis/6381/sentinel mkdir /mydata/redis/6381/sentinel/log vi /mydata/redis/6381/sentinel/sentinel.conf #輸入 port 26381 dir "/var/log/sentinel" logfile "/var/log/sentinel/26381.log" sentinel monitor mymaster 192.168.243.4 6381 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 #end docker run -d --name sentinel26381 -v /mydata/redis/6381/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6381/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf
3.SpringBoot連接
application.yml
spring: redis: timeout: 5000 sentinel: master: mymaster nodes: 192.168.243.4:26379,192.168.243.4:26380,192.168.243.4:26381
controller
@Autowired private StringRedisTemplate redisTemplate; @RequestMapping("/redis") public String redis() { redisTemplate.opsForValue().set("test", "121323123"); String test = redisTemplate.opsForValue().get("test"); return "RESULT: " + test; }
訪問localhost:8080/redis
原文鏈接:https://blog.csdn.net/m0_52640724/article/details/124904019
相關推薦
- 2022-07-04 python設計模式之裝飾器模式_python
- 2022-09-24 ASP.NET?MVC創建XML文件并實現元素增刪改_實用技巧
- 2022-07-11 Android?Studio實現注冊頁面跳轉登錄頁面的創建_Android
- 2022-06-26 django中模板繼承與ModelForm實例詳解_python
- 2022-10-07 React中父組件如何獲取子組件的值或方法_React
- 2022-11-18 詳解C語言內核字符串拷貝與比較_C 語言
- 2022-06-06 visualStudioCode自動添加、補全雙引號、vsc、配置
- 2022-12-12 Python中函數帶括號和不帶括號的區別及說明_python
- 最近更新
-
- 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同步修改后的遠程分支