day67_Redis學習筆記_03

五、keys命令(瞭解)

5.一、經常使用命令(自學)

  • (1)keys
返回知足給定pattern的全部key
示例:
    127.0.0.1:6379keys *
    1"zset1"
    2"list:1"
    3"s1"
    4"s2"
    5"newlist"
    6"s3"
    7"set2"
    8"set1"
  • (2)exists
確認一個key是否存在
    從示例結果來看,數據庫中不存在HongWan這個key,可是age這個key是存在的
示例:
    127.0.0.1:6379exists set8
    (integer) 0
    127.0.0.1:6379exists set1
    (integer) 1
  • (3)del
刪除一個key
示例:
    127.0.0.1:6379del s3
    (integer) 1
    127.0.0.1:6379exists set3
    (integer) 0
    127.0.0.1:6379
  • (4)rename
重命名key
    從示例結果來看,s1成功的被咱們更名爲s1_new了。
示例:
    127.0.0.1:6379keys *
    1"zset1"
    2"list:1"
    3"s1"
    4"s2"
    5"newlist"
    6"set2"
    7"set1"
    127.0.0.1:6379rename s1 s1_new
    OK
    127.0.0.1:6379keys *
    1"zset1"
    2"list:1"
    3"s2"
    4"newlist"
    5"set2"
    6"s1_new"
    7"set1"
  • (5)type
返回值的類型
    從示例結果來看,這個方法能夠很是簡單的判斷出值的類型。
示例:
    127.0.0.1:6379keys *
    1) "zset1"
    2) "list:1"
    3) "s2"
    4) "newlist"
    5) "set2"
    6) "s1_new"
    7) "set1"
    127.0.0.1:6379type s2
    string
    127.0.0.1:6379type set1
    set
    127.0.0.1:6379type zset1
    zset
    127.0.0.1:6379type list:1
    list

5.二、設置key的生存時間

  • Redis在實際使用過程當中更多的用做緩存,然而緩存的數據通常都是須要設置生存時間的,即:到期後數據銷燬。
EXPIRE key seconds          設置key的生存時間(單位:秒)key在多少秒後會自動刪除
TTL key                     查看key生於的生存時間
PERSIST key                 清除生存時間 
PEXPIRE key milliseconds    生存時間設置單位爲:毫秒

示例:
    127.0.0.1:6379keys *
    1) "zset1"
    2) "list:1"
    3) "s2"
    4) "newlist"
    5) "set2"
    6) "s1_new"
    7) "set1"
    127.0.0.1:6379expire s2 10        設置s2的生存時間爲10秒
    (integer) 1
    127.0.0.1:6379ttl s2              查看s2的生於生成時間還有4秒刪除
    (integer) 4
    127.0.0.1:6379ttl s2
    (integer-2
    127.0.0.1:6379get s2              獲取s2的值,已經刪除
    (nil)

六、Redis的持久化方案

6.一、RDB持久化(默認)

  RDB方式的持久化是經過快照(snapshotting)完成的,當符合必定條件時Redis會自動將內存中的數據進行快照並持久化到硬盤
  RDB是Redis默認採用的持久化方式。javascript

6.1.一、設置持久化快照的條件

在redis.conf中修改持久化快照的條件,以下圖:css


詳解以下:
save 開頭的一行就是持久化配置,能夠配置多個條件(每行配置一個條件),每一個條件之間是「或」的關係。
    save 900 1      表示15分鐘(900秒)內至少1個鍵被更改則進行快照。
    save 300 10     表示5分鐘(300秒)內至少10個鍵被更改則進行快照。
    save 60 10000   表示1分鐘(60秒)內至少10000個鍵被更改則進行快照。

6.1.二、設置快照文件(持久化文件)存儲的名稱和目錄

在redis.conf中能夠指定持久化文件存儲的目錄,以下圖:java


詳解以下:
dbfilename dump.rdb     表示設置而快照文件(持久化文件)存儲的名稱爲:dump.rdb 
dir ./                  表示設置而快照文件(持久化文件)存儲的目錄爲:在當前目錄下

Redis啓動後會讀取RDB快照文件,將數據從硬盤載入到內存。根據數據量大小與結構和服務器性能不一樣,這個時間也不一樣。
一般將記錄一千萬個字符串類型鍵、大小爲1GB的快照文件載入到內存中須要花費2030秒鐘。

6.1.四、RDB問題總結

  經過RDB方式實現持久化,一旦Redis異常退出(非法關閉),就會丟失最後一次快照之後更改的全部數據。這就須要開發者根據具體的應用場合,經過組合設置自動快照條件的方式來將可能發生的數據損失控制在可以接受的範圍。
  若是數據很重要以致於沒法承受任何損失,則能夠考慮使用AOF方式進行持久化。
  即:一旦redis非法關閉,那麼會丟失最後一次持久化以後的數據。
  若是數據不重要,則沒必要要關心。
  若是數據不能容許丟失,那麼要使用AOF方式。node

6.二、AOF持久化

詳解以下:linux

默認狀況下Redis沒有開啓AOF(append only file)方式的持久化。
能夠經過修改redis.conf配置文件中的appendonly參數開啓,以下:
appendonly yes

開啓AOF持久化後每執行一條會更改Redis中的數據的命令,Redis就會將該命令寫入硬盤中的AOF文件。

AOF文件的保存位置和RDB文件的位置相同,都是經過dir參數設置的。
dir ./

默認的文件名是appendonly.aof,能夠經過appendfilename參數修改:
appendfilename appendonly.aof

在同時使用aof和rdb方式時,若是redis重啓,則數據從aof文件加載。

七、Redis的主從複製

7.一、什麼是主從複製

  • 持久化保證了即便redis服務重啓也不會丟失數據,由於redis服務重啓後會將硬盤上持久化的數據恢復到內存中,可是當redis服務器的硬盤損壞了可能會致使數據丟失,若是經過redis的主從複製機制就能夠避免這種單點故障,以下圖所示:
  • 詳解以下:
    • 主redis中的數據有兩個副本(replication)即從redis1和從redis2,即便一臺redis服務器宕機其它兩臺redis服務也能夠繼續提供服務。
    • 主redis中的數據和從redis上的數據保持實時同步,當主redis寫入數據時經過`主從複製機制`會複製到兩個從redis服務上。
    • 只有一個主redis,能夠有多個從redis。
    • `主從複製不會阻塞master`,在同步數據時,master能夠繼續處理client請求。
    • 一個redis能夠便是主又是從,以下圖:

7.二、主從配置

7.2.一、主redis配置

  • 無需特殊配置。

7.2.二、從redis配置

  • 因爲目前咱們只有一臺電腦,須要模擬出兩臺服務器。
  • 第一步:複製出一個從機bin2/
      [root@itheima redis]# cp bin/ bin2 –r
  • 第二步:修改【從redis服務器】上的redis.conf文件

      上邊的配置說明當前該【從redis服務器】所對應的【主redis服務器】的IP是192.168.5.128,端口是6379。
  • 第三步:修改【從redis服務器】上的redis.conf文件,修改port地址爲6380
  • 第四步:清除【從redis服務器】中的持久化文件爲了保證主從機數據一致
      [root@itheima bin2]# rm -rf appendonly.aof dump.rdb
  • 第五步:啓動【從redis服務器】
      [root@itheima bin2]# ./redis-server redis.conf
  • 第六步:啓動【6380的客戶端】
      [root@itheima bin2]# ./redis-cli -p 6380
  • 注意:
      主機一旦發生增刪改操做,那麼從機會將數據同步到從機中。
      從機不能執行寫操做(增刪改操做)。

八、Redis的集羣(重點)

8.一、redis-cluster架構圖


詳解以下:
架構細節:
    (1) 全部的redis節點彼此互聯(`PING-PONG機制`),內部使用`二進制協議`優化傳輸速度和帶寬。
    (2) 節點的fail(失敗)是經過集羣中`超過半數的節點檢測失效`時才生效。即`集羣搭建中主機的個數爲奇數`
    (3) 客戶端與redis節點直連,不須要中間proxy層,客戶端不須要鏈接集羣全部節點,鏈接集羣中任何一個可用節點便可。
    (4) redis-cluster把全部的物理節點映射到[0-16383]slot(槽)上,cluster負責維護node<-->slot<-->value

Redis 集羣中內置了 `16384 個哈希槽`,當須要在 Redis 集羣中放置一個 key-value 時,redis 先對 key 使用 crc16 算法算出一個結果,而後把結果`對 16384 求餘數`,這樣每一個 key 都會對應一個編號在 0-16383 之間的哈希槽,redis 會根據節點數量`大體均等`的將哈希槽映射到不一樣的節點。

示例以下:nginx

8.二、redis-cluster投票:容錯


詳解以下:
(1) 集羣中全部master參與投票,若是半數以上master節點與其中一個master節點通訊超過(cluster-node-timeout),認爲該master節點掛掉。
(2) 何時整個集羣不可用(cluster_state:fail)
    一、若是集羣任意master掛掉,且當前master沒有slave,則集羣進入fail狀態。也能夠理解成集羣的[0-16383]slot映射不徹底時進入fail狀態。
    二、若是集羣超過半數以上master掛掉,不管是否有slave,集羣進入fail狀態。

8.三、搭建ruby環境

  • redis集羣管理工具redis-trib.rb依賴ruby環境,首先須要安裝ruby環境。
  • 第一步:安裝ruby環境
      [root@itheima bin]# yum install ruby
      [root@itheima bin]# yum install rubygems
  • 第二步:使用sftp工具將如下文件上傳到linux系統
      sftp> put -r "E:\學習資料\java\java就業班\1七、第十七階段redis(1天)\redis\res\ruby和redis接口\redis-3.0.0.gem"
  • 第三步:安裝ruby和redis接口
      [root@itheima ~]# gem install redis-3.0.0.gem
  • 第四步:將redis-3.0.0包下src目錄中的redis集羣搭建腳本文件redis-trib.rb拷貝到/user/local/redis/redis-cluster 目錄下(注意:先在/user/local/redis/目錄下建立目錄redis-cluster)
      [root@itheima ~]# cd /root/redis-3.0.0/src/
      [root@itheima src]# ll *rb
      -rwxr-xr-x. 1 root root 48141 11月 3 19:30 redis-trib.rb
      [root@itheima src]# cp redis-trib.rb /usr/local/redis/redis-cluster
  • 第五步:查看是否拷貝成功
      [root@itheima redis-cluster]# ll
      總用量 48
      -rwxr-xr-x. 1 root root 48141 11月 3 19:30 redis-trib.rb

8.四、搭建redis集羣

  • 搭建集羣最少也得須要3臺主機,若是每臺主機再配置一臺從機的話,則最少須要6臺機器。
  • 如今咱們須要在一臺電腦上模擬出6臺電腦。
    • 端口設計以下:7001-7006
  • 第一步:複製出一個7001機器
      [root@itheima redis]# cp bin ./redis-cluster/7001 -r
  • 第二步:若是存在持久化文件,則刪除
      [root@itheima 7001]# rm -rf appendonly.aof dump.rdb
  • 第三步:設置集羣參數,修改redis.conf配置文件,打開cluster-enable yes
  • 第四步:修改端口
  • 第五步:複製出7002-7006機器
      [root@itheima redis-cluster]# cp 7001/ 7002 -r
      [root@itheima redis-cluster]# cp 7001/ 7003 -r
      [root@itheima redis-cluster]# cp 7001/ 7004 -r
      [root@itheima redis-cluster]# cp 7001/ 7005 -r
      [root@itheima redis-cluster]# cp 7001/ 7006 -r
  • 第六步:依次修改7002-7006機器的端口
  • 第七步:啓動7001-7006這六臺機器,咱們本身寫一個腳本文件start-all.sh批量啓動它們:
      [root@itheima redis-cluster]# vim start-all.sh

    補上1:咱們本身寫一個腳本文件shutdown-all.sh批量關閉它們:
      [root@itheima redis-cluster]# vim shutdown-all.sh

    補上2:咱們本身寫一個腳本文件delete-aof-rdb-nodes.sh批量刪除持久化文件和節點配置文件:
      [root@itheima redis-cluster]# vim delete-aof-rdb-nodes.sh

    注意:自定義的腳本文件須要授予寫權限,方可以使用!!!
  • 第八步:修改start-all.sh文件的權限
      [root@itheima redis-cluster]# chmod u+x start-all.sh
  • 第九步:執行腳本文件start-all.sh,批量啓動它們
      [root@itheima redis-cluster]# ./start-all.sh
  • 第十步:查看啓動狀態
      [root@itheima redis-cluster]# ps -aux | grep redis
  • 第十一步:建立集羣,注意:建立集羣以前,先搭建ruby環境,如上:8.四、搭建ruby環境
[root@itheima redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.5.128:7001 192.168.5.128:7002 192.168.5.128:7003 192.168.5.128:7004 192.168.5.128:7005 192.168.5.128:7006
>>> Creating cluster
Connecting to node 192.168.5.128:7001: OK
Connecting to node 192.168.5.128:7002: OK
Connecting to node 192.168.5.128:7003: OK
Connecting to node 192.168.5.128:7004: OK
Connecting to node 192.168.5.128:7005: OK
Connecting to node 192.168.5.128:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.5.128:7001
192.168.5.128:7002
192.168.5.128:7003
Adding replica 192.168.5.128:7004 to 192.168.5.128:7001
Adding replica 192.168.5.128:7005 to 192.168.5.128:7002
Adding replica 192.168.5.128:7006 to 192.168.5.128:7003
M: 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 192.168.5.128:7001
   slots:0-5460 (5461 slots) master
M: 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 192.168.5.128:7002
   slots:5461-10922 (5462 slots) master
M: d7d28caadfdc4161261305f2d2baf55d2d8f4221 192.168.5.128:7003
   slots:10923-16383 (5461 slots) master
S: f124b72c0421c7514f44668d30761d075e42643d 192.168.5.128:7004
   replicates 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7
S: 8cf60c085a58b60557a887a5e8451ce38e6b54fa 192.168.5.128:7005
   replicates 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d
S: 05ad0eb9b5f839771b09dc18192909d5fa1f893e 192.168.5.128:7006
   replicates d7d28caadfdc4161261305f2d2baf55d2d8f4221
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 192.168.5.128:7001)
M: 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 192.168.5.128:7001
   slots:0-5460 (5461 slots) master
M: 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 192.168.5.128:7002
   slots:5461-10922 (5462 slots) master
M: d7d28caadfdc4161261305f2d2baf55d2d8f4221 192.168.5.128:7003
   slots:10923-16383 (5461 slots) master
M: f124b72c0421c7514f44668d30761d075e42643d 192.168.5.128:7004
   slots: (0 slots) master
   replicates 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7
M: 8cf60c085a58b60557a887a5e8451ce38e6b54fa 192.168.5.128:7005
   slots: (0 slots) master
   replicates 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d
M: 05ad0eb9b5f839771b09dc18192909d5fa1f893e 192.168.5.128:7006
   slots: (0 slots) master
   replicates d7d28caadfdc4161261305f2d2baf55d2d8f4221
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@itheima redis-cluster]

8.五、鏈接集羣

  [root@itheima 7001]# ./redis-cli -h 192.168.5.128 -p 7001 –c
  -c:指定是集羣鏈接redis

8.六、查看集羣信息的命令

  • 查看集羣信息
192.168.5.128:7002cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_sent:1837
cluster_stats_messages_received:1837
192.168.5.128:7002>
  • 查看集羣中的節點
192.168.5.128:7002cluster nodes
d7d28caadfdc4161261305f2d2baf55d2d8f4221 192.168.5.128:7003 master - 0 1541263366376 3 connected 10923-16383
f124b72c0421c7514f44668d30761d075e42643d 192.168.5.128:7004 slave 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 0 1541263372425 4 connected
05ad0eb9b5f839771b09dc18192909d5fa1f893e 192.168.5.128:7006 slave d7d28caadfdc4161261305f2d2baf55d2d8f4221 0 1541263371417 6 connected
3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 192.168.5.128:7001 master - 0 1541263370402 1 connected 0-5460
8cf60c085a58b60557a887a5e8451ce38e6b54fa 192.168.5.128:7005 slave 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 0 1541263369396 5 connected
27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 192.168.5.128:7002 myself,master - 0 0 2 connected 5461-10922
192.168.5.128:7002

8.七、維護集羣節點(自學)

8.7.一、添加主節點

  • 集羣建立成功後能夠向集羣中添加節點,下面演示是添加一個master主節點。
  • 準備工做:1) 先複製出一個7007的機器;2) 若是存在持久化文件和節點配置文件,就刪除掉;3) 修改機器7007的端口;4) 啓動機器7007
    [root@itheima redis-cluster]# cp 7001/ 7007 -r
    [root@itheima redis-cluster]# cd 7007/
    [root@itheima 7007]# rm -rf appendonly.aof dump.rdb nodes.conf
    [root@itheima redis-cluster]# cd 7007/
    [root@itheima 7007]# vim redis.conf
    [root@itheima 7007]# ./redis-server redis.conf
  • 添加7007結點做爲新主節點
      命令:[root@itheima redis-cluster]# ./redis-trib.rb add-node 192.168.5.128:7007 192.168.5.128:7001
  • 示例以下:
[root@itheima redis-cluster]# ./redis-trib.rb add-node 192.168.5.128:7007 192.168.5.128:7001
>>> Adding node 192.168.5.128:7007 to cluster 192.168.5.128:7001
Connecting to node 192.168.5.128:7001: OK
Connecting to node 192.168.5.128:7006: OK
Connecting to node 192.168.5.128:7004: OK
Connecting to node 192.168.5.128:7002: OK
Connecting to node 192.168.5.128:7005: OK
Connecting to node 192.168.5.128:7003: OK
>>> Performing Cluster Check (using node 192.168.5.128:7001)
M: 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 192.168.5.128:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 05ad0eb9b5f839771b09dc18192909d5fa1f893e 192.168.5.128:7006
   slots: (0 slots) slave
   replicates d7d28caadfdc4161261305f2d2baf55d2d8f4221
S: f124b72c0421c7514f44668d30761d075e42643d 192.168.5.128:7004
   slots: (0 slots) slave
   replicates 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7
M: 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 192.168.5.128:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 8cf60c085a58b60557a887a5e8451ce38e6b54fa 192.168.5.128:7005
   slots: (0 slots) slave
   replicates 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d
M: d7d28caadfdc4161261305f2d2baf55d2d8f4221 192.168.5.128:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.5.128:7007: OK
>>> Send CLUSTER MEET to node 192.168.5.128:7007 to make it join the cluster.
[OK] New node added correctly.
[root@itheima redis-cluster]
  • 查看集羣結點,發現主節點7007已添加到集羣中
  • 可是咱們發現新的集羣主節點沒有槽,因此添加完主節點須要對主節點進行hash槽分配,這樣該主節才能夠存儲數據。

8.7.二、hash槽從新分配

  • 添加完主節點須要對主節點進行hash槽分配,這樣該主節才能夠存儲數據。
  • 查看集羣中槽佔用狀況
  • redis集羣有16384個槽,集羣中的每一個結點分配自已槽,經過查看集羣結點能夠看到槽佔用狀況。
  • 給剛添加的主節點7007分配槽
    第一步:鏈接上集羣(鏈接集羣中任意一個可用結點都行)
    [root@itheima redis-cluster]# # ./redis-trib.rb reshard 192.168.5.128:7001
    第二步:輸入要分配的槽數量、輸入接收槽的結點id和輸入源結點id,回車

    第五步:輸入yes開始移動槽到目標結點id

    第六步:查看分配後主節點7007的槽

8.7.三、添加從節點

  • 集羣建立成功後能夠向集羣中添加節點,下面是添加一個slave從節點。
  • 添加7008從結點,將7008做爲7007的從結點。
  • 準備工做:1) 先複製出一個7008的機器;2) 若是存在持久化文件和節點配置文件,就刪除掉;3) 修改機器7008的端口;4) 啓動機器7008
    [root@itheima redis-cluster]# cp 7001/ 7008 -r
    [root@itheima redis-cluster]# cd 7008/
    [root@itheima 7008]# rm -rf appendonly.aof dump.rdb nodes.conf
    [root@itheima redis-cluster]# cd 7008/
    [root@itheima 7008]# vim redis.conf
    [root@itheima 7008]# ./redis-server redis.conf
  • 添加7008從結點,將7008做爲7007的從結點   命令:./redis-trib.rb add-node --slave --master-id 主節點id 新節點的ip和端口 舊節點ip和端口   命令:[root@itheima redis-cluster]# ./redis-trib.rb add-node --slave --master-id 26630461d8a63a7398e3f43b7366014c72a6a7ef 192.168.5.128:7008 192.168.5.128:7007
    • 示例以下:
[root@itheima redis-cluster]# ./redis-trib.rb add-node --slave --master-id 26630461d8a63a7398e3f43b7366014c72a6a7ef 192.168.5.128:7008 192.168.5.128:7007
>>> Adding node 192.168.5.128:7008 to cluster 192.168.5.128:7007
Connecting to node 192.168.5.128:7007: OK
Connecting to node 192.168.5.128:7001: OK
Connecting to node 192.168.5.128:7003: OK
Connecting to node 192.168.5.128:7005: OK
Connecting to node 192.168.5.128:7004: OK
Connecting to node 192.168.5.128:7006: OK
Connecting to node 192.168.5.128:7002: OK
>>> Performing Cluster Check (using node 192.168.5.128:7007)
M: 26630461d8a63a7398e3f43b7366014c72a6a7ef 192.168.5.128:7007
   slots:0-165,5461-5627,10923-11088 (499 slots) master
   0 additional replica(s)
M: 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7 192.168.5.128:7001
   slots:166-5460 (5295 slots) master
   1 additional replica(s)
M: d7d28caadfdc4161261305f2d2baf55d2d8f4221 192.168.5.128:7003
   slots:11089-16383 (5295 slots) master
   1 additional replica(s)
S: 8cf60c085a58b60557a887a5e8451ce38e6b54fa 192.168.5.128:7005
   slots: (0 slots) slave
   replicates 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d
S: f124b72c0421c7514f44668d30761d075e42643d 192.168.5.128:7004
   slots: (0 slots) slave
   replicates 3cbed89c47ca14b3d1eb11dd2f7525fa6cb4fcd7
S: 05ad0eb9b5f839771b09dc18192909d5fa1f893e 192.168.5.128:7006
   slots: (0 slots) slave
   replicates d7d28caadfdc4161261305f2d2baf55d2d8f4221
M: 27d069e1b4d459a92b6cc9a1c92ad46ea00cb61d 192.168.5.128:7002
   slots:5628-10922 (5295 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.5.128:7008: OK
>>> Send CLUSTER MEET to node 192.168.5.128:7008 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.5.128:7007.
[OK] New node added correctly.
  • 注意:若是原來該結點在集羣中的配置信息已經生成到cluster-config-file指定的配置文件中(若是cluster-config-file沒有指定則默認爲nodes.conf),這時可能會報錯:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
  • 解決方法:是刪除生成的配置文件nodes.conf,刪除後再執行./redis-trib.rb add-node 指令
  • 查看集羣中的結點,剛添加的7008爲7007的從節點:

8.7.四、刪除結點

  • 命令:./redis-trib.rb del-node 192.168.5.128:7003 d7d28caadfdc4161261305f2d2baf55d2d8f4221
  • 刪除已經佔有hash槽的結點(即主節點)會失敗,報錯以下:
    [ERR] Node 192.168.5.128:7003 is not empty! Reshard data away and try again.
  • 須要將該結點(主節點)佔用的hash槽分配出去後再刪除(參考hash槽從新分配章節)。

示例以下:
刪除從節點:算法

[root@itheima redis-cluster]# ./redis-trib.rb del-node 192.168.5.128:7005 8cf60c085a58b60557a887a5e8451ce38e6b54fa
>>> Removing node 8cf60c085a58b60557a887a5e8451ce38e6b54fa from cluster 192.168.5.128:7005
Connecting to node 192.168.5.128:7005: OK
Connecting to node 192.168.5.128:7008: OK
Connecting to node 192.168.5.128:7004: OK
Connecting to node 192.168.5.128:7007: OK
Connecting to node 192.168.5.128:7006: OK
Connecting to node 192.168.5.128:7003: OK
Connecting to node 192.168.5.128:7001: OK
Connecting to node 192.168.5.128:7002: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@itheima redis-cluster]

刪除主節點:spring

[root@itheima redis-cluster]# ./redis-trib.rb del-node 192.168.5.128:7003 d7d28caadfdc4161261305f2d2baf55d2d8f4221
>>> Removing node d7d28caadfdc4161261305f2d2baf55d2d8f4221 from cluster 192.168.5.128:7003
Connecting to node 192.168.5.128:7003: OK
Connecting to node 192.168.5.128:7001: OK
Connecting to node 192.168.5.128:7008: OK
Connecting to node 192.168.5.128:7007: OK
Connecting to node 192.168.5.128:7004: OK
Connecting to node 192.168.5.128:7002: OK
Connecting to node 192.168.5.128:7006: OK
[ERR] Node 192.168.5.128:7003 is not empty! Reshard data away and try again.
[root@itheima redis-cluster]

九、Jedis鏈接Redis集羣

9.一、防火牆配置

  • CentOS7默認的防火牆不是iptables,而是firewalld。咱們能夠試一下systemctl stop firewalld關閉防火牆,可是不推薦該方式。
  • 若是是iptables,就vim /etc/sysconfig/iptables修改配置便可。詳細的過程,博主之後會整理補充上。
  • 本博主的是CentOS7,防火牆使用的是firewalld,咱們使用命令的方式來添加端口(修改後須要重啓firewalld服務):
      [root@itheima ~]# cd /etc/firewalld/zones/
      [root@itheima zones]# firewall-cmd --permanent --add-port=6379/tcp
      [root@itheima zones]# service firewalld restart
      Redirecting to /bin/systemctl restart firewalld.service
      [root@itheima zones]#

9.二、代碼實現

  • 建立JedisCluster類鏈接redis集羣。
    /**
     * Jedis鏈接Redis集羣
     */

    @Test
    public void testJedisCluster1() 
{
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.5.128"7001));
        nodes.add(new HostAndPort("192.168.5.128"7002));
        nodes.add(new HostAndPort("192.168.5.128"7003));
        nodes.add(new HostAndPort("192.168.5.128"7004));
        nodes.add(new HostAndPort("192.168.5.128"7005));
        nodes.add(new HostAndPort("192.168.5.128"7006));
        // 建立JedisCluster對象,在系統中是單例存在的
        JedisCluster jedisCluster = new JedisCluster(nodes);
        // 執行JedisCluster對象中的方法,方法和redis中的一一對應
        jedisCluster.set("cluster-test""Jedis connects Redis cluster test");
        String result = jedisCluster.get("cluster-test");
        System.out.println(result);
        // 程序結束時須要關閉JedisCluster對象
        jedisCluster.close();
    }

9.三、使用spring實現Jedis鏈接Redis集羣

  • 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
>

    <!-- 鏈接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大鏈接數 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空閒鏈接數 -->
        <property name="maxIdle" value="10" />
        <!-- 每次釋放鏈接的最大數目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 釋放鏈接的掃描間隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 鏈接最小空閒時間 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 鏈接空閒多久後釋放, 當空閒時間>該值 且 空閒鏈接>最大空閒鏈接數 時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 獲取鏈接時的最大等待毫秒數,小於零:阻塞不肯定的時間,默認-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在獲取鏈接的時候檢查有效性, 默認false -->
        <property name="testOnBorrow" value="true" />
        <!-- 在空閒時檢查有效性, 默認false -->
        <property name="testWhileIdle" value="true" />
        <!-- 鏈接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>
    <!-- redis集羣 -->
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg index="0">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.5.128"></constructor-arg>
                    <constructor-arg index="1" value="7006"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
    </bean>
</beans>
  • 測試代碼
    private ApplicationContext applicationContext;
    @Before
    public void init() 
{
        applicationContext = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");
    }

    /**
   * 使用spring實現Jedis鏈接Redis集羣
   */

    @Test
    public void testJedisCluster2() 
{
        JedisCluster jedisCluster = (JedisCluster) applicationContext.getBean("jedisCluster");

        jedisCluster.set("name""xiaoyi");
        String value = jedisCluster.get("name");
        System.out.println(value);    }
相關文章
相關標籤/搜索