22、Redis進(jìn)階

Redis的發(fā)布與訂閱(一般不用)

  • 打開三個(gè)遠(yuǎn)程連接工具

  • subscribe channel1 channel2 channel3 訂閱頻道1、2、3

  • subscribe channel4 channel2 channel3 訂閱頻道4、2、3

  • publish channel1 123 往1頻道中發(fā)布消息 訂閱1頻道的用戶收到 "message" "channel1" "123"

  • publish channel2 nihao 往2頻道中發(fā)布消息 兩邊都收到 "message" "channel2" "nihao"

  • psubscribe channel* 通配符訂閱

Redis持久化

  • (默認(rèn))RDB 持久化 每隔一段時(shí)間備份一次 最后一次可能丟失 redis.conf 文件配置

    save 3600 1 # redis保存的數(shù)據(jù)發(fā)生了1次改變 在一個(gè)小時(shí)后備份
    save 300 100 # redis保存的數(shù)據(jù)發(fā)生了100次改變 在5分鐘后備份
    save 60 10000 # redis保存的數(shù)據(jù)發(fā)生了10000次改變 在1分鐘后備份
    stop-writes-on-bgsave-error yes # 保存的時(shí)候發(fā)生錯(cuò)誤 則停止寫操作
    rdbcompression yes # 是否壓縮 壓縮消耗cpu 不壓縮文件比較大
    rdbchecksum yes # 一旦壓縮 是否使用CRC64校驗(yàn) 在保存和加載的時(shí)候會(huì)有性能損耗
    dbfilename dump.rdb # 保存的文件名
    dir /usr/local/redis/workspace # 保存的路徑
    
  • AOF 持久化 每次寫都會(huì)備份 (追加) 文件會(huì)很大

    appendonly no # 是否開啟
    appendfilename "appendonly.aof" # 文件名
    # appendfsync always # 只要有寫操作就會(huì)保存 
    appendfsync everysec # 每秒同步(用的最多)
    # appendfsync no # 關(guān)閉
    no-appendfsync-on-rewrite no # 當(dāng)有正在保存時(shí)是否執(zhí)行重寫操作 默認(rèn)即可 文件大,服務(wù)器配置低可能1s還沒(méi)寫好
    auto-aof-rewrite-percentage 100 # 當(dāng)前AOF文件大小和最后一次重寫后的大小之間的比率等于或者大于指定的增長(zhǎng)百分比
    auto-aof-rewrite-min-size 64mb # 只有當(dāng)AOF文件大小大于該值時(shí)候才可能重寫
    

主從架構(gòu)(就是讀寫分離)

  • 主從原理(必須開啟Master的持久化)
圖1.png
  • 搭建一主二從

    • 三臺(tái)內(nèi)網(wǎng)互通的計(jì)算機(jī) ip分別是192.168.0.199、200、201 199作為主 其他作為從
    • info replication 查看主從信息
    # Replication
    
    role:master # 節(jié)點(diǎn)角色
    connected_slaves:0 # 連接的從節(jié)點(diǎn)redis數(shù)量
    master_failover_state:no-failover
    master_replid:de00913da67e50cc9a4d3d11ef8a7947ab545a28
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:0
    second_repl_offset:-1
    repl_backlog_active:0
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:0
    repl_backlog_histlen:0
    
    • 修改200和201的conf文件
    replicaof 192.168.0.199 6379 # 配置是哪個(gè)ip + 端口下的從節(jié)點(diǎn)
    masterauth 123456 # master主節(jié)點(diǎn)redis的密碼
    replica-read-only yes # 只允許讀取
    
    • 繼續(xù)查看主從信息
    # Replication
    
    role:master
    connected_slaves:2
    slave0:ip=192.168.0.200,port=6379,state=online,offset=210,lag=0
    slave1:ip=192.168.0.201,port=6379,state=online,offset=210,lag=0
    master_failover_state:no-failover
    master_replid:78f753a8a4e85ec78c7bb490f26c9fe997870d7e
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:210
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:210
    
    • 此時(shí)199中redis里的數(shù)據(jù)會(huì)同步到200和201里面 從節(jié)點(diǎn)查看主從信息
    # Replication
    
    role:slave
    master_host:192.168.0.199
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:2
    master_sync_in_progress:0
    slave_repl_offset:406
    slave_priority:100
    slave_read_only:1
    replica_announced:1
    connected_slaves:0
    master_failover_state:no-failover
    master_replid:78f753a8a4e85ec78c7bb490f26c9fe997870d7e
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:406
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:406
    
    • 無(wú)磁盤化復(fù)制(主節(jié)點(diǎn)修改配置 一般不采用)
    repl-diskless-sync no # 是否開啟
    repl-diskless-sync-delay 5 # 在復(fù)制之前等待的秒數(shù)
    

哨兵模式

  • 進(jìn)入redis的解壓目錄 cd /usr/local/software/redis-6.2.3/

  • cp sentinel.conf /usr/local/redis/

  • cd /usr/local/redis/

  • mkdir /usr/local/redis/sentinel -p

  • 編輯sentinel.conf 文件(192.168.0.199)

protected-mode no # 是否開啟保護(hù)模式
port 26379 #工作端口
daemonize yes # yes在后臺(tái)運(yùn)行 no前臺(tái)運(yùn)行
pidfile /var/run/redis-sentinel.pid # pid
logfile /usr/local/redis/sentinel/redis-sentinel.log # 日志
dir /usr/local/redis/sentinel # 工作空間
sentinel monitor mymaster 192.168.0.199 6379 2 # 主要配置 mymaster開始意思分別是 別名 ip 端口 哨兵數(shù)量
sentinel auth-pass mymaster 123456 #密碼 mymaster 要和上面的別名一致
sentinel down-after-milliseconds mymaster 30000 #認(rèn)為master節(jié)點(diǎn)宕機(jī)的時(shí)間段單位毫秒 30s沒(méi)響應(yīng)認(rèn)為宕機(jī)
sentinel parallel-syncs mymaster 1 # 并行同步數(shù)量 一次1個(gè) 設(shè)為2就是一次2個(gè)
sentinel failover-timeout mymaster 180000 # 3分鐘內(nèi)該哨兵重選一個(gè)master沒(méi)有響應(yīng),則換下一個(gè)哨兵
  • scp sentinel.conf root@192.168.0.200:/usr/local/redis/sentinel.conf
  • scp sentinel.conf root@192.168.0.201:/usr/local/redis/sentinel.conf
  • redis-sentinel /usr/local/redis/sentinel.conf 必須要有配置文件啟動(dòng)
  • ps -ef |grep redis 多出來(lái)一個(gè) redis-sentinel *:26379 [sentinel] 進(jìn)程
  • 打開日志 tail /usr/local/redis/sentinel/redis-sentinel.log查看
  • 啟動(dòng)200和201的哨兵 log日志中多出兩個(gè)
+sentinel sentinel f5be33f3e9e1e9f80d2bde0c75065788bf7592c4 192.168.0.200 26379 @ mymaster 192.168.0.199 6379
+sentinel sentinel bdf78c2ff3e82457c102a1492663a8c4b58020d4 192.168.0.201 26379 @ mymaster 192.168.0.199 6379
  • 手動(dòng)關(guān)閉199的redis 查看日志
+sdown master mymaster 192.168.0.199 6379 # 標(biāo)志宕機(jī)
+new-epoch 1
+vote-for-leader bdf78c2ff3e82457c102a1492663a8c4b58020d4 1 #投票選舉新的master
+odown master mymaster 192.168.0.199 6379 #quorum 3/2
Next failover delay: I will not start a failover before Thu May 13 09:24:29 2021
+config-update-from sentinel bdf78c2ff3e82457c102a1492663a8c4b58020d4 192.168.0.201 26379 @ mymaster 192.168.0.199 6379
+switch-master mymaster 192.168.0.199 6379 192.168.0.201 6379
+slave slave 192.168.0.200:6379 192.168.0.200 6379 @ mymaster 192.168.0.201 6379
+slave slave 192.168.0.199:6379 192.168.0.199 6379 @ mymaster 192.168.0.201 6379
+sdown slave 192.168.0.199:6379 192.168.0.199 6379 @ mymaster 192.168.0.201 6379
  • 再啟動(dòng)199的redis 會(huì)自動(dòng)變成從節(jié)點(diǎn)
  • 按照上面的配置 發(fā)現(xiàn)之前master節(jié)點(diǎn)變成從節(jié)點(diǎn)后 如果數(shù)據(jù)沒(méi)有同步查看redis.conf是否少了下面的配置
masterauth "123456" 
  • 和項(xiàng)目整合只需要在yml文件中配置
spring:
  redis:
    # redis 哨兵模式
    database: 0
    password: 123456
    sentinel:
      master: mymaster
      nodes: 192.168.0.199:26379,192.168.0.200:26379,192.168.0.201:26379

Redis-Cluster集群

  • 準(zhǔn)備6臺(tái)服務(wù)器 ip地址分別為192.168.0.(主199、從200)(主201、從202)(主203、從204)
  • 配置199 的redis.conf文件
  • cd /usr/local/redis/workspace
  • rm dump.rdb
  • rm append only.aof 集群必須要?jiǎng)h除這兩個(gè)文件否則報(bào)錯(cuò)
  • 重啟redis
  • ps -ef|grep redis 查看進(jìn)程 發(fā)現(xiàn)后面多了一個(gè) [cluster]標(biāo)志
cluster-enabled yes # 是否開啟集群
cluster-config-file nodes-6379.conf # 每個(gè)節(jié)點(diǎn)在集群中的配置文件 默認(rèn)即可
cluster-node-timeout 15000 # 超時(shí)時(shí)間 超過(guò)認(rèn)為master掛了 主備切換
appendonly yes # 保持開啟就可以了
  • 重復(fù)步驟到剩下的服務(wù)器
  • redis-cli -a 123456 --cluster create 192.168.0.199:6379 192.168.0.200:6379 192.168.0.201:6379 192.168.0.202:6379 192.168.0.203:6379 192.168.0.204:6379 --cluster replicas 1 ( 創(chuàng)建了三主三從的集群) 輸入yes
  • redis-cli -a 123456 --cluster check 192.168.0.199:6379 查看集群信息

slot槽

  • 總數(shù)是16384個(gè)

  • 平均分配給master節(jié)點(diǎn)

    • 199 0-5460
    • 201 5461 10922
    • 203 10923 16383
  • slot 存儲(chǔ)方式 hash(key)%16384 得到的值在0-5460就在199中

  • redis-cli -c -a 123456 -h 192.168.0.199 -p6379 連接到redis集群里的某個(gè)節(jié)點(diǎn)

  • cluster info 查看集群信息

  • cluster nodes 查看節(jié)點(diǎn)信息

  • 和項(xiàng)目整合

    spring:
      redis:
        # 集群模式
        password: 123456
        cluster:
          nodes: 192.168.0.199:6379,192.168.0.200:6379,192.168.0.201:6379,192.168.0.202:6379,192.168.0.203:6379,192.168.0.204:6379
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • NOsql 泛指非關(guān)系型數(shù)據(jù)庫(kù),為了克服超大規(guī)模的高并發(fā)的問(wèn)題,NoSQL在當(dāng)今大數(shù)據(jù)環(huán)境下發(fā)展的十分迅速,Red...
    Wirdeo閱讀 342評(píng)論 0 0
  • 轉(zhuǎn)原創(chuàng)地址:https://blog.csdn.net/hebtu666/article/details/1073...
    crossme閱讀 314評(píng)論 0 1
  • 標(biāo)簽: redis 緩存 主從 哨兵 集群 本文簡(jiǎn)單的介紹redis三種模式在linux的安裝部署和數(shù)據(jù)存儲(chǔ)的總結(jié)...
    luhanlin閱讀 4,493評(píng)論 0 5
  • http://blog.51cto.com/mxlmgl/2065789 redis-server說(shuō)明 服務(wù)器A:...
    SkTj閱讀 2,416評(píng)論 0 5
  • 前言 Redis是一個(gè)高性能的key-value數(shù)據(jù)庫(kù),現(xiàn)時(shí)越來(lái)越多企業(yè)與應(yīng)用使用Redis作為緩存服務(wù)器。樓主是...
    技術(shù)管理方法論閱讀 1,661評(píng)論 0 0

友情鏈接更多精彩內(nèi)容