注意:Windows3.2.100版本和Linux5.0.4版本同步機制不一樣無法做同步。Windows的版本明顯跟不上Linux版本更新的速度,如果從庫是Windows版本,Linux需要對應使用相近的版本。
目錄
Redis需求說明
需求
現狀
目标
Redis主從安裝配置
Redis環境說明
Redis主從同步原理
Redis查詢指令說明
Redis安裝包下載下傳
Redis master節點安裝
windows安裝Redis
Linux安裝Redis
Redis slave 節點安裝
Windows安裝Redis
Redis伺服器要求
阿裡雲Linux伺服器
記憶體擴容
端口開放
阿裡雲windows伺服器
記憶體擴容
端口開放
B前置機Windows伺服器
端口開放
參考資料
Redis需求說明
需求
B内網部署A系統:要求能夠從内網A環境檢視A正式環境系統護林員實時位置和實時軌迹功能。由于A正式環境和B内網之間不能直接進行資料互動,需要前置機作為資料中轉以實作B内網A系統的實時軌迹和實時位置檢視功能。
現狀
目前A正式環境尚未啟用Redis緩存版本,所有記憶體資料存儲暫時都采用記憶體緩存(雖然A系統以實作記憶體與Redis的切換但尚未系統地對Redis進行測試,故暫未使用Redis緩存方案)。
目标
因為B使用者的特殊性,需要對其實時位置采集資料進行過濾處理,通過Redis主從同步機制将實時位置和實時軌迹資料同步到前置機Redis,Redis從庫預設是隻讀不允許寫入以保證資料一緻性。
注意區分A本身的Redis功能和B伺服器Redis功能無關聯關系。
Redis主從安裝配置
Redis環境說明
由于前置機是windows Server R2 2008 版本,是以我們的版本也采用Windows Server系列的伺服器版本作為Redis主機,需要版本保持基本一緻(Linux最新版本5.0.4而Windows版本停滞于在3.2.100)。阿裡雲Windows伺服器需要擴大記憶體,建議擴大到跟目标機器一緻的記憶體大小32G。
Redis-master——阿裡雲(192.168.1.99) Windows Server 2012
Redis-slave——B前置機(192.168.1.100) Windows Server 2008
Redis主從同步原理
主從同步流程圖:
1、Slave服務啟動,主動連接配接Master,并發送SYNC指令,請求初始化同步;
2、Master收到SYNC後,執行BGSAVE指令生成RDB檔案,并緩存該時間段内的寫指令;
3、Master完成RDB檔案後,将其發送給所有Slave伺服器;
4、Slave伺服器接收到RDB檔案後,删除記憶體中舊的緩存資料,并裝載RDB檔案;
5、Master在發送完RDB後,即刻向所有Slave伺服器發送緩存中的寫指令;
至此初始化完成,後續進行增量同步。
Redis查詢指令說明
# 查詢所有key
keys *
#清空所有key
flushall
#認證auth password
auth [email protected]
Redis安裝包下載下傳
Windows: https://github.com/MicrosoftArchive/redis/releases
Linux版本:https://redis.io/download
Redis master節點安裝
windows安裝Redis
- 解壓下載下傳的Redis-x64-3.2.100.zip到Redis-x64-3.2.100檔案夾下,并更名為Redis-x64-3.2.100-master。
注:dump.rdb和redis.log是redis啟動後生成的資料備份和日志檔案。
- 修改redis.windows.conf,核對修改如下選項
#通路端口
port 6379
# 開啟遠端通路bind 127.0.0.1改為bind 0.0.0.0
bind 0.0.0.0
# 開啟日志功能,預設 logfile ""
logfile "redis.log"
# 設定通路密碼,預設沒有密碼
requirepass [email protected]
- 啟動Redis主節點
C:\Users\admin>cd C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-master
C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-master>redis-server.exe redis.windows.conf
注:運作時跟上配置檔案,不跟配置檔案為預設配置。
- Redis指令行操作
C:\Users\admin>cd C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-master
C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-master>redis-cli.exe -h 192.168.1.99 -p 6379
192.168.1.99:6379> keys *
(error) NOAUTH Authentication required.
192.168.1.99:6379> set user boonya
(error) NOAUTH Authentication required.
192.168.1.99:6379> auth [email protected]
OK
192.168.1.99:6379> set user boonya
OK
192.168.1.99:6379> get user
"boonya"
192.168.1.99:6379>
注:由于前面我們設定了通路密碼,必須認證後才能操作;操作阿裡雲環境對應修改IP。
Linux安裝Redis
- 下載下傳與Windows目标接近的Redis版本并編譯
$ wget http://download.redis.io/releases/redis-3.2.1.tar.gz
$ tar xzf redis-3.2.1.tar.gz
$ cd redis-3.2.1
$ make
- 修改redis.conf檔案
#通路端口
port 6379
# 開啟遠端通路bind 127.0.0.1改為bind 0.0.0.0
bind 0.0.0.0
# 開啟日志功能,預設 logfile ""
logfile "redis.log"
# 設定通路密碼,預設沒有密碼
requirepass [email protected]
#pid 檔案修改--如果存在多個redis執行個體請修改pid檔案名
pidfile /var/run/redis_6379.pid
注意:如果存在多個redis執行個體請修改pidfile 檔案名。
- 啟動Redis服務
$ cd redis-3.2.1
$ ./src/redis-server ./redis.conf
- 使用redis指令行測試
[[email protected] redis-3.2.1]# ./src/redis-cli -h 192.168.1.98 -p 6379
192.168.1.98:6379> auth [email protected]
OK
192.168.1.98:6379> keys *
1) "user"
192.168.1.98:6379>
注意:Redis版本下載下傳保持一緻(Linux下沒有對應的3.2.100故下載下傳3.2.1),若出現同步失敗導緻如下錯誤,請確定6379端口可通路。
Sending command to master in replication handshake: -Writing to master: Unknown error
3.2.1啟動後警告資訊,對應設定即可:
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
警告overcommit_memory設定為0! 在低記憶體條件下,背景儲存可能會失敗。 要解決此問題,請将“vm.overcommit_memory = 1”添加到/etc/sysctl.conf,然後重新啟動或運作指令“sysctl vm.overcommit_memory = 1”以使其生效。
Redis slave 節點安裝
前置機是windows故隻提供windows的安裝,Linux版本類似。
Windows安裝Redis
安裝步驟如下:
- 解壓下載下傳的Redis-x64-3.2.100.zip到Redis-x64-3.2.100檔案夾下,并更名為Redis-x64-3.2.100-slave。
注:dump.rdb和redis.log是redis啟動後生成的資料備份和日志檔案。
- 修改redis.windows.conf,核對修改如下選項
# 通路端口
port 6379
# 預設綁定bind 127.0.0.1
bind 192.168.1.100
# 開啟日志功能,預設 logfile ""
logfile "redis.log"
# 設定通路密碼,預設沒有密碼
requirepass [email protected]
# 設定從節點
slaveof 127.0.0.1 6379
# 設定從節點通路主節點的認證憑據
masterauth [email protected]
注:bind的位址改為前置機IP位址,不在同一台機器上端口仍然可以使用6379。
- 啟動Redis主節點
C:\Users\admin>cd C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-slave
C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-slave>redis-server.exe redis.windows.conf
注:運作時跟上配置檔案,不跟配置檔案為預設配置。
- Redis指令行操作
C:\Users\admin>cd C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-slave
C:\Users\admin\Desktop\Redis\Redis-x64-3.2.100-slave>redis-cli.exe -h 192.168.1.100 -p 6379
192.168.1.100:6379> keys *
(error) NOAUTH Authentication required.
192.168.1.100:6379> auth [email protected]
OK
192.168.1.100:6379> get user
"boonya"
192.168.1.100:6379>
注:由于前面我們設定了通路密碼,必須認證後才能操作;操作前置機環境對應修改IP。從上面看到我們在主庫中設定的user已經在從庫中查詢到了。
Redis伺服器要求
阿裡雲Linux伺服器
記憶體擴容
阿裡雲Linux伺服器目前部署了A背景管理系統、移動端、資料同步等服務,本身具有16G記憶體,可用不到7G,建議擴大到跟目标機器一緻的記憶體大小32G。
端口開放
根據Redis同步原理,Master會向Slave發送RDB指令,是以需要開啟遠端通路6379端口。
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
阿裡雲windows伺服器
記憶體擴容
阿裡雲Windows伺服器需要擴大記憶體,目前為8G,建議擴大到跟目标機器一緻的記憶體大小32G。
Redis-master——阿裡雲(192.168.1.99) Windows Server 2012
端口開放
根據Redis同步原理,Master會向Slave發送RDB指令,是以需要開啟遠端通路6379端口。需要公司阿裡雲平台賬号管理者開放此端口(本地入站出站端口已配置仍不通)。
參考解決:https://blog.csdn.net/qihaoocheng/article/details/80137073
B前置機Windows伺服器
端口開放
根據Redis同步原理,Master會向Slave發送RDB指令,是以需要開啟遠端通路6379端口。B伺服器需要在阿裡雲平台上開放此端口(本地入站出站端口已配置仍不通)。
參考解決:https://blog.csdn.net/qihaoocheng/article/details/80137073
參考資料
redis.conf配置詳細解析
Redis核心解讀-從Master到Slave的Replicantion
Redis主從之Windows主Linux從
Redis主從無法同步的坑
Redis 主從配置(Windows版)
下載下傳word:https://download.csdn.net/download/boonya/11169128