1. 程式人生 > 實用技巧 >windows 2012 R2 系統下, redis 主從配置

windows 2012 R2 系統下, redis 主從配置

環境

system: Windows 2012 R2

Redis: 5.0.10 64bit

原來只有一個節點進行讀寫,隨著時間推移,時不時出現超時現象,於是改為1主(寫)2從(讀)方案。專案背景可以使 用 redis 只做快取使用, 不需要持久化。

1,安裝redis 後,再複製出兩個資料夾。

埠:

redis: 6379
redis_slave1: 6380
redis_slave2: 6381

2,分別開啟 Redis_Slave1, Redis_Slave2 下的配置檔案, redis.windows.conf, 在“Replication" 部位新增配置:

slaveof 127.0.0.1 6379
################################# REPLICATION #################################

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 
2) Redis slaves are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. #
3) Replication is automatic and does not need user intervention. After a # network partition slaves automatically try to reconnect to masters # and resynchronize with them. # # slaveof <masterip> <masterport> slaveof 127.0.0.1 6379

3,註冊為windows service

分別到Redis_Slave1, Redis_Slave2 資料夾下執行命令

# 資料夾 Redis_Slave1
redis-server --service-install redis.windows.conf --service-name Redis6380
# 資料夾 Redis_Slave2
redis-server --service-install redis.windows.conf --service-name Redis6381

在win服務裡檢視執行結果:

4,驗證安裝

通過redis-cli 命令分別登入服務,並測試一個key,