欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Rocky9部署redis的實現(xiàn)示例

 更新時間:2024年06月28日 11:23:24   作者:懵逼的大鵝  
本文主要介紹了Rocky9部署redis的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧

1.redis簡介

為了提高網(wǎng)站響應(yīng)速度,企業(yè)會將熱點數(shù)據(jù)保存在內(nèi)存中而不是直接從后端數(shù)據(jù)庫中讀取。大型網(wǎng)站應(yīng)用,熱點數(shù)據(jù)往往巨大,幾十G上百G是很正常的事,這種情況下,就需要用到緩存服務(wù)器,通過緩存服務(wù)器承載大部分用戶請求,小部分用戶請求交給后端服務(wù)器處理,如此一來,就可以大大提高用戶訪問的速度,提升用戶使用體驗。

常用的緩存服務(wù)器有:

  • memcache
  • redis

Redis 是一種開源(BSD 許可)內(nèi)存中數(shù)據(jù)結(jié)構(gòu)存儲,用作數(shù)據(jù)庫、緩存、消息代理和流引擎。

為了實現(xiàn)最佳性能,Redis 使用 內(nèi)存數(shù)據(jù)集。根據(jù)您的使用案例,Redis 可以通過定期將數(shù)據(jù)集轉(zhuǎn)儲到磁盤或?qū)⒚總€命令附加到基于磁盤的日志來持久保存您的數(shù)據(jù)。如果您只需要功能豐富的網(wǎng)絡(luò)內(nèi)存緩存,您還可以禁用持久性。

Redis 支持異步復(fù)制,具有快速非阻塞同步和自動重新連接以及網(wǎng)絡(luò)分割上的部分重新同步。

2 redis集群

2.1 redis集群分類

集群是一組相互獨立、通過高速網(wǎng)絡(luò)互聯(lián)的計算機,它們構(gòu)成了一個組,并以單一系統(tǒng)的模式加以管理。一個客戶與集群相互作用時,集群就像是一個獨立的服務(wù)器。集群配置是用于提高可用性和可縮放性。

redis集群是一個由多個主從節(jié)點群組成的分布式服務(wù)集群,它具有復(fù)制、高可用和分片特性。

redis集群有三種集群模式:

  • 主從模式
  • 哨兵(Sentinel)模式
  • Cluster模式(群集模式)

在服務(wù)開發(fā)中,單機都會存在單點故障的問題,即服務(wù)部署在一臺服務(wù)器上,一旦服務(wù)器宕機服務(wù)就不可用,所以為了讓服務(wù)高可用,分布式服務(wù)就出現(xiàn)了,將同一服務(wù)部署到多臺機器上,即使其中幾臺服務(wù)器宕機,只要有一臺服務(wù)器可用服務(wù)就可用。

redis也是一樣,為了解決單機故障引入了主從模式,但主從模式存在一個問題:master節(jié)點故障后服務(wù),需要人為的手動將slave節(jié)點切換成為maser節(jié)點后服務(wù)才恢復(fù)。redis為解決這一問題又引入了哨兵模式,哨兵模式能在master節(jié)點故障后能自動將salve節(jié)點提升成master節(jié)點,不需要人工干預(yù)操作就能恢復(fù)服務(wù)可用。

但是主從模式、哨兵模式都沒有達到真正的數(shù)據(jù)sharding存儲,每個redis實例中存儲的都是全量數(shù)據(jù),所以redis cluster就誕生了,實現(xiàn)了真正的數(shù)據(jù)分片存儲。

2.2 主從模式

redis單節(jié)點雖然有通過RDB和AOF持久化機制能將數(shù)據(jù)持久化到硬盤上,但數(shù)據(jù)是存儲在一臺服務(wù)器上的,如果服務(wù)器出現(xiàn)硬盤故障等問題,會導(dǎo)致數(shù)據(jù)不可用,而且讀寫無法分離,讀寫都在同一臺服務(wù)器上,請求量大時會出現(xiàn)I/O瓶頸。

為了避免單點故障 和 讀寫不分離,Redis 提供了復(fù)制(replication)功能實現(xiàn)master數(shù)據(jù)庫中的數(shù)據(jù)更新后,會自動將更新的數(shù)據(jù)同步到其他slave數(shù)據(jù)庫上。

在這里插入圖片描述

通過數(shù)據(jù)復(fù)制,Redis 的一個 master 可以掛載多個 slave,而 slave 下還可以掛載多個 slave,形成多層嵌套結(jié)構(gòu)。所有寫操作都在 master 實例中進行,master 執(zhí)行完畢后,將寫指令分發(fā)給掛在自己下面的 slave 節(jié)點。slave 節(jié)點下如果有嵌套的 slave,會將收到的寫指令進一步分發(fā)給掛在自己下面的 slave。

通過多個 slave,Redis 的節(jié)點數(shù)據(jù)就可以實現(xiàn)多副本保存,任何一個節(jié)點異常都不會導(dǎo)致數(shù)據(jù)丟失,同時多 slave 可以 N 倍提升讀性能。master 只寫不讀,這樣整個 master-slave 組合,讀寫能力都可以得到大幅提升。

主從模式優(yōu)缺點:

  • 優(yōu)點: 主從結(jié)構(gòu)具有讀寫分離,提高效率、數(shù)據(jù)備份,提供多個副本等優(yōu)點。
  • 不足: 最大的不足就是主從模式不具備自動容錯和恢復(fù)功能,主節(jié)點故障,集群則無法進行工作,可用性比較低,從節(jié)點升主節(jié)點需要人工手動干預(yù)。

普通的主從模式,當主數(shù)據(jù)庫崩潰時,需要手動切換從數(shù)據(jù)庫成為主數(shù)據(jù)庫:

  • 在從數(shù)據(jù)庫中使用SLAVE NO ONE命令將從數(shù)據(jù)庫提升成主數(shù)據(jù)繼續(xù)服務(wù)。

  • 啟動之前崩潰的主數(shù)據(jù)庫,然后使用SLAVEOF命令將其設(shè)置成新的主數(shù)據(jù)庫的從數(shù)據(jù)庫,即可同步數(shù)據(jù)。

2.3 哨兵模式

主從同步/復(fù)制的模式,當主服務(wù)器宕機后,需要手動把一臺從服務(wù)器切換為主服務(wù)器,這就需要人工干預(yù),費事費力,還會造成一段時間內(nèi)服務(wù)不可用,這時候就需要哨兵模式登場了。

哨兵模式是從Redis的2.6版本開始提供的,但是當時這個版本的模式是不穩(wěn)定的,直到Redis的2.8版本以后,這個哨兵模式才穩(wěn)定下來。

哨兵模式核心還是主從復(fù)制,只不過在相對于主從模式在主節(jié)點宕機導(dǎo)致不可寫的情況下,多了一個競選機制:從所有的從節(jié)點競選出新的主節(jié)點。競選機制的實現(xiàn),是依賴于在系統(tǒng)中啟動一個sentinel進程。

哨兵本身也有單點故障的問題,所以在一個一主多從的Redis系統(tǒng)中,可以使用多個哨兵進行監(jiān)控,哨兵不僅會監(jiān)控主數(shù)據(jù)庫和從數(shù)據(jù)庫,哨兵之間也會相互監(jiān)控。每一個哨兵都是一個獨立的進程,作為進程,它會獨立運行。

2.3.1 哨兵模式的作用

監(jiān)控所有服務(wù)器是否正常運行:通過發(fā)送命令返回監(jiān)控服務(wù)器的運行狀態(tài),除了監(jiān)控主服務(wù)器、從服務(wù)器外,哨兵之間也相互監(jiān)控。

故障切換:當哨兵監(jiān)測到master宕機,會自動將slave切換成master,然后通過發(fā)布訂閱模式通知其他的從服務(wù)器,修改配置文件,讓它們切換master。同時那臺有問題的舊主也會變?yōu)樾轮鞯膹?,也就是說當舊的主即使恢復(fù)時,并不會恢復(fù)原來的主身份,而是作為新主的一個從。

2.3.2 哨兵模式優(yōu)缺點

優(yōu)點:

哨兵模式是基于主從模式的,解決主從模式中master故障不能自動切換故障的問題。

缺點:

哨兵模式下每臺 Redis 服務(wù)器都存儲相同的數(shù)據(jù),很浪費內(nèi)存空間;數(shù)據(jù)量太大,主從同步時嚴重影響了master性能。

哨兵模式是中心化的集群實現(xiàn)方案,每個從機和主機的耦合度很高,master宕機到salve選舉master恢復(fù)期間服務(wù)不可用。因為投票選舉結(jié)束之前,誰也不知道主機和從機是誰,此時Redis也會開啟保護機制,禁止寫操作,直到選舉出了新的Redis主機。

哨兵模式始終只有一個Redis主機來接收和處理寫請求,寫操作還是受單機瓶頸影響,沒有實現(xiàn)真正的分布式架構(gòu)。

2.4 Cluster模式

主從模式或哨兵模式每個節(jié)點存儲的數(shù)據(jù)都是全量的數(shù)據(jù),數(shù)據(jù)量過大時,就需要對存儲的數(shù)據(jù)進行分片后存儲到多個redis實例上。此時就要用到Redis Sharding技術(shù)。

redis在3.0上加入了 Cluster 集群模式,實現(xiàn)了 Redis 的分布式存儲,也就是說每臺 Redis 節(jié)點上存儲不同的數(shù)據(jù)。cluster模式為了解決單機Redis容量有限的問題,將數(shù)據(jù)按一定的規(guī)則分配到多臺機器,內(nèi)存/QPS不受限于單機,可受益于分布式集群高擴展性。

Redis Cluster是一種服務(wù)器Sharding技術(shù)(分片和路由都是在服務(wù)端實現(xiàn)),采用多主多從,每一個分區(qū)都是由一個Redis主機和多個從機組成,片區(qū)和片區(qū)之間是相互平行的。Redis Cluster集群采用了P2P的模式,完全去中心化。

官方推薦,集群部署至少要 3 臺以上的master節(jié)點,最好使用 3 主 3 從六個節(jié)點的模式。Redis Cluster集群具有如下幾個特點:

  • 集群完全去中心化,采用多主多從;所有的redis節(jié)點彼此互聯(lián)(PING-PONG機制),內(nèi)部使用二進制協(xié)議優(yōu)化傳輸速度和帶寬。
  • 客戶端與 Redis 節(jié)點直連,不需要中間代理層。客戶端不需要連接集群所有節(jié)點,連接集群中任何一個可用節(jié)點即可。
  • 每一個分區(qū)都是由一個Redis主機和多個從機組成,分片和分片之間是相互平行的。
  • 每一個master節(jié)點負責維護一部分槽,以及槽所映射的鍵值數(shù)據(jù);集群中每個節(jié)點都有全量的槽信息,通過槽每個node都知道具體數(shù)據(jù)存儲到哪個node上。

redis cluster主要是針對海量數(shù)據(jù)+高并發(fā)+高可用的場景,如果你的數(shù)據(jù)量很大,那么建議就用redis cluster,數(shù)據(jù)量不是很大時,使用sentinel就夠了。redis cluster的性能和高可用性均優(yōu)于哨兵模式。

Redis Cluster采用虛擬哈希槽分區(qū)而非一致性hash算法,預(yù)先分配一些卡槽,所有的鍵根據(jù)哈希函數(shù)映射到這些槽內(nèi),每一個分區(qū)內(nèi)的master節(jié)點負責維護一部分槽以及槽所映射的鍵值數(shù)據(jù)。

3. 部署redis

由于資源有限,所有redis都在一臺主機部署

3.1 redis單機部署

//關(guān)閉防火墻和selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@localhost ~]# vi /etc/selinux/config 
SELINUX=disabled
[root@localhost ~]# reboot  //重啟虛擬機使seLinux保持disabled模式

//安裝解析軟件包,并下載redis
[root@localhost ~]# yum -y install wget make gcc gcc-c++
[root@localhost ~]# wget https://download.redis.io/redis-stable.tar.gz

//解壓redis
[root@localhost ~]# tar xf redis-stable.tar.gz 
[root@localhost ~]# cd redis-stable
[root@localhost redis-stable]# ls
00-RELEASENOTES     COPYING   MANIFESTO   runtest-cluster    sentinel.conf  utils
BUGS                deps      README.md   runtest-moduleapi  src
CODE_OF_CONDUCT.md  INSTALL   redis.conf  runtest-sentinel   tests
CONTRIBUTING.md     Makefile  runtest     SECURITY.md        TLS.md

//源碼安裝redis
[root@localhost redis-stable]# make
[root@localhost redis-stable]# make install

//配置redis
[root@localhost redis-stable]# mkdir /etc/redis
[root@localhost redis-stable]# cp redis.conf /etc/redis/
[root@localhost ~]# echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf  //打開超載提交功能
[root@localhost ~]# sysctl -p
vm.overcommit_memory = 1
[root@localhost ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

//啟動redis
[root@localhost ~]# redis-server /etc/redis/redis.conf
16905:C 29 Dec 2023 16:11:24.435 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16905:C 29 Dec 2023 16:11:24.435 * Redis version=7.2.3, bits=64, commit=00000000, modified=0, pid=16905, just started
16905:C 29 Dec 2023 16:11:24.435 * Configuration loaded
16905:M 29 Dec 2023 16:11:24.436 * Increased maximum number of open files to 10032 (it was originally set to 1024).
16905:M 29 Dec 2023 16:11:24.436 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.2.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16905
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

16905:M 29 Dec 2023 16:11:24.437 * Server initialized
16905:M 29 Dec 2023 16:11:24.437 * Ready to accept connections tcp
// 能看到大正方體redis單機部署就完成了

[root@localhost ~]# ss -anlt  //redis單機的默認端口號是6379
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        511              127.0.0.1:6379            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0        511                  [::1]:6379               [::]:*                
LISTEN   0        128                   [::]:22                 [::]:*
                
[root@localhost ~]# redis-cli  //使用此命令就能登錄到redis
127.0.0.1:6379> 

//修改配置文件/etc/redis/redis.conf使redis在后臺運行
[root@localhost ~]# vim /etc/redis/redis.conf 
bind 192.168.50.151
daemonize yes  //守護模式默認是no改為yes
requirepass password  //此句下方可以設(shè)置redis的密碼

[root@localhost ~]# redis-server /etc/redis/redis.conf
[root@localhost ~]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port Process   
LISTEN   0        511         192.168.50.151:6379            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                   [::]:22                 [::]:*
[root@localhost ~]# redis-cli -h 192.168.50.151
192.168.50.151:6379> 

//認證
192.168.50.151:6379> keys *
(error) NOAUTH Authentication required.
192.168.50.151:6379> auth password
OK
192.168.50.151:6379> keys *
1) "name"

3.2 redis主從

//修改名稱方便分辨,復(fù)制配置文件
[root@localhost redis]# mv redis.conf 6379.conf
[root@localhost redis]# cp 6379.conf 6380.conf
[root@localhost redis]# ls
6379.conf  6380.conf

//修改6380.conf配置文件
[root@localhost redis]# vim 6380.conf 
port 6380
pidfile /var/run/redis_6380.pid
replicaof 192.168.50.151 6379 //確定主是什么,這里的主是6379端口
masterauth  password  //在此行下面設(shè)置主的密碼

//啟動redis
[root@localhost redis]# redis-server /etc/redis/6379.conf
[root@localhost redis]# redis-server /etc/redis/6380.conf
[root@localhost redis]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        511         192.168.50.151:6379            0.0.0.0:*                
LISTEN   0        511         192.168.50.151:6380            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                   [::]:22                 [::]:*

//在6379上創(chuàng)建主鍵,測試是否配置主從復(fù)制成功
[root@localhost ~]# redis-cli -h 192.168.50.151 -p 6379
192.168.50.151:6379> auth password
OK
192.168.50.151:6379> set name tom
OK
192.168.50.151:6379> set age 20
OK
192.168.50.151:6379> keys *
1) "name"
2) "age

[root@localhost redis]# redis-cli -h 192.168.50.151 -p 6380
192.168.50.151:6380> auth password
OK
192.168.50.151:6380> keys *
1) "name"
2) "age"
192.168.50.151:6380> get name
"tom"

192.168.50.151:6380> info replication  //查看狀態(tài)
# Replication
role:slave   //角色名
master_host:192.168.50.151
master_port:6379
master_link_status:up  //連接是否正常,up即為正常
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:631
slave_repl_offset:631
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d8c6a13dde0fe88c70a3b8f71c30acbf73c4c72c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:631
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:617

3.3 redis哨兵

//停止先前的進程
[root@localhost redis]# pkill redis

//復(fù)制文件
[root@localhost redis]# cp 6379.conf 6380.conf
[root@localhost redis]# cp 6379.conf 6381.conf

//配置6380文件
[root@localhost redis]# vim 6380.conf
port 6380
pidfile /var/run/redis_6380.pid
logfile "/var/log/redis6380.log"

//配置6381文件
[root@localhost redis]# vim 6381.conf
:%s/6379/6381/g   //修改所有的6379為6380

//復(fù)制文件
[root@localhost redis]# cp 6379.conf 6389.conf
[root@localhost redis]# cp 6380.conf 6390.conf
[root@localhost redis]# cp 6381.conf 6391.conf

//配置6389文件
[root@localhost redis]# vim 6389.conf
:%s/6379/6389/g  //末行模式下執(zhí)行
replicaof 192.168.50.151  //添加以下參數(shù),確認主和主的密碼
masterauth password

//配置6390文件
[root@localhost redis]# vim 6390.conf
:%s/6380/6390/g  //修改所有的6380為6390
replicaof 192.168.50.151 6380  //添加以下參數(shù),確認主和主的密碼
masterauth password

//配置6391文件
[root@localhost redis]# vim 6391.conf
:%s/6381/6391/g
replicaof 192.168.50.151 6381
masterauth password

//復(fù)制哨兵文件到/etc/redis/下
[root@localhost ~]# cp redis-stable/sentinel.conf /etc/redis/
[root@localhost ~]# cd /etc/redis/

//配置哨兵要監(jiān)控的節(jié)點,
[root@localhost redis]# vim sentinel.conf
daemonize yes //默認是no改為yes
pidfile /var/run/redis-sentinel1.pid
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster1 192.168.50.151 6379 2
sentinel monitor mymaster2 192.168.50.151 6380 2
sentinel monitor mymaster3 192.168.50.151 6381 2
sentinel monitor myslave1 192.168.50.151 6389 2
sentinel monitor myslave2 192.168.50.151 6390 2
sentinel monitor myslave3 192.168.50.151 6391 2
sentinel monitor mysentinel1 192.168.50.151 26380 2
sentinel monitor mysentinel2 192.168.50.151 26381 2

# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
sentinel auth-pass mymaster1 password
sentinel auth-pass mymaster2 password
sentinel auth-pass mymaster3 password
sentinel auth-pass myslave1 password
sentinel auth-pass myslave2 password
sentinel auth-pass myslave3 password
sentinel auth-pass mysentinel1 password
sentinel auth-pass mysentinel2 password

# Default is 30 seconds.
sentinel down-after-milliseconds mymaster1 30000
sentinel down-after-milliseconds mymaster2 30000
sentinel down-after-milliseconds mymaster3 30000
sentinel down-after-milliseconds myslave1 30000
sentinel down-after-milliseconds myslave2 30000
sentinel down-after-milliseconds myslave3 30000
sentinel down-after-milliseconds mysentinel1 30000
sentinel down-after-milliseconds mysentinel2 30000

# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster1 1
sentinel parallel-syncs mymaster2 1
sentinel parallel-syncs mymaster3 1
sentinel parallel-syncs myslave1 1
sentinel parallel-syncs myslave2 1
sentinel parallel-syncs myslave3 1
sentinel parallel-syncs mysentinel1 1
sentinel parallel-syncs mysentinel2 1

# Default is 3 minutes.
sentinel failover-timeout mymaster1 180000
sentinel failover-timeout mymaster2 180000
sentinel failover-timeout mymaster3 180000
sentinel failover-timeout myslave1 180000
sentinel failover-timeout myslave2 180000
sentinel failover-timeout myslave3 180000
sentinel failover-timeout mysentinel1 180000
sentinel failover-timeout mysentinel2 180000

#最后一行
SENTINEL master-reboot-down-after-period mymaster1 0
SENTINEL master-reboot-down-after-period mymaster2 0
SENTINEL master-reboot-down-after-period mymaster3 0
SENTINEL master-reboot-down-after-period myslave1 0
SENTINEL master-reboot-down-after-period myslave2 0
SENTINEL master-reboot-down-after-period myslave3 0
SENTINEL master-reboot-down-after-period mysentinel1 0
SENTINEL master-reboot-down-after-period mysentinel2 0

//修改哨兵文件名字,方便分辨
[root@localhost redis]# mv sentinel.conf sentinel26379.conf

//復(fù)制哨兵文件
[root@localhost redis]# cp sentinel26379.conf sentinel26380.conf
[root@localhost redis]# cp sentinel26379.conf sentinel26381.conf

//配置哨兵26380文件
[root@localhost redis]# vim sentinel26380.conf
port 26380   //修改為26380
pidfile /var/run/redis-sentinel2.pid

//配置哨兵26381文件
[root@localhost redis]# vim sentinel26381.conf
port 26381
pidfile  /var/run/redis-sentinel3.pid

//啟動所有的節(jié)點
[root@localhost redis]# redis-server /etc/redis/6379.conf
[root@localhost redis]# redis-server /etc/redis/6380.conf
[root@localhost redis]# redis-server /etc/redis/6381.conf
[root@localhost redis]# redis-server /etc/redis/6389.conf
[root@localhost redis]# redis-server /etc/redis/6390.conf
[root@localhost redis]# redis-server /etc/redis/6391.conf
[root@localhost redis]# redis-sentinel /etc/redis/sentinel26379.conf 
[root@localhost redis]# redis-sentinel /etc/redis/sentinel26380.conf 
[root@localhost redis]# redis-sentinel /etc/redis/sentinel26381.conf 

//查看進程
[root@localhost ~]# ps -ef | grep redis
root        1468       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6379
root        1475       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6380
root        1483       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6381
root        1490       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6389
root        1498       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6390
root        1505       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6391
root        1512       1  0 16:10 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root        1518       1  0 16:11 ?        00:00:00 redis-sentinel *:26380 [sentinel]
root        1524       1  0 16:11 ?        00:00:00 redis-sentinel *:26381 [sentinel]
root        1530    1385  0 16:11 pts/0    00:00:00 grep --color=auto redis

//查看所有主節(jié)點以及對應(yīng)的從節(jié)點
[root@localhost ~]# redis-cli -h 192.168.50.151 -p 6379
192.168.50.151:6379> auth password
OK
192.168.50.151:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.50.151,port=6389,state=online,offset=24713,lag=0

[root@localhost ~]# redis-cli -h 192.168.50.151 -p 6389
192.168.50.151:6389> auth password
OK
192.168.50.151:6389> info replication
192.168.50.151:6389> info replication
# Replication
role:slave
master_host:192.168.50.151
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:16593
slave_repl_offset:16593
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:cd01e2131197e4e1eb2d40c4c4d40c33de7f2784
master_replid2:d8c6a13dde0fe88c70a3b8f71c30acbf73c4c72c
master_repl_offset:16593
second_repl_offset:15
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:16579

//測試主節(jié)點故障后會不會自動切換
[root@localhost ~]# ps -ef | grep redis
root        1468       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6379
root        1475       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6380
root        1483       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6381
root        1490       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6389
root        1498       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6390
root        1505       1  0 16:10 ?        00:00:00 redis-server 192.168.50.151:6391
root        1512       1  0 16:10 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root        1518       1  0 16:11 ?        00:00:00 redis-sentinel *:26380 [sentinel]
root        1524       1  0 16:11 ?        00:00:00 redis-sentinel *:26381 [sentinel]
root        1563    1535  0 16:14 pts/1    00:00:00 grep --color=auto redis
[root@localhost ~]# kill -9 1468
[root@localhost ~]# redis-cli -h 192.168.50.151 -p 6389
192.168.50.151:6389> auth password
OK
192.168.50.151:6389> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:e0327b0641a64e6dab20b141ab4cf21e4d5434e2
master_replid2:cd01e2131197e4e1eb2d40c4c4d40c33de7f2784
master_repl_offset:122588
second_repl_offset:81238
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:122574

//重啟節(jié)點看是否變?yōu)閺墓?jié)點以及對應(yīng)的主節(jié)點
[root@localhost ~]# redis-server /etc/redis/6379.conf
[root@localhost ~]# redis-cli -h 192.168.50.151 -p 6379
192.168.50.151:6379> auth password
OK
192.168.50.151:6379> info replication
# Replication
role:slave
master_host:192.168.50.151
master_port:6389
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:0
slave_repl_offset:0
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:40fef959403624aa384f742eadbe6e4ea85773bb
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

3.4 Cluster模式

//配置Cluster模式下的6379
[root@localhost redis]# vim 6379.conf
port 6379  //端口號
logfile "/var/log/redis-6379.log"  //認證文件
daemonize yes  //保護模式(在后臺啟動)
cluster-enabled yes (開啟Cluster模式)
cluster-config-file nodes-6379.conf  //Cluster配置文件
luster-node-timeout 15000 //超時間
appendonly yes

//配置Cluster模式下的6380
[root@localhost redis]# cp 6379.conf 6380.conf
[root@localhost redis]# vim 6380.conf
%s/6379/6380/g

//配置Cluster模式下的6389、6390、6391
[root@localhost redis]# cp 6379.conf 6381.conf
[root@localhost redis]# vim 6381.conf
%s/6379/6381/g

//配置Cluster模式下的6389、6390、6391
[root@localhost redis]# cp 6379.conf 6389.conf
[root@localhost redis]# sed -i 's/6379/6389/g' 6389.conf 
[root@localhost redis]# cp 6379.conf 6390.conf
[root@localhost redis]# sed -i 's/6379/6390/g' 6390.conf 
[root@localhost redis]# cp 6379.conf 6391.conf
[root@localhost redis]# sed -i 's/6379/6391/g' 6391.conf

//配置redis開機自啟
[root@localhost ~]# vim /usr/bin/redis-daemon
#!/bin/bash
/usr/local/bin/redis-server /etc/redis/6379.conf
/usr/local/bin/redis-server /etc/redis/6380.conf
/usr/local/bin/redis-server /etc/redis/6381.conf
/usr/local/bin/redis-server /etc/redis/6389.conf
/usr/local/bin/redis-server /etc/redis/6390.conf
/usr/local/bin/redis-server /etc/redis/6391.conf
//此腳本可以一次性啟動多個服務(wù)端口

[root@localhost ~]# chmod +x /usr/bin/redis-daemon
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/redis-daemon.service
[root@localhost ~]# vim /usr/lib/systemd/system/redis-daemon.service
[Unit]
Description=redis replication server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/bin/redis-daemon
ExecStop=/usr/bin/pkill redis
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

[root@localhost redis]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

[root@localhost redis]# ss -anlt
State    Recv-Q    Send-Q       Local Address:Port        Peer Address:Port   Process   
LISTEN   0         511              127.0.0.1:16389            0.0.0.0:*                
LISTEN   0         511              127.0.0.1:16390            0.0.0.0:*                
LISTEN   0         511              127.0.0.1:16391            0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6379             0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6380             0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6381             0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6389             0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6390             0.0.0.0:*                
LISTEN   0         128                0.0.0.0:22               0.0.0.0:*                
LISTEN   0         511              127.0.0.1:6391             0.0.0.0:*                
LISTEN   0         511              127.0.0.1:16379            0.0.0.0:*                
LISTEN   0         511              127.0.0.1:16380            0.0.0.0:*                
LISTEN   0         511              127.0.0.1:16381            0.0.0.0:*                
LISTEN   0         511                  [::1]:16389               [::]:*                
LISTEN   0         511                  [::1]:16390               [::]:*                
LISTEN   0         511                  [::1]:16391               [::]:*                
LISTEN   0         511                  [::1]:6379                [::]:*                
LISTEN   0         511                  [::1]:6380                [::]:*                
LISTEN   0         511                  [::1]:6381                [::]:*                
LISTEN   0         511                  [::1]:6389                [::]:*                
LISTEN   0         511                  [::1]:6390                [::]:*                
LISTEN   0         128                   [::]:22                  [::]:*                
LISTEN   0         511                  [::1]:6391                [::]:*                
LISTEN   0         511                  [::1]:16379               [::]:*                
LISTEN   0         511                  [::1]:16380               [::]:*                
LISTEN   0         511                  [::1]:16381               [::]:*

//查看日志
[root@localhost log]# tail -100f redis-6379.log
20114:M 02 Jan 2024 15:05:17.373 * No cluster configuration found, I'm f33525126ab16bfa8c9056d19f9c3634aa3db08a

//創(chuàng)建集群
[root@localhost ~]# redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1:6391 --cluster-replicas 1
Adding replica 127.0.0.1:6391 to 127.0.0.1:6379
Adding replica 127.0.0.1:6389 to 127.0.0.1:6380
Adding replica 127.0.0.1:6390 to 127.0.0.1:6381
Can I set the above configuration? (type 'yes' to accept): yes
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

//測試
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> set name tom
OK
127.0.0.1:6380> get name
"tom"

[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6389
127.0.0.1:6389> keys *
1) "name"

127.0.0.1:6379> cluster nodes //查看節(jié)點信息

[root@localhost~]#: yum -y install ruby
[root@localhost ~]#: vim example.rb
require './cluster'

     if ARGV.length != 2
         startup_nodes = [
             {:host => "127.0.0.1", :port => 6379},
             {:host => "127.0.0.1", :port => 6380}
         ]
     else
         startup_nodes = [
            {:host => ARGV[0], :port => ARGV[1].to_i}
         ]
     end
     
     rc = RedisCluster.new(startup_nodes,32,:timeout => 0.1)
     
     last = false  
    
     while not last
         begin
             last = rc.get("__last__")
             last = 0 if !last
         rescue => e
             puts "error #{e.to_s}"
             sleep 1
         end 
     end
     
     ((last.to_i+1)..1000000000).each{|x|
         begin
             rc.set("foo#{x}",x)
             puts rc.get("foo#{x}")
             rc.set("__last__",x)
         rescue => e
             puts "error #{e.to_s}"
         end
         sleep 0.1
      }

[root@localhost ~]#: redis-cli -h 127.0.0.1 -p 6379 -c
127.0.0.1:6379> keys *   //可修改不同節(jié)點的事務(wù)
1) "age"
[root@localhost ~]#: redis-cli -h 127.0.0.1 -p 6380 -c

127.0.0.1:6380> get age
-> Redirected to slot [741] located at 127.0.0.1:6379
"20"

到此這篇關(guān)于Rocky9部署redis的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Rocky9部署redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Redis分布式鎖python-redis-lock使用方法

    Redis分布式鎖python-redis-lock使用方法

    這篇文章主要介紹了Redis分布式鎖python-redis-lock使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-11-11
  • 基于redis 7.2.3的makefile源碼解讀學(xué)習

    基于redis 7.2.3的makefile源碼解讀學(xué)習

    這篇文章主要為大家介紹了基于redis 7.2.3的makefile源碼解讀學(xué)習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • 基于Redis實現(xiàn)抽獎功能及問題小結(jié)

    基于Redis實現(xiàn)抽獎功能及問題小結(jié)

    這篇文章主要介紹了基于Redis實現(xiàn)抽獎功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 單線程Redis快的4 個原因總結(jié)

    單線程Redis快的4 個原因總結(jié)

    作為內(nèi)存中數(shù)據(jù)存儲,Redis 以其速度和性能著稱,通常被用作大多數(shù)后端服務(wù)的緩存解決方案,但是,在內(nèi)部,Redis 采用單線程架構(gòu),為什么單線程設(shè)計依然會有這么高的性能,在本文中,讓我們深入探討為什么 Redis 才有單線程架構(gòu)
    2023-07-07
  • Redis 事務(wù)知識點相關(guān)總結(jié)

    Redis 事務(wù)知識點相關(guān)總結(jié)

    這篇文章主要介紹了Redis 事務(wù)相關(guān)總結(jié),幫助大家更好的理解和學(xué)習使用Redis,感興趣的朋友可以了解下
    2021-03-03
  • Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能

    Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能

    很多在windows環(huán)境中安裝Redis總是出錯,今天小編抽空給大家分享在Windows中Redis安裝配置流程并實現(xiàn)遠程訪問功能,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-06-06
  • 一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例

    一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例

    這篇文章主要介紹了一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例,需要的朋友可以參考下
    2017-04-04
  • 一篇文章帶你徹底搞懂Redis?事務(wù)

    一篇文章帶你徹底搞懂Redis?事務(wù)

    這篇文章主要介紹了一篇文章帶你徹底搞懂Redis?事務(wù)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Redis分布式緩存-Redis持久化詳解

    Redis分布式緩存-Redis持久化詳解

    RDB持久化將內(nèi)存數(shù)據(jù)快照到磁盤,用于故障恢復(fù);AOF持久化記錄每個寫命令,提供數(shù)據(jù)安全性,兩者各有優(yōu)缺點,可根據(jù)需求選擇或結(jié)合使用
    2024-12-12
  • 利用redisson快速實現(xiàn)自定義限流注解(接口防刷)

    利用redisson快速實現(xiàn)自定義限流注解(接口防刷)

    利用redis的有序集合即Sorted?Set數(shù)據(jù)結(jié)構(gòu),構(gòu)造一個令牌桶來實施限流,而redisson已經(jīng)幫我們封裝成了RRateLimiter,通過redisson,即可快速實現(xiàn)我們的目標,這篇文章主要介紹了利用redisson快速實現(xiàn)自定義限流注解,需要的朋友可以參考下
    2024-07-07

最新評論