redis主從+哨兵搭建的實(shí)現(xiàn)示例
本文主要介紹了redis主從+哨兵搭建的實(shí)現(xiàn)示例,具體如下:

1. 工程準(zhǔn)備
將安裝包、配置文件、啟動(dòng)腳本統(tǒng)一放置到redis-m-s工程中。

2. 基于Dockerfile構(gòu)建
2.1 拷貝工程到工作目錄
# 設(shè)置工作目錄
WORKDIR ${WORK_DIR}
# 文件拷貝:將所有本地文件拷貝到容器目錄
COPY ./redis-m-s ${WORK_DIR}/redis-m-s
2.2 安裝依賴包
RUN yum -y install make gcc gcc-c++
2.3 解壓redis安裝包、編譯、安裝
RUN tar -xzvf ${WORK_DIR}/redis-m-s/app/redis-${REDIS_VERSION}.tar.gz && \
cd redis-${REDIS_VERSION} && \
make && make PREFIX=${SRC_APP_DIR}/redis install && \
2.4 拷貝配置文件到安裝目錄
拷貝配置文件和啟動(dòng)腳本
mkdir -p ${SRC_APP_DIR}/redis/bin/ && \
mkdir -p ${SRC_APP_DIR}/redis/conf/ && \
cp ${WORK_DIR}/redis-m-s/conf/* ${SRC_APP_DIR}/redis/conf && \
cp ${WORK_DIR}/redis-m-s/bin/* ${SRC_APP_DIR}/redis/bin && \
chmod +x ${SRC_APP_DIR}/redis/bin/start_redis.sh && \
清理安裝包
cd ${WORK_DIR} && \
rm -rf redis-${REDIS_VERSION}
3. 配置文件
3.1 redis-master.conf
基于安裝目錄下redis.conf進(jìn)行修改,主要修改項(xiàng):
bind 0.0.0.0 protected-mode no logfile /export/Logs/redis/master/redis.log dir /export/Data/redis/db requirepass your_pass
3.2 redis-slave.conf
基于安裝目錄下redis.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
bind 0.0.0.0 protected-mode no logfile /export/Logs/redis/slave/redis.log dir /export/Data/redis/db replicaof master_ip 6379 masterauth your_pass requirepass your_pass
3.3 sentinel-master.conf
基于安裝目錄下sentinel.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
logfile /export/Logs/redis/sentinel/sentinel.log sentinel monitor redis-master master-ip 6379 1 sentinel auth-pass redis-master your_pass sentinel down-after-milliseconds redis-master 30000 sentinel parallel-syncs redis-master 1 sentinel failover-timeout redis-master 180000 SENTINEL master-reboot-down-after-period redis-master 0 # 容器部署時(shí)需聲明 sentinel announce-ip master_ip sentinel announce-port 26379
3.4 sentinel-slave.conf
基于安裝目錄下sentinel.conf進(jìn)行修改,主要修改項(xiàng)如下,注意將master_ip替換為實(shí)際IP
logfile /export/Logs/redis/sentinel/sentinel.log sentinel monitor redis-master master-ip 6379 1 sentinel auth-pass redis-master your_pass sentinel down-after-milliseconds redis-master 30000 sentinel parallel-syncs redis-master 1 sentinel failover-timeout redis-master 180000 SENTINEL master-reboot-down-after-period redis-master 0 # 容器部署時(shí)需聲明 sentinel announce-ip slave_ip sentinel announce-port 26379
4. 啟動(dòng)redis
4.1 redis主節(jié)點(diǎn)+哨兵啟動(dòng)
redis-server $REDIS_HOME/conf/redis-master.conf & redis-sentinel $REDIS_HOME/conf/sentinel-master.conf &
4.2 redis從節(jié)點(diǎn)+哨兵啟動(dòng)
redis-server $REDIS_HOME/conf/redis-slave.conf & redis-sentinel $REDIS_HOME/conf/sentinel-slave.conf &
5. 驗(yàn)證redis
5.1 登錄主節(jié)點(diǎn)
下述 slave_ip 為從節(jié)點(diǎn)實(shí)際IP
# ./redis-cli > auth your_pass OK > info replication # Replication role:master connected_slaves:1 slave0:ip=slave_ip,port=6379,state=online,offset=28160117,lag=1 master_failover_state:no-failover master_replid:201648fb313a4359fea11e57c434afe8f372316d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28160410 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:27103441 repl_backlog_histlen:1056970 >
5.2 登錄哨兵
下述 master_ip 為主節(jié)點(diǎn)IP,slave_ip為從節(jié)點(diǎn)IP
./redis-cli -h master_ip -p 26379
> sentinel sentinels redis-master
1) 1) "name"
2) "a23117a8d41fb1ace5785a5b940e7e43e5efe316"
3) "ip"
4) "slave_ip"
5) "port"
6) "26379"
7) "runid"
8) "a23117a8d41fb1ace5785a5b940e7e43e5efe316"
9) "flags"
10) "sentinel"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "988"
19) "last-ping-reply"
20) "988"
21) "down-after-milliseconds"
22) "30000"
23) "last-hello-message"
24) "49"
25) "voted-leader"
26) "?"
27) "voted-leader-epoch"
28) "0"
>
6. springboot應(yīng)用集成redis
6.1 配置文件
spring:
redis:
sentinel:
master: redis-master
nodes:
- redis://master_ip:26379
- redis://slave_ip:26379
password: your_pass
6.2 啟動(dòng)應(yīng)用
啟動(dòng)應(yīng)用時(shí)日志打印日志
: master: redis://master_ip:6379 added : slave: redis://slave_ip:6379 added : sentinel: redis://slave_ip:26379 added : sentinel: redis://master_ip:26379 added
到此這篇關(guān)于redis主從+哨兵搭建的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)redis主從+哨兵搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Redis?bigkeys命令會(huì)阻塞問題的解決
這篇文章主要介紹了關(guān)于Redis?bigkeys命令會(huì)阻塞問題的解決,今天分享一次Redis引發(fā)的線上事故,避免再次踩雷,實(shí)現(xiàn)快速入門,需要的朋友可以參考下2023-03-03
springboot整合使用云服務(wù)器上的Redis方法
這篇文章主要介紹了springboot整合使用云服務(wù)器上的Redis,整合步驟通過導(dǎo)入依賴,配置yml文件,注入redisTemplate結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),文中給大家分享了可能遇到的坑,感興趣的朋友跟隨小編一起看看吧2022-09-09
Redis高階使用消息隊(duì)列分布式鎖排行榜等(高階用法)
在大多數(shù)傳統(tǒng)的web系統(tǒng)中,使用Redis一般都是作為緩存使用,在大數(shù)據(jù)查詢時(shí)作為緩解性能的一種解決方案,這篇文章主要介紹了Redis高階使用消息隊(duì)列分布式鎖排行榜等,需要的朋友可以參考下2024-03-03
設(shè)置Redis最大占用內(nèi)存的實(shí)現(xiàn)
本文主要介紹了設(shè)置Redis最大占用內(nèi)存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

