目錄html
本篇文章專一於講解redis在windows環境下使用Redis Sentinel(哨兵)實現高可用方案。git
想要在windows環境下實現redis高可用,先要準備如下材料或知識點。github
下載的壓縮文件內容以下圖所示。
redis
具體結構以下圖所示
c#
應用層鏈接redis集羣,集羣包括3個redis服務(1主2從)和3個哨兵。windows
哨兵用於監控主redis可用性。若長時間不可用,則哨兵進行投票選舉出新的主redis。原來的主redis降級爲從redis。哨兵會將全部redis的配置進行自動更新。bash
將redis複製一份。一個文件夾用於部署redis服務,一個文件夾用於部署哨兵。
app
規定redis服務端口爲28380~28382。將redis.windiws-service.config更名爲redis-28380.conf。打開進行修改。ide
bind 127.0.0.1
,默認爲監聽全部ip。protected-mode yes
爲protected-mode no
,默認redis開啓了保護模式,只容許本機經過127.0.0.1訪問,其餘ip沒法訪問。port 6379
爲port 28380
loglevel debug
爲loglevel notice
logfile "server_log.txt"
爲logfile "server_log_28380.txt"
syslog-ident redis
爲syslog-ident "redis-28380"
修改不保存rdb文件
註釋掉一下三行,因爲rdb會每次全量寫文件,當數據量較大時會對redis穩定性形成必定影響,尤爲是rbd保存失敗時會必定時間內拒絕寫入數據。性能
save 900 1 save 300 10 save 60 10000
建立密碼
修改# requirepass foobared
爲requirepass "test1"
新增masterauth "test1"
若要設置密碼,單臺redis設置
requirepass
便可。集羣redis必須同時設置requirepass
和masterauth
。若主redis沒有設置masterauth
當它變爲從時,就認爲主無密碼。若從redis沒有設置requirepass
,當它變爲主時,會沒有密碼。以上兩種狀況自動切換都沒法自動鏈接到新的主。
啓用aof保存
修改appendonly no
爲appendonly yes
修改appendfilename "appendonly.aof"
爲appendfilename "appendonly_28380.aof"
aof寫入的是redis命令,每次向文件後面添加,所以對IO性能影響較小。最壞狀況主redis丟失1s的數據。
配置改好後保存,這個看成主redis的配置。將改配置複製2份,分別更名爲redis-28381.conf和redis-28382.conf
port 28380
爲port 28381
logfile "server_log-28380.txt"
爲logfile "server_log_28381.txt"
syslog-ident "redis-28380"
爲syslog-ident "redis-28381"
修改從庫對應的主庫ip
修改# slaveof <masterip> <masterport>
爲slaveof 127.0.0.1 28380
23880作爲主庫,填寫的就是28380的端口
修改從庫競選主庫優先級
修改slave-priority 100
爲slave-priority 99
將每一個從庫設置爲不通的優先級,數字較低的優先被競選爲主庫。
修改aof保存的文件名
修改appendfilename "appendonly_28380.aof"
爲appendfilename "appendonly_28381.aof"
port 28380
爲port 28382
logfile "server_log-28380.txt"
爲logfile "server_log_28382.txt"
syslog-ident "redis-28380"
爲syslog-ident "redis-28382"
修改從庫對應的主庫ip
修改# slaveof <masterip> <masterport>
爲slaveof 127.0.0.1 28380
23880作爲主庫,填寫的就是28380的端口
修改從庫競選主庫優先級
修改slave-priority 100
爲slave-priority 98
將每一個從庫設置爲不通的優先級,數字較低的優先被競選爲主庫。
修改aof保存的文件名
修改appendfilename "appendonly_28380.aof"
爲appendfilename "appendonly_28382.aof"
經過命令redis-server --service-install 配置名 --service-name 服務名
將redis安裝爲服務。
先cd到redis所在目錄,在cmd命令行中輸入一下三條語句將三個redis安裝成功爲windows服務。
redis-server --service-install redis-28380.conf --service-name redis-28380 redis-server --service-install redis-28381.conf --service-name redis-28381 redis-server --service-install redis-28382.conf --service-name redis-28382
設置服務別名是爲了在服務中能更好的看到哪一個是哪一個服務,不然默認都是redis服務。
能夠直接在服務中右鍵啓動或者經過命令redis-server --service-start --service-name 服務名
啓動指定服務。
經過redis-cli 鏈接到各個redis服務。
F:\Study\redis> redis-cli -p 28380 -a test1 127.0.0.1:28380> info Replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=28382,state=online,offset=183,lag=1 slave1:ip=127.0.0.1,port=28381,state=online,offset=183,lag=1 master_repl_offset:183 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:182
能夠看到咱們的主從配置已經生效了。
鏈接到從庫28381
F:\Study\redis> redis-cli -p 28381 -a test1 127.0.0.1:28381> info Replication # Replication role:slave master_host:127.0.0.1 master_port:28380 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:323 slave_priority:99 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
鏈接到從庫28382
F:\Study\redis> redis-cli -p 28382 -a test1 127.0.0.1:28382> info Replication # Replication role:slave master_host:127.0.0.1 master_port:28380 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:379 slave_priority:99 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
如今咱們往主庫寫入數據會自動並主動同步到從庫。
配置中默認配了如下配置了
slave-read-only yes
,從庫是隻讀的,不容許寫入。
設置三個哨兵端口爲 24381~24383
配置哨兵24381
dir ./ logfile "sentinel.28481.txt" bind 127.0.0.1 port 28481 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1
sentinel monitor master 127.0.0.1 28380 2
設置主redis的別名爲master,後面都經過master表示主redis。後面的2表示2個哨兵檢測到主redis掛了即爲掛了,須要從新選舉出新的主redis,而原來的主redis降級爲從redis。sentinel down-after-milliseconds master 5000
哨兵多久連不上主redis即認爲它掛了sentinel failover-timeout master 30000
投票選舉超時時間,超過期間沒有選出則該輪投票失敗。
sentinel auth-pass master test1
主redis的密碼 。
因爲哨兵只能設置主的密碼,所以主從的密碼應該設置爲同樣的。
因爲我在本地測試直接使用127.0.0.1,正常狀況下redis和哨兵部署集羣確定是在不通機子上。哨兵則必須綁定ip,不然哨兵自動切換主從。經過在哨兵的配置中加入bind XXXX
。
配置哨兵24382
dir ./ logfile "sentinel.28482.txt" bind 127.0.0.1 port 28482 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1
配置哨兵24383
dir ./ logfile "sentinel.28483.txt" bind 127.0.0.1 port 28483 sentinel monitor master 127.0.0.1 28380 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 sentinel auth-pass 127.0.0.1 28380 test1
經過命令redis-server.exe 配置 --sentinel
啓動哨兵
redis-server redis-sentinel-28481.conf --sentinel redis-server redis-sentinel-28482.conf --sentinel redis-server redis-sentinel-28483.conf --sentinel
啓動哨兵28481後查看日誌
[8464] 21 Nov 02:02:05.258 # +tilt #tilt mode entered [8464] 21 Nov 08:33:01.513 # +tilt #tilt mode entered [8464] 21 Nov 08:33:31.561 # -tilt #tilt mode exited _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 28481 | `-._ `._ / _.-' | PID: 22332 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [22332] 05 Jan 12:01:48.399 # Sentinel ID is 48693b2911456ef3da6ebfd14d1adf46e76fbb8b [22332] 05 Jan 12:01:48.399 # +monitor master master 127.0.0.1 28382 quorum 2 [22332] 05 Jan 12:01:53.399 # +sdown sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28382 [22332] 05 Jan 12:01:53.399 # +sdown sentinel 162c66f370550a9926b794abecfb431cf3f8bcc9 127.0.0.1 28483 @ master 127.0.0.1 28382 [22332] 05 Jan 12:02:13.404 # +sdown master master 127.0.0.1 28382
同時啓動完會自動在哨兵的配置文件中生成一些配置,完整的redis-sentinel-28481.conf配置以下
dir "F:\\Study\\redis\\redis-sentinel" logfile "sentinel.28481.txt" port 28481 sentinel myid 48693b2911456ef3da6ebfd14d1adf46e76fbb8b sentinel monitor master 127.0.0.1 28381 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 30000 # Generated by CONFIG REWRITE sentinel auth-pass master test1 sentinel config-epoch master 11 sentinel leader-epoch master 11 sentinel known-slave master 127.0.0.1 28382 sentinel known-slave master 127.0.0.1 28380 sentinel known-sentinel master 127.0.0.1 28482 ce50397a76e2e3ca165ce407859cbc94d7caf504 sentinel known-sentinel master 127.0.0.1 28483 162c66f370550a9926b794abecfb431cf3f8bcc9 sentinel current-epoch 11
啓動後會爲哨兵生成一個id,同時會記錄一些關鍵信息。
將主redis關閉,讓哨兵自動切換主從。
哨兵24381日誌
[22332] 05 Jan 12:34:49.286 # +sdown master master 127.0.0.1 28380 [22332] 05 Jan 12:34:49.295 # +new-epoch 16 [22332] 05 Jan 12:34:49.296 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16 [22332] 05 Jan 12:34:49.363 # +odown master master 127.0.0.1 28380 #quorum 3/2 [22332] 05 Jan 12:34:49.363 # Next failover delay: I will not start a failover before Sat Jan 05 12:35:49 2019 [22332] 05 Jan 12:34:50.397 # +config-update-from sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28380 [22332] 05 Jan 12:34:50.397 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382 [22332] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382 [22332] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382 [22332] 05 Jan 12:34:55.442 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
哨兵24382日誌
[8760] 05 Jan 12:34:49.237 # +sdown master master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.292 # +odown master master 127.0.0.1 28380 #quorum 2/2 [8760] 05 Jan 12:34:49.292 # +new-epoch 16 [8760] 05 Jan 12:34:49.292 # +try-failover master master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.293 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16 [8760] 05 Jan 12:34:49.296 # 162c66f370550a9926b794abecfb431cf3f8bcc9 voted for ce50397a76e2e3ca165ce407859cbc94d7caf504 16 [8760] 05 Jan 12:34:49.296 # 48693b2911456ef3da6ebfd14d1adf46e76fbb8b voted for ce50397a76e2e3ca165ce407859cbc94d7caf504 16 [8760] 05 Jan 12:34:49.394 # +elected-leader master master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.394 # +failover-state-select-slave master master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.470 # +selected-slave slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.470 * +failover-state-send-slaveof-noone slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:49.541 * +failover-state-wait-promotion slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:50.341 # +promoted-slave slave 127.0.0.1:28382 127.0.0.1 28382 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:50.341 # +failover-state-reconf-slaves master master 127.0.0.1 28380 [8760] 05 Jan 12:34:50.396 * +slave-reconf-sent slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:51.380 * +slave-reconf-inprog slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:51.446 # -odown master master 127.0.0.1 28380 [8760] 05 Jan 12:34:52.393 * +slave-reconf-done slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28380 [8760] 05 Jan 12:34:52.468 # +failover-end master master 127.0.0.1 28380 [8760] 05 Jan 12:34:52.468 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382 [8760] 05 Jan 12:34:52.469 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382 [8760] 05 Jan 12:34:52.469 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382 [8760] 05 Jan 12:34:57.562 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
哨兵24383日誌
[26484] 05 Jan 12:34:49.161 # +sdown master master 127.0.0.1 28380 [26484] 05 Jan 12:34:49.294 # +new-epoch 16 [26484] 05 Jan 12:34:49.295 # +vote-for-leader ce50397a76e2e3ca165ce407859cbc94d7caf504 16 [26484] 05 Jan 12:34:50.284 # +odown master master 127.0.0.1 28380 #quorum 3/2 [26484] 05 Jan 12:34:50.284 # Next failover delay: I will not start a failover before Sat Jan 05 12:35:49 2019 [26484] 05 Jan 12:34:50.397 # +config-update-from sentinel ce50397a76e2e3ca165ce407859cbc94d7caf504 127.0.0.1 28482 @ master 127.0.0.1 28380 [26484] 05 Jan 12:34:50.397 # +switch-master master 127.0.0.1 28380 127.0.0.1 28382 [26484] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28381 127.0.0.1 28381 @ master 127.0.0.1 28382 [26484] 05 Jan 12:34:50.397 * +slave slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382 [26484] 05 Jan 12:34:55.437 # +sdown slave 127.0.0.1:28380 127.0.0.1 28380 @ master 127.0.0.1 28382
具體投票過程這裏不作具體分析。
當選出新的主redis,哨兵會對redis配置進行更新,將主redis的slaveof XXX XXX
配置刪除,將從的slaveof XXX XXX
設置爲主的地址。同時哨兵會監控redis28380。當28380恢復後會將slaveof XXX XXX
添加到它的配置中。
鏈接到28382查看
127.0.0.1:28382> info Replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=28381,state=online,offset=193307,lag=1 slave1:ip=127.0.0.1,port=28380,state=online,offset=193441,lag=1 master_repl_offset:193575 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:193574
若咱們此時動態新增一個從redis,端口爲28383,則複製一個28380的配置進行修改,將優先級改成97。並啓動服務便可。
redis-28383日誌以下
[14048] 05 Jan 12:55:20.421 * Redis 3.2.100 (00000000/0) 64 bit, standalone mode, port 28383, pid 14048 ready to start. [14048] 05 Jan 12:55:20.422 # Server started, Redis version 3.2.100 [14048] 05 Jan 12:55:20.423 * The server is now ready to accept connections on port 28383 [14048] 05 Jan 12:55:20.424 * Connecting to MASTER 127.0.0.1:28382 [14048] 05 Jan 12:55:20.426 * MASTER <-> SLAVE sync started [14048] 05 Jan 12:55:20.427 * Non blocking connect for SYNC fired the event. [14048] 05 Jan 12:55:20.428 * Master replied to PING, replication can continue... [14048] 05 Jan 12:55:20.428 * Partial resynchronization not possible (no cached master) [14048] 05 Jan 12:55:20.432 * Full resync from master: 2c344529e4acdc44cd311818fe8179825877a9f4:241347 [14048] 05 Jan 12:55:20.653 * MASTER <-> SLAVE sync: receiving 107 bytes from master [14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Flushing old data [14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Loading DB in memory [14048] 05 Jan 12:55:20.656 * MASTER <-> SLAVE sync: Finished with success [14048] 05 Jan 12:55:20.660 * Background append only file rewriting started by pid 29392 [14048] 05 Jan 12:55:20.827 * AOF rewrite child asks to stop sending diffs. [14048] 05 Jan 12:55:20.927 # fork operation complete [14048] 05 Jan 12:55:20.928 * Background AOF rewrite terminated with success [14048] 05 Jan 12:55:20.929 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB) [14048] 05 Jan 12:55:20.931 * Background AOF rewrite finished successfully
再看下28382的主從鏈接信息
127.0.0.1:28382> info Replication # Replication role:master connected_slaves:3 slave0:ip=127.0.0.1,port=28381,state=online,offset=268634,lag=0 slave1:ip=127.0.0.1,port=28380,state=online,offset=268768,lag=0 slave2:ip=127.0.0.1,port=28383,state=online,offset=268634,lag=0 master_repl_offset:268768 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:268767
哨兵也能檢測到新的從庫鏈接
[22332] 05 Jan 12:55:25.420 * +slave slave 127.0.0.1:28383 127.0.0.1 28383 @ master 127.0.0.1 28382
經過上述配置,就完成了redis高可用方案。
我使用的是StackExchange.Redis
鏈接redis。
它自己就支持主從鏈接,在創建鏈接的時候輸入多個鏈接地址接口。因爲從庫不容許寫入。所以它能辨別出哪一個是主哪一個是從。主從切換後寫入數據的時候從新斷定哪一個是主庫。
ConfigurationOptions options = ConfigurationOptions.Parse("password=test1"); options.EndPoints.Add("127.0.0.1", 28380); options.EndPoints.Add("127.0.0.1", 28381); options.EndPoints.Add("127.0.0.1", 28382);
ConfigurationOptions options = ConfigurationOptions.Parse("127.0.0.1:28380,127.0.0.1:28381,127.0.0.1:28382,keepAlive=5,password=test1");
經過該篇文章詳細的探究了window下的redis高可用方案如何實現。本文對具體配置沒有作深刻探究,僅僅爲了抓住重點,具體配置其餘的文檔都介紹的比較詳細,可是一些細節並無說明,經過該片文章將reids高可用的坑都填滿。如有錯誤,歡迎指正。
實例能夠從這裏下載
本文地址:http://www.javashuo.com/article/p-vnknmswd-kr.html 做者博客:傑哥很忙 歡迎轉載,請在明顯位置給出出處及連接