搜尋
首頁資料庫RedisRedis步驟解析之sentinel哨兵集群

推薦學習:Redis影片教學

#一、Redis sentinel哨兵群集概述

(1)Redis哨兵概述

*Sentinel 哨兵:這是一個分散式系統,該進程是用於監控Redis叢集中Master主伺服器的工作狀態,在Master主伺服器發生故障時,可以實現Master和Slave伺服器的秒級切換,確保系統有一個Master主伺服器,提供了Redis叢集的高可用,在Reids2.6.版本時被加入,到2.8版本之後得到了穩定

Redis哨兵和Redis主從的差異:

Redis哨兵:主伺服器故障後,會有一個從伺服器取代主伺服器

##Redis主從:主伺服器故障後,從伺服器不會做任何事

Redis步驟解析之sentinel哨兵集群

#(2)Redis哨兵的工作機制

哨兵只需要部署在master主伺服器上即可

工作進程:

監控(Monitoring):哨兵透過流言協定(gossip protocols)會不斷檢查叢集中每一台伺服器是否運作正常

提醒(Notification):當哨兵監控的某個redis伺服器出現問題時,哨兵可以透過API(應用程式介面)向管理員或其他應用程式發送通知

# 自動故障轉移(Automatic failover):在叢集中如果有一個Master主伺服器發生故障時,哨兵會透過投票協定(Agreement Protocols)開始一次自動故障遷移操作,他會選擇一台資料較完整的Slave從伺服器升級為主伺服器,當客戶端試圖連接失效的Master主伺服器時,叢集也會向客戶端傳回新的Master主伺服器的位址,讓叢集可以使用現在的Master取代掉失效的Master。

Master和Slave切換後,Master的redis主設定檔、Slave的redis主設定檔和哨兵的設定檔的內容都會發生對應的改變,也就是原來的Master的redis主設定檔會多一行Slave伺服器的配置,之後哨兵的監控目標就會改變到現在的Master主伺服器上

(3)哨兵的三個定時監控任務

每隔10秒,每個Sentinel節點會向主節點和從節點發送info指令取得Redis資料節點的資訊

作用:

透過向主節點執行info指令,取得從節點的信息,這也是為什麼Sentinel節點不需要明確設定監控從節點。當有新的從節點加入時都可以立刻感知出來,當節點不可達或故障轉移後,可以透過info指令即時更新節點拓樸資訊。

每隔1秒,每個Sentinel節點會向主節點、從節點、發送一條ping指令做一次心跳偵測,來確認這些節點目前是否可達如果主節點掛掉,那麼sentinel,就會從剩餘的從節點選擇一個資料比較完整來做主節點

二、部署Redis哨兵系統

(1)實驗環境

系統ip#主機名稱Redis版本埠扮演角色Centos7.4192.168.100.202master#Redis-5.0.4Redis:6379 Sentinel:26379Master##Centos7.4Centos7.4#Redis:6379Slave
192.168.100.203 slave1 Redis-5.0.4 Redis:6379 Slave
192.168.100.204 slave2 Redis-5.0.4 Redis:6379 Slave
##### ####

(2)实验步骤 -在每台服务器上都安装Redis

安装步骤相同,主机名、ip不同,下面只写Master配置

[root@Centos7 ~]# hostnamectl set-hostname master
[root@Centos7 ~]# su
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0
setenforce: SELinux is disabled
[root@master ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
       /dev/sr0 已经挂载到 /mnt 上
[root@master ~]# ll
总用量 1928
-rw-------. 1 root root    1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 1966337 6月   9 01:16 redis-5.0.4.tar.gz
[root@master ~]# tar xf redis-5.0.4.tar.gz
[root@master ~]# cd redis-5.0.4
[root@master redis-5.0.4]# make
[root@master redis-5.0.4]# mkdir -p /usr/local/redis
[root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-server /usr/local/redis/
[root@master redis-5.0.4]# cp /root/redis-5.0.4/src/redis-cli /usr/local/redis/
[root@master redis-5.0.4]# cp /root/redis-5.0.4/redis.conf  /usr/local/redis/ 
[root@master redis-5.0.4]# vim /usr/local/redis/redis.conf   #修改
。。。。。。
  68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69 bind 192.168.100.202  #修改为本机地址,如果为127.0.0.1就只能本机访问
  70 
。。。。。。
  87 # are explicitly listed using the "bind" directive.
  88 protected-mode no  #关闭redis的保护模式,如果为yes的话其他客户端就无法连接到此服务器
  89 
。。。。。。
 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
 136 daemonize yes  #开启redis的后台守护程序,即在redis开启之后是放在后台运行的
 137 
。。。。。。
 262 # Note that you must specify a directory here, not a file name.
 263 dir /usr/local/redis/rdb
 264 
。。。。。。
 506 #
 507 requirepass 123123  #去掉注释,修改redis的密码为123123
 508 
#保存退出
[root@slave2 redis-5.0.4]# mkdir /usr/local/redis/rdb
[root@master redis-5.0.4]# vim /etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 80 90
# description: Start and Stop redis
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
AUTH="123123"
LISTEN_IP=$(netstat -utpln |grep redis-server |awk '{print $4}'|awk -F':' '{print $1}')

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -h $LISTEN_IP -p $REDISPORT -a $AUTH SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac
[root@master redis-5.0.4]# chkconfig --add redis
[root@master redis-5.0.4]# chmod 755 /etc/init.d/redis
[root@master redis-5.0.4]# ln -s /usr/local/redis/* /usr/local/bin/
[root@master redis-5.0.4]# /etc/init.d/redis start 
Starting Redis server...
5233:C 09 Jun 2021 01:25:53.069 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5233:C 09 Jun 2021 01:25:53.069 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5233, just started
5233:C 09 Jun 2021 01:25:53.069 # Configuration loaded
Redis is running...
[root@master redis-5.0.4]# netstat -anpt | grep 6379
tcp        0      0 192.168.100.202:6379    0.0.0.0:*               LISTEN      5234/redis-server 1

-做redis主从

******(1)Master配置
[root@master redis-5.0.4]# vim /usr/local/redis/redis.conf #修改
。。。。。。
 292 #
 293  masterauth 123123  #配置主服务器密码,哨兵有一个问题,就是当主服务器坏掉,切换到从服务器时,原来的主服务器可以正常运行之后,再次加入集群是加不进去的,因为哨兵没有配置主服务器的密码,所以无法连接,所以在使用哨兵集群时,要把每台的主服务器密码都配置上,每台redis的密码最好都一样
 294 
。。。。。。
 456 #
 457  min-replicas-to-write 1 #设置slave服务器的数量,当slave服务器少于这个数量时,Master主服务器会停止接收客户端的一切写请求
 458  min-replicas-max-lag 10 #设置主服务器和从服务器之间同步数据的超时时间,当超过此时间时,master主服务器会停止客户端的一切写操作,单位为秒
 459 #
。。。。。。
[root@master redis-5.0.4]# /etc/init.d/redis restart   #重启redis
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
Starting Redis server...
5291:C 09 Jun 2021 02:04:39.132 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5291:C 09 Jun 2021 02:04:39.132 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5291, just started
5291:C 09 Jun 2021 02:04:39.132 # Configuration loaded
Redis is running...

******(2)Slave1配置
[root@slave1 redis-5.0.4]# vim /usr/local/redis/redis.conf 
。。。。。。
 285 #
 286 replicaof 192.168.100.202 6379  #在从服务器上指定主服务器的ip和端口
 287 
。。。。。。
 292 #
 293 masterauth 123123  #指定主服务器上redis的密码
 294
。。。。。。
#保存退出
[root@slave redis-5.0.4]# /etc/init.d/redis restart  #重启服务
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
Starting Redis server...
5304:C 09 Jun 2021 02:11:32.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5304:C 09 Jun 2021 02:11:32.241 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5304, just started
5304:C 09 Jun 2021 02:11:32.241 # Configuration loaded
Redis is running...

******(3)Slave2配置
[root@slave2 redis-5.0.4]# vim /usr/local/redis/redis.conf 
。。。。。。
 286  replicaof 192.168.100.204 6379
 287 
 288 # If the master is password protected (using the "requirepass" configuration
 289 # directive below) it is possible to tell the replica to authenticate before
 290 # starting the replication synchronization process, otherwise the master will
 291 # refuse the replica request.
 292 #
 293  masterauth 123123
 294 
。。。。。。
#保存退出
[root@slave2 redis-5.0.4]# /etc/init.d/redis restart 
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
Starting Redis server...
5253:C 09 Jun 2021 17:50:25.680 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5253:C 09 Jun 2021 17:50:25.680 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5253, just started
5253:C 09 Jun 2021 17:50:25.680 # Configuration loaded
Redis is running...

******(3)验证主从是否成功
[root@master ~]# redis-cli -h 192.168.100.202 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.202:6379> set aaa bbb
OK
192.168.100.202:6379> set bbb ccc 
OK
192.168.100.202:6379> keys *
1) "aaa"
2) "bbb"

[root@slave1 ~]# redis-cli -h 192.168.100.203 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.203:6379> keys *
1) "bbb"
2) "aaa"
192.168.100.203:6379> set ttt fff
(error) READONLY You can't write against a read only replica.  #从服务器无法写入数据

[root@slave2 redis-5.0.4]# redis-cli -h 192.168.100.204 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.204:6379> keys *
1) "aaa"
2) "bbb"
192.168.100.204:6379> set ggg aaa
(error) READONLY You can't write against a read only replica.

#主从配置完成

-配置哨兵

******(1)在master上配置sentinel哨兵
[root@master ~]# cp redis-5.0.4/src/redis-sentinel /usr/local/redis/  #复制哨兵启动脚本
[root@master ~]# cp redis-5.0.4/sentinel.conf /usr/local/redis/  #复制哨兵配置文件
[root@master ~]# mkdir -p /var/redis/data  #创建日志文件存放位置	
[root@master ~]# vim /usr/local/redis/sentinel.conf   #修改哨兵配置文件
。。。。。。
 21 port 26379  #指定端口默认为26379
 22 
 23 # By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
 24 # Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
 25 # daemonized.
 26 daemonize yes  #yes为放在后台运行,使用no放在前台运行可以看到主从切换时候的信息
 27 
。。。。。。
 64 # unmounting filesystems.
 65 dir /var/redis/data  #指定日志存放位置,就是刚才创建的路径
 66 
。。。。。。
 83 # The valid charset is A-z 0-9 and the three characters ".-_".
 84 sentinel monitor mymaster 192.168.100.202  6379 1  #指定用户为mymaster,ip为202,端口为6379,1表示当有一台master出现故障时,就进行切换
 85 
 86 # sentinel a
。。。。。。
112 # Default is 30 seconds.
113 sentinel down-after-milliseconds mymaster 3000  #指定master的失效时间,单位为毫秒3000为3秒,表示master超过3秒没响应就判定为故障
114 
。。。。。。
145 # Default is 3 minutes.
146 sentinel failover-timeout mymaster 180000  #切换操作完成的超时时间,单位为毫秒180000为180秒,在主从切换超过这个时间就判定为切换失败
147 
148 # SCRIPTS EXE
。。。。。。
102 #
103  sentinel auth-pass mymaster 123123  #连接master和slave的密码
104 sentinel config-epoch mymaster  1  #切换后最多有多少节点可以于新的master进行同步数据
105 
#保存退出
[root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf  #启动哨兵
1118:X 09 Jun 2021 18:09:29.027 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1118:X 09 Jun 2021 18:09:29.027 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1118, just started
1118:X 09 Jun 2021 18:09:29.027 # Configuration loaded
[root@master ~]# netstat -anpt | grep 26379
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      1119/redis-sentinel 
tcp6       0      0 :::26379                :::*                    LISTEN      1119/redis-sentinel 
[root@master ~]# kill -9 1119  #先关闭哨兵
[root@master ~]# netstat -anpt | grep 26379
[root@master ~]# sed -i '26s/yes/no/g' /usr/local/redis/sentinel.conf  #修改为前台启动
[root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf  #再次开启哨兵,稍等一段时间会有提示
1129:X 09 Jun 2021 18:11:02.585 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1129:X 09 Jun 2021 18:11:02.585 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1129, just started
1129:X 09 Jun 2021 18:11:02.585 # Configuration loaded
1129:X 09 Jun 2021 18:11:02.586 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
  .-`` .-".  "\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 1129
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1129:X 09 Jun 2021 18:11:02.586 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1129:X 09 Jun 2021 18:11:02.586 # Sentinel ID is fce7776020cf12792fd239f6f9d34f2d3fdef98c
1129:X 09 Jun 2021 18:11:02.586 # +monitor master mymaster 192.168.100.202 6379 quorum 1
1129:X 09 Jun 2021 18:18:04.434 * +reboot slave 192.168.100.204:6379 192.168.100.204 6379 @ mymaster 192.168.100.202 6379  #看到新增两条消息,从服务器增加了203和204主服务器时202
1129:X 09 Jun 2021 18:18:14.478 * +reboot slave 192.168.100.203:6379 192.168.100.203 6379 @ mymaster 192.168.100.202 6379

#哨兵配置完成

-测试哨兵的故障切换

******(1)把master服务器在开启一个终端,在新开启的终端中关闭redis,测试是否可以主从切换
[root@master ~]# /etc/init.d/redis stop 
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped

******(2)切换到开启哨兵的终端,查看新弹出的信息
1129:X 09 Jun 2021 18:20:36.588 # +failover-end master mymaster 192.168.100.202 6379
1129:X 09 Jun 2021 18:20:36.588 # +switch-master mymaster 192.168.100.202 6379 192.168.100.203 6379
1129:X 09 Jun 2021 18:20:36.588 * +slave slave 192.168.100.204:6379 192.168.100.204 6379 @ mymaster 192.168.100.203 6379  #发现主服务器变成了203
1129:X 09 Jun 2021 18:20:36.588 * +slave slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379
1129:X 09 Jun 2021 18:20:39.607 # +sdown slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379‘

******(3)在203上测试主从复制是否可以正常同步
[root@slave1 ~]# redis-cli -h 192.168.100.203 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.203:6379> keys *
1) "aaa"
2) "bbb"
192.168.100.203:6379> set yyy aaa
OK
192.168.100.203:6379> keys *
1) "yyy"
2) "aaa"
3) "bbb"

[root@slave2 redis-5.0.4]# redis-cli -h 192.168.100.204 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.204:6379> keys *  #发现同步成功
1) "yyy"
2) "bbb"
3) "aaa"

******(4)此时重新开启202的redis,并且查看哨兵的提示消息
[root@master ~]# /etc/init.d/redis start 
Starting Redis server...
1167:C 09 Jun 2021 18:23:39.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1167:C 09 Jun 2021 18:23:39.756 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1167, just started
1167:C 09 Jun 2021 18:23:39.756 # Configuration loaded
Redis is running...

1129:X 09 Jun 2021 18:23:50.324 * +convert-to-slave slave 192.168.100.202:6379 192.168.100.202 6379 @ mymaster 192.168.100.203 6379   #提示增加了一台slave

******(5)在202的新终端上查看redis的数据是否成功同步
[root@master ~]# redis-cli -h 192.168.100.202 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.202:6379> keys *  #发现已经成功同步
1) "bbb"
2) "aaa"
3) "yyy"

#测试故障切换缓存,发现在master主机出现故障然后重新连接到集群后,master角色不会进行转移

-哨兵日志分析

#把哨兵放在前台运行时,日志信息会直接输出到终端上,放到后台运行时,日志会写到指定的路径中
+reset-master <instance details>   #当master被重置时.
+slave <instance details>  #当检测到一个slave并添加进slave列表时.
+failover-state-reconf-slaves <instance details> #Failover状态变为reconf-slaves状态时
+failover-detected <instance details> #当failover发生时
+slave-reconf-sent <instance details> #sentinel发送SLAVEOF命令把它重新配置时
+slave-reconf-inprog <instance details> #slave被重新配置为另外一个master的slave,但数据复制还未发生时。
+slave-reconf-done <instance details> #slave被重新配置为另外一个master的slave并且数据复制已经与master同步时。
-dup-sentinel <instance details> #删除指定master上的冗余sentinel时,当一个sentinel重新启动时,可能会发生这个事件
+sentinel <instance details> #当master增加了一个sentinel时。
+sdown <instance details> #进入SDOWN状态时;
-sdown <instance details> #离开SDOWN状态时。
+odown <instance details> #进入ODOWN状态时。
-odown <instance details> #离开ODOWN状态时。
+new-epoch <instance details>  #当前配置版本被更新时。
+try-failover <instance details> #达到failover条件,正等待其他sentinel的选举。
+elected-leader <instance details> #被选举为去执行failover的时候。
+failover-state-select-slave <instance details> #开始要选择一个slave当选新master时。
no-good-slave <instance details> #没有合适的slave来担当新master
selected-slave <instance details> #找到了一个适合的slave来担当新master
failover-state-send-slaveof-noone <instance details> #当把选择为新master的slave的身份进行切换的时候。
failover-end-for-timeout <instance details>   #failover由于超时而失败时。
failover-end <instance details> #failover成功完成时。
switch-master <master name> <oldip> <oldport> <newip> <newport> #当master的地址发生变化时。通常这是客户端最感兴趣的消息了。
+tilt #进入Tilt模式。
-tilt #退出Tilt模式。

推荐学习:Redis视频教程

以上是Redis步驟解析之sentinel哨兵集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:脚本之家。如有侵權,請聯絡admin@php.cn刪除
REDIS:流行數據結構指南REDIS:流行數據結構指南Apr 11, 2025 am 12:04 AM

Redis支持多種數據結構,具體包括:1.字符串(String),適合存儲單一值數據;2.列表(List),適用於隊列和棧;3.集合(Set),用於存儲不重複數據;4.有序集合(SortedSet),適用於排行榜和優先級隊列;5.哈希表(Hash),適合存儲對像或結構化數據。

redis計數器怎麼實現redis計數器怎麼實現Apr 10, 2025 pm 10:21 PM

Redis計數器是一種使用Redis鍵值對存儲來實現計數操作的機制,包含以下步驟:創建計數器鍵、增加計數、減少計數、重置計數和獲取計數。 Redis計數器的優勢包括速度快、高並發、持久性和簡單易用。它可用於用戶訪問計數、實時指標跟踪、遊戲分數和排名以及訂單處理計數等場景。

redis命令行怎麼用redis命令行怎麼用Apr 10, 2025 pm 10:18 PM

使用 Redis 命令行工具 (redis-cli) 可通過以下步驟管理和操作 Redis:連接到服務器,指定地址和端口。使用命令名稱和參數向服務器發送命令。使用 HELP 命令查看特定命令的幫助信息。使用 QUIT 命令退出命令行工具。

redis集群模式怎麼搭建redis集群模式怎麼搭建Apr 10, 2025 pm 10:15 PM

Redis集群模式通過分片將Redis實例部署到多個服務器,提高可擴展性和可用性。搭建步驟如下:創建奇數個Redis實例,端口不同;創建3個sentinel實例,監控Redis實例並進行故障轉移;配置sentinel配置文件,添加監控Redis實例信息和故障轉移設置;配置Redis實例配置文件,啟用集群模式並指定集群信息文件路徑;創建nodes.conf文件,包含各Redis實例的信息;啟動集群,執行create命令創建集群並指定副本數量;登錄集群執行CLUSTER INFO命令驗證集群狀態;使

redis怎麼讀取隊列redis怎麼讀取隊列Apr 10, 2025 pm 10:12 PM

要從 Redis 讀取隊列,需要獲取隊列名稱、使用 LPOP 命令讀取元素,並處理空隊列。具體步驟如下:獲取隊列名稱:以 "queue:" 前綴命名,如 "queue:my-queue"。使用 LPOP 命令:從隊列頭部彈出元素並返回其值,如 LPOP queue:my-queue。處理空隊列:如果隊列為空,LPOP 返回 nil,可先檢查隊列是否存在再讀取元素。

redis集群zset怎麼使用redis集群zset怎麼使用Apr 10, 2025 pm 10:09 PM

Redis 集群中使用 zset:zset 是一種有序集合,將元素與評分關聯。分片策略: a. 哈希分片:根據 zset 鍵的哈希值分佈。 b. 範圍分片:根據元素評分劃分為範圍,並將每個範圍分配給不同的節點。讀寫操作: a. 讀操作:如果 zset 鍵屬於當前節點的分片,則在本地處理;否則,路由到相應的分片。 b. 寫入操作:始終路由到持有 zset 鍵的分片。

redis數據怎麼清空redis數據怎麼清空Apr 10, 2025 pm 10:06 PM

如何清空 Redis 數據:使用 FLUSHALL 命令清除所有鍵值。使用 FLUSHDB 命令清除當前選定數據庫的鍵值。使用 SELECT 切換數據庫,再使用 FLUSHDB 清除多個數據庫。使用 DEL 命令刪除特定鍵。使用 redis-cli 工具清空數據。

redis過期策略怎麼設置redis過期策略怎麼設置Apr 10, 2025 pm 10:03 PM

Redis數據過期策略有兩種:定期刪除:定期掃描刪除過期鍵,可通過 expired-time-cap-remove-count、expired-time-cap-remove-delay 參數設置。惰性刪除:僅在讀取或寫入鍵時檢查刪除過期鍵,可通過 lazyfree-lazy-eviction、lazyfree-lazy-expire、lazyfree-lazy-user-del 參數設置。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器