redis高级教程

news2024/9/20 14:42:46

一 关系型数据库和 NoSQL 数据库

数据库主要分为两大类:关系型数据库与 NoSQL 数据库

关系型数据库 ,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库中的数据主流的 MySQL Oracle MS SQL Server DB2 都属于这类传统数据库。
NoSQL 数据库 ,全称为 Not Only SQL ,意思就是适用关系型数据库的时候就使用关系型数据库,不适用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键值存储(memcached Redis )、永久性键值存储( ROMA Redis )、面向文档的数据库(MongoDB CouchDB )、面向列的数据库( Cassandra HBase ),每种 NoSQL 都有其特有的使用场景及优点。

为什么还要用 NoSQL 数据库呢?

主要是由于随着互联网发展,数据量越来越大,对性能要求越来越高,传统数据库存在着先天性的缺陷,即单机(单库)性能瓶颈,并且扩展困难。这样既有单机单库瓶颈,却又扩展困难,自然无法满足日益增长的海量数据存储及其性能要求,所以才会出现了各种不同的 NoSQL 产品, NoSQL 根本性的优势在于在云计算时代,简单、易于大规模分布式扩展,并且读写性能非常高

RDBMSNOSQL的特点及优缺点:

Remote Dictionary Server 简介

中文官网   https://redis.cn

什么是redis  

Redis (Remote Dictionary Server)
2009 年发布,开发者是意大利的萨尔瓦多 · 桑菲利波普( Salvatore Sanfilippo ),他本想为自己的公司开发一个用于替换MySQL 的产品 Redis ,但是没有想到他把 Redis 开源后大受欢迎,短短几年, Redis 就有了很大的用户群体,目前国内外使用的公司众多, 比如 : 阿里 , 百度 , 新浪微博 , 知乎网 ,GitHub,Twitter
Redis 是一个开源的、遵循 BSD 协议的、基于内存的而且目前比较流行的键值数据库 (key-value database),是一个非关系型数据库, redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached ,但相比 memcached redis 还提供了易扩展、高性能、具备数据持久性等功能。
Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛。

Redis特性  

  • 速度快: 10W QPS,基于内存,C语言实现
  • 单线程
  • 持久化
  • 支持多种数据结构
  • 支持多种编程语言
  • 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
  • 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
  • 主从复制
  • 支持高可用和分布式

单线程为何如此快 ?
  • 纯内存
  • 非阻塞
  • 避免线程切换和竞态消耗

 Redis应用场景

  • Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
  • 缓存:数据查询、电商网站商品信息、新闻内容
  • 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
  • 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
  • 消息队列:ELK的日志缓存、部分业务的订阅发布系统
  • 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能

 缓存的实现流程

数据更新操作流程

数据读操作流程

  Redis的安装

官方下载地址:http://download.redis.io/releases/  

 源码安装

[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz #压缩包自行下载
[root@redis-node1 ~]# ls
anaconda-ks.cfg  redis-7.4.0  redis-7.4.0.tar.gz
[root@redis-node1 ~]# yum install make gcc initscripts -y #安装编译工具

#执行编译命令
[root@redis-node1 redis-7.4.0]# make && make install

[root@redis-node1 redis-7.4.0]# cd utils/
[root@redis-node1 utils]# vim install_server.sh #讲一下内容注释掉
 #bail if this system is managed by systemd
 #_pid_1_exe="$(readlink -f /proc/1/exe)"
 #if [ "${_pid_1_exe##*/}" = systemd ]
 #then
 # echo "This systems seems to use systemd."
 # echo "Please take a look at the provided example service unit files in this     directory, and adapt and install them. Sorry!"
 # exit 1
 #fi


[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

[root@redis-node1 utils]# vim /etc/redis/6379.conf #修改以下参数
bind * -::*
protected-mode no

[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

[root@redis-node1 utils]# netstat -antlupe | grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      0          31759      7611/redis-server *
tcp6       0      0 :::6379                 :::*                    LISTEN      0          31760      7611/redis-server *

[root@redis-node1 utils]# redis-cli
127.0.0.1:6379>

四 Redis 主从复制

环境配置

redis-node1 master
redis-node2 slave
redis-node3 slave
在配置多台 redis 时建议用复制的方式节省编译时间

配置主从同步 

1.修改mastser节点的配置文件

[root@redis-node1 bin]# vim /etc/redis/6379.conf

protected-mode no #关闭protected模式

[root@redis-node1 utils]# /etc/init.d/redis_6379 restart

2.配置slave节点

[root@redis-node2 bin]# vim /etc/redis/6379.conf

protected-mode no #关闭protected模式
replicaof 172.25.254.10 6379

[root@redis-node2 utils]# /etc/init.d/redis_6379 restart

[root@redis-node3 bin]# vim /etc/redis/6379.conf

protected-mode no #关闭protected模式
replicaof 172.25.254.100 6379

[root@redis-node3 utils]# /etc/init.d/redis_6379 restart

3.测试效果  

[root@redis-node1 bin]# redis-cli
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.20,port=6379,state=online,offset=2636,lag=1
slave1:ip=172.25.254.30,port=6379,state=online,offset=2636,lag=0
master_failover_state:no-failover
master_replid:2aac666a85461a4af445c6f48397a074ea2a500e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2636
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2636

127.0.0.1:6379> set name zhangsan
OK

[root@redis-node2 utils]# redis-cli
127.0.0.1:6379> get name
"zhangsan"

[root@redis-node3 utils]# redis-cli
127.0.0.1:6379> get name
"zhangsan"

主从同步过程

  • slave节点发送同步亲求到master节点
  • slave节点通过master节点的认证开始进行同步
  • master节点会开启bgsave进程发送内存rbdslave节点,在此过程中是异步操作,也就是说 master节点仍然可以进行写入动作
  • slave节点收到rdb后首先清空自己的所有数据
  • slave节点加载rdb并进行数据恢复
  • masterslave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存
  • 然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到
slave

五 Redis的哨兵(高可用)

 Redis哨兵

Sentinel 进程是用于监控 redis 集群中 Master 主服务器工作的状态,在 Master 主服务器发生故障的时候,可以实现Master Slave 服务器的切换,保证系统的高可用,此功能在 redis2.6+ 的版本已引用, Redis 的哨兵模式到了2.8 版本之后就稳定了下来。一般在生产环境也建议使用 Redis 2.8 版本的以后版本。

每个哨兵 (Sentinel) 进程会向其它哨兵 (Sentinel) Master Slave 定时发送消息,以确认对方是否 ”着,如果发现对方在指定配置时间( 此项可配置 ) 内未得到回应,则暂时认为对方已离线,也就是所谓的 ”主观认为宕机” ( 主观 : 是每个成员都具有的独自的而且可能相同也可能不同的意识 ) ,英文名称:Subjective Down,简称 SDOWN
有主观宕机,对应的有客观宕机。当 哨兵群 中的多数 Sentinel 进程在对 Master 主服务器做出 SDOWN 的判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的 Master Server 下线判断,这种方式就是“ 客观宕机 ”( 客观 : 是不依赖于某种意识而已经实际存在的一切事物 ) ,英文名称是:Objectively Down, 简称 ODOWN
通过一定的 vote 算法,从剩下的 slave 从服务器节点中,选一台提升为 Master 服务器节点,然后自动修改相关配置,并开启故障转移(failover
Sentinel 机制可以解决 master slave 角色的自动切换问题,但单个 Master 的性能瓶颈问题无法解决 , 类似于MySQL 中的 MHA 功能
Redis Sentinel 中的 Sentinel 节点个数应该为大于等于 3 且最好为奇数
sentinel 中的三个定时任务
  • 10秒每个sentinelmasterslave执行info         
  • 发现slave节点
  • 确认主从关系
  • 2秒每个sentinel通过master节点的channel交换信息(pub/sub)
  • 通过sentinel__:hello频道交互
  • 交互对节点的看法和自身信息
  • 1秒每个sentinel对其他sentinelredis执行pi

哨兵的实验过程

在所有阶段中关闭 protected-mode no

1.master节点中

#编辑配置文件
[root@redis-node1 ~]# cd redis-7.4.0/
[root@redis-node1 redis-7.4.0]# cp sentinel.conf /etc/redis/
[root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf
protected-mode no #关闭保护模式
port 26379 #监听端口
daemonize no #进入不打如后台
pidfile /var/run/redis-sentinel.pid #sentinel进程pid文件
loglevel notice #日志级别
sentinel monitor mymaster 172.25.254.100 6379 2 #创建sentinel监控监控master主
机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 10000 #master中断时长,10秒连不上视为
master下线
sentinel parallel-syncs mymaster 1 #发生故障转移后,同时开始同步新
master数据的slave数量
sentinel failover-timeout mymaster 180000 #整个故障切换的超时时间为3分钟
####复制配置文件到其他阶段
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.20:/etc/redis/

[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.30:/etc/redis/

 2 启动服务

[root@redis-node1 redis]# redis-sentinel /etc/redis/sentinel.conf
8186:X 08 Sep 2024 18:28:42.695 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
8186:X 08 Sep 2024 18:28:42.695 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8186:X 08 Sep 2024 18:28:42.695 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=8186, just started
8186:X 08 Sep 2024 18:28:42.695 * Configuration loaded
8186:X 08 Sep 2024 18:28:42.696 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8186:X 08 Sep 2024 18:28:42.696 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis Community Edition
  .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 8186
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

8186:X 08 Sep 2024 18:28:42.698 * Sentinel new configuration saved on disk
8186:X 08 Sep 2024 18:28:42.699 * Sentinel ID is 6f61c9c00908c018579564cacb1b87ce1819ae39
8186:X 08 Sep 2024 18:28:42.699 # +monitor master mymaster 172.25.254.10 6379 quorum 2
8186:X 08 Sep 2024 18:28:42.699 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
8186:X 08 Sep 2024 18:28:42.701 * Sentinel new configuration saved on disk
8186:X 08 Sep 2024 18:28:42.701 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
8186:X 08 Sep 2024 18:28:42.702 * Sentinel new configuration saved on disk
8186:X 08 Sep 2024 18:29:05.029 * +sentinel sentinel 4a69b2501e30133613ef79a691fb79e469987a2a 172.25.254.20 26379 @ mymaster 172.25.254.10 6379
8186:X 08 Sep 2024 18:29:05.031 * Sentinel new configuration saved on disk
8186:X 08 Sep 2024 18:29:15.502 * +sentinel sentinel b6de2cc3781faf822bf6a89b9c8d70e5b9a2d0c9 172.25.254.30 26379 @ mymaster 172.25.254.10 6379
8186:X 08 Sep 2024 18:29:15.504 * Sentinel new configuration saved on disk
8186:X 08 Sep 2024 18:30:03.421 # +sdown master mymaster 172.25.254.10 6379
8186:X 08 Sep 2024 18:30:03.457 * Sentinel new configuration saved on disk

注意:/etc/redis/sentinel.conf 文件在用哨兵程序调用后会更改其配置文件,如果需要重新做需要删掉文件重新编辑

测试:

[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.20,port=6379,state=online,offset=31425,lag=0
slave1:ip=172.25.254.30,port=6379,state=online,offset=31425,lag=0
master_failover_state:no-failover
master_replid:f5747c81b81bc83acf68a269251225721fbf810e
master_replid2:f289a3b72a069a75915690ba0f102e8f08b7a074
master_repl_offset:31707
second_repl_offset:23776
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:23776
repl_backlog_histlen:7932
127.0.0.1:6379> SHUTDOWN
(0.92s)

[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.25.254.30,port=6379,state=online,offset=44414,lag=0
master_failover_state:no-failover
master_replid:988d209809c7e1980bb24f83f53964a750e0cf81
master_replid2:f5747c81b81bc83acf68a269251225721fbf810e
master_repl_offset:44555
second_repl_offset:34875
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:44541

在整个架构中可能会出现的问题

问题:
在生产环境中如果 master slave 中的网络出现故障,由于哨兵的存在会把 master 提出去
当网络恢复后, master 发现环境发生改变, master 就会把自己的身份转换成 slave
master 变成 slave 后会把网络故障那段时间写入自己中的数据清掉,这样数据就丢失了。
解决:
master 在被写入数据时会持续连接 slave mater 确保有 2 slave 可以写入我才允许写入
如果 slave 数量少于 2 个便拒绝写入。
#在master中配置
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> config get min-slaves-to-write
1) "min-slaves-to-write"
2) "0"
127.0.0.1:6379> config set min-slaves-to-write 2
OK
127.0.0.1:6379> config get min-slaves-to-write
1) "min-slaves-to-write"
2) "2"
##################################以上为临时修改

#永久修改
[root@redis-node2 ~]# vim /etc/redis/6379.conf
min-slaves-to-write 2

 六 Redis Cluster(无中心化设计)

Redis Cluster 工作原理

在哨兵 sentinel 机制中,可以解决 redis 高可用问题,即当 master 故障后可以自动将 slave 提升为 master, 从而可以保证redis 服务的正常使用,但是无法解决 redis 单机写入的瓶颈问题,即单机 redis 写入性能受限于单机的内存大小、并发数量、网卡速率等因素。
redis 3.0 版本之后推出了无中心架构的 redis cluster 机制,在无中心的 redis 集群当中,其每个节点保存当前节点数据和整个集群状态, 每个节点都和其他所有节点连接。
Redis Cluster特点如下
1. 所有Redis节点使用(PING机制)互联
2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node承担16384/N个槽位
5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。
Redis cluster 架构
假如三个主节点分别是: A, B, C 三个节点,采用哈希槽 (hash slot) 的方式来分配 16384 slot 的话它们
三个节点分别承担的 slot 区间可以是:
节点 A 覆盖 0 5460
节点 B 覆盖 5461 10922
节点 C 覆盖 10923 16383

 

Redis cluster 主从架构
Redis cluster 的架构虽然解决了并发的问题,但是又引入了一个新的问题,每个 Redis master 的高可用如何解决?
那就是对每个 master 节点都实现主从复制 , 从而实现 redis 高可用性

 Redis Cluster 部署架构说明

创建redis cluster的前提 

1. 每个 redis node 节点采用相同的硬件配置、相同的密码、相同的 redis 版本。
2. 每个节点必须开启的参数
  • cluster-enabled yes   #必须开启集群状态,开启后redis进程会有cluster显示
  • cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要任何手动操作
3. 所有 redis 服务器必须没有任何数据
4. 先启动为单机 redis 且没有任何 key value

 部署redis cluster

在所有 redis 主机中
[root@redis-node1 ~]# vim /etc/redis/redis.conf #修改以下参数
bind * -::*
masterauth "123456" #集群主从认证

requirepass "123456" #redis登陆密码 redis-cli 命令连接redis后要用“auth 密码”进行认证

cluster-enabled yes #开启cluster集群功能

cluster-config-file nodes-6379.conf #指定集群配置文件

cluster-node-timeout 15000 #节点加入集群的超时时间单位是ms

[root@redis-node1 ~]# systemctl restart redis.service

[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info

#其他主机均要配置以上配置(简便方案)
[root@redis-node1 ~]# for i in 20 30 110 120 130 ;do scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf ; done
#再重启服务

redis-cli --cluster 参数说明

[root@redis-node1 ~]# redis-cli  --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN #创建集群
--cluster-replicas <arg> #指定master的副本数

check <host:port> or <host> <port> #检测集群信息

info <host:port> or <host> <port> #查看集群信息

fix <host:port> or <host> <port> #修复集群

reshard <host:port> or <host> <port> #在线热迁移集群指定主机的slots数据

rebalance <host:port> or <host> <port> #平衡各集群主机的slot数量

add-node new_host:new_port existing_host:existing_port #添加主机

del-node host:port node_id #删除主机

import host:port #导入外部redis服务器的数据到
当前集群

创建redis-cluster

[root@redis-node1 ~]# redis-cli  --cluster  create -a 123456 \
> 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \
> 172.25.254.110:6379 172.25.254.120:6379 172.25.254.130:6379 \
> --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460 #哈希槽分配
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.25.254.120:6379 to 172.25.254.10:6379 #主从分配情况
Adding replica 172.25.254.130:6379 to 172.25.254.20:6379
Adding replica 172.25.254.110:6379 to 172.25.254.30:6379
M: c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
M: 5614ce1e5d6c25524126130864b7eea815c373a0 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
M: f2a85df5b5355b3fd7ded898552b8bbfd2c2f925 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
S: adf52933b7434c129b81292b47103e6b1eab23fb 172.25.254.110:6379
   replicates f2a85df5b5355b3fd7ded898552b8bbfd2c2f925
S: b3f4291d1a2313d3a9907bf65e8287f488292c66 172.25.254.120:6379
   replicates c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea
S: aee197141809a2f031f7e952d504c6aa9dd0c0c3 172.25.254.130:6379
   replicates 5614ce1e5d6c25524126130864b7eea815c373a0
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 172.25.254.10:6379)
M: c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: aee197141809a2f031f7e952d504c6aa9dd0c0c3 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 5614ce1e5d6c25524126130864b7eea815c373a0
S: b3f4291d1a2313d3a9907bf65e8287f488292c66 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea
M: f2a85df5b5355b3fd7ded898552b8bbfd2c2f925 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: adf52933b7434c129b81292b47103e6b1eab23fb 172.25.254.110:6379
   slots: (0 slots) slave
   replicates f2a85df5b5355b3fd7ded898552b8bbfd2c2f925
M: 5614ce1e5d6c25524126130864b7eea815c373a0 172.25.254.20:6379
   slots:[5461-10922] (5462 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.    #所有槽位分配完成

 检测redis集群状态

[root@redis-node1 ~]# redis-cli -a 123456 --cluster info 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (c0b144b6...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.30:6379 (f2a85df5...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (5614ce1e...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@redis-node1 ~]# redis-cli -a 123456 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.10:6379 (c0b144b6...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.30:6379 (f2a85df5...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (5614ce1e...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: aee197141809a2f031f7e952d504c6aa9dd0c0c3 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 5614ce1e5d6c25524126130864b7eea815c373a0
S: b3f4291d1a2313d3a9907bf65e8287f488292c66 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea
M: f2a85df5b5355b3fd7ded898552b8bbfd2c2f925 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: adf52933b7434c129b81292b47103e6b1eab23fb 172.25.254.110:6379
   slots: (0 slots) slave
   replicates f2a85df5b5355b3fd7ded898552b8bbfd2c2f925
M: 5614ce1e5d6c25524126130864b7eea815c373a0 172.25.254.20:6379
   slots:[5461-10922] (5462 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.
写入数据
[root@redis-node1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name haha
(error) MOVED 5798 172.25.254.20:6379 #被分配到172.25.254.20的hash槽位上

[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379>  set name haha
OK

集群扩容

添加节点的时候是先添加 node 节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的Redis node 上的槽位迁移到集群中的其他 Redis node 节点上,然后再将其删除,如果一个Redis node 节点上的槽位没有被完全迁移,删除该 node 的时候会提示有数据且无法删除。
#添加master
[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.50:6379 172.25.254.20:6379

#分配槽位
[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.25.254.20:6379)
M: 5614ce1e5d6c25524126130864b7eea815c373a0 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: c2ee7a506d8cc97b8f1b3b697d30aae90d51890a 172.25.254.50:6379
   slots: (0 slots) master
M: f2a85df5b5355b3fd7ded898552b8bbfd2c2f925 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: aee197141809a2f031f7e952d504c6aa9dd0c0c3 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 5614ce1e5d6c25524126130864b7eea815c373a0
S: b3f4291d1a2313d3a9907bf65e8287f488292c66 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea
S: adf52933b7434c129b81292b47103e6b1eab23fb 172.25.254.110:6379
   slots: (0 slots) slave
   replicates f2a85df5b5355b3fd7ded898552b8bbfd2c2f925
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? c2ee7a506d8cc97b8f1b3b697d30aae90d51890a
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all

#添加salve
[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.150:6379 172.25.254.20:6379  --cluster-slave --cluster-master-id  5614ce1e5d6c25524126130864b7eea815c373a0

#查看集群信息
[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.20:6379          Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.20:6379 (5614ce1e...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.50:6379 (c2ee7a50...) -> 1 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (f2a85df5...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.10:6379 (c0b144b6...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.20:6379)
M: 5614ce1e5d6c25524126130864b7eea815c373a0 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 4325081cf2646880c6848decfda9a87f01068e97 172.25.254.150:6379
   slots: (0 slots) slave
   replicates c2ee7a506d8cc97b8f1b3b697d30aae90d51890a
M: c2ee7a506d8cc97b8f1b3b697d30aae90d51890a 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: f2a85df5b5355b3fd7ded898552b8bbfd2c2f925 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: aee197141809a2f031f7e952d504c6aa9dd0c0c3 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 5614ce1e5d6c25524126130864b7eea815c373a0
S: b3f4291d1a2313d3a9907bf65e8287f488292c66 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c0b144b6e29ea811040dd9e67a5b6ea8b8a1f5ea
S: adf52933b7434c129b81292b47103e6b1eab23fb 172.25.254.110:6379
   slots: (0 slots) slave
   replicates f2a85df5b5355b3fd7ded898552b8bbfd2c2f925
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

clsuter集群维护

#clave节点没有数据,可以直接删除
[root@redis-node2 ~]# redis-cli  -a 123456 --cluster del-node 172.25.254.150:6379  4325081cf2646880c6848decfda9a87f01068e97

#移除要下线主机的哈希槽位(master节点上)
[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20:6379 
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 5614ce1e5d6c25524126130864b7eea815c373a0
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: c2ee7a506d8cc97b8f1b3b697d30aae90d51890a #移除节点的id
Source node #2: done

[root@redis-node2 ~]# redis-cli  -a 123456 --cluster del-node 172.25.254.50:6379 c2ee7a506d8cc97b8f1b3b697d30aae90d51890a #再删除节点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2132335.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于SpringBoot+Vue+MySQL的美术馆管理系统

系统展示 用户前台界面 管理员后台界面 系统背景 随着文化艺术产业的蓬勃发展&#xff0c;美术馆作为展示与传播艺术的重要场所&#xff0c;其管理工作变得日益复杂。为了提升美术馆的运营效率、优化参观体验并加强艺术品管理&#xff0c;我们开发了基于SpringBootVueMySQL的美…

树莓派!干农活!

农作物种植是一个需要精准操作的行业&#xff0c;而农业的长期趋势是朝着机械化方向发展。Directed Machines公司的土地护理机器人&#xff08;Land Care Robot&#xff09;&#xff0c;基于Raspberry Pi4和RP2040构建&#xff0c;是解放稀缺人力资本的一种经济高效方式。 Dir…

墨西哥海外仓市场如何?如何选择墨西哥海外仓系统?

随着全球电商市场的迅猛发展&#xff0c;墨西哥作为拉美地区的重要市场&#xff0c;其电商增速在2023年高达24.6%&#xff0c;位居世界第一&#xff0c;这一数据无疑展示了墨西哥电商市场的巨大潜力和繁荣景象。 作为拉美地区最大的电商平台&#xff0c;美客多在墨西哥市场的扩…

iPhone 16分辨率,屏幕尺寸,PPI 详细数据对比 iPhone 16 Plus、iPhone 16 Pro、iPhone 16 Pro Max

史上最全iPhone 机型分辨率&#xff0c;屏幕尺寸&#xff0c;PPI详细数据&#xff01;已更新到iPhone 16系列&#xff01; 点击放大查看高清图 &#xff01;

传承中华文脉·弘扬北疆文化“四季内蒙古演出季”区内外文艺院团交流演出活动即将启动

为推进“北疆文化”品牌建设&#xff0c;由内蒙古自治区文化和旅游厅、呼和浩特市人民政府主办&#xff0c;呼和浩特市文化旅游广电局承办的传承中华文脉弘扬北疆文化——“四季内蒙古演出季”区内外文艺院团交流演出活动将于9月14日至11月期间在呼和浩特市举办。 传承中华文脉…

新书推荐:《智人之上:AI时代的信息网络简史》——尤瓦尔·赫拉利的深刻哲学警示

导言&#xff1a;AI革命的到来与历史性的深思 随着人工智能&#xff08;AI&#xff09;的快速发展&#xff0c;越来越多的学者、科学家和哲学家开始反思AI带来的潜在威胁与机遇。以色列著名历史学家尤瓦尔赫拉利&#xff08;Yuval Noah Harari&#xff09;&#xff0c;以其广受…

用Kimi输出流程图

1.输入 我希望设计一个ERP系统&#xff0c;请帮我简单列一个流程图&#xff0c;用mermaid输出2.输出

电脑重装系统后硬盘数据可以恢复吗?系统重装后以前的文件怎么找回来?

重装系统是指对电脑的操作系统例如Windows重新安装。系统重装可以解决各种系统问题&#xff0c;例如电脑感染病毒、系统文件受损、系统变慢、崩溃无法启动、蓝屏等。正常的重装系统操作是将原来的系统分区&#xff08;通常是C盘&#xff09;格式化&#xff0c;然后再重新安装Wi…

opencv学习:calcHist 函数绘制图像直方图及代码实现

cv2.calcHist 函数是 OpenCV 库中用于计算图像直方图的函数。直方图是一种统计图像中像素值分布的工具&#xff0c;它可以提供图像的亮度、颜色等信息。这个函数可以用于灰度图像和彩色图像。 函数语法 hist cv2.calcHist(images, channels, mask, histSize, ranges, accumu…

IO中断原理浅析

目录 什么是中断 什么是IO中断 无中断的情况 有中断的情况 什么是中断 中断是指&#xff0c;在程序运行过程中&#xff0c;系统出现一个必须由CPU立即处理的情况&#xff0c;此时CPU暂时中止程序的执行转而处理这个新情况的过程叫做中断。 什么是IO中断 I/O中断通过中断处理…

操作系统的重点笔记-1

一、操作系统的设计目标 1.易用性 使计算机易于使用&#xff0c;提供文件抽象后&#xff0c;对文件的操作就是对磁盘的操作&#xff0c;不再需要考虑如何通过控制磁盘移动&#xff0c;实现对磁盘某个信号的读写细节 2.高效性 完成特定功能的效率&#xff0c;如时间效率&…

音视频开发之旅(93)-图像超分增强之Real-ESRGAN

目录 1、背景和问题 2、高清-低清 数据集构建 3、Real-ESRGAN模型结构 4、源码分析 5、不足与局限性 6、资料 一、背景和问题 图像超分一直是一个活跃的研究课题&#xff0c;旨在从低分辨率&#xff08;LR&#xff09;重建高分辨率&#xff08;HR&#xff09;图像。在数…

中秋节了,送大家一个月饼

按F12&#xff0c;直接在浏览器控制台输入下面代码&#xff0c;你就会得到下面的月饼 console.log("%c\uD83E\uDD6E","font-size: 20em")

pandas中基于范围条件进行表连接

来自&#xff1a;Python大数据分析 费弗里 表连接是我们日常开展数据分析过程中很常见的操作&#xff0c;在pandas中基于join()、merge()等方法&#xff0c;可以根据左右表连接依赖字段之间对应值是否相等&#xff0c;来实现常规的表连接。 但在有些情况下&#xff0c;我们可能…

JDBC与MyBatis:数据库访问技术的变迁【后端 15】

JDBC与MyBatis&#xff1a;数据库访问技术的变迁 JDBC的基本使用 Java Database Connectivity (JDBC) 是Java提供的一种标准API&#xff0c;用于与数据库进行交互。它提供了一系列的接口和类&#xff0c;使得开发人员能够直接使用Java代码来编写SQL语句并执行数据库操作。JDBC…

Sequential的使用和搭建实战

一、Sequential 是什么 Sequential 主要出现在 Keras 库中&#xff0c;这是一个用于构建和训练深度学习模型的高级 API。Sequential 类允许你按顺序构建神经网络模型&#xff0c;其中每一层都按照给定的顺序逐层堆叠。这种模型适用于大多数线性堆叠的神经网络结构。Sequential…

GEE 迭代删除谷歌资产文件夹

在Google Earth Engine (GEE) 中管理大量地理空间数据时&#xff0c;我们可能会遇到需要清理不再需要的资产的情况。但需要提前删除子文件后才可删除文件夹&#xff0c;才可释放存储空间&#xff0c;删除过时的数据。本文将介绍如何在GEE中迭代删除资产文件夹。 代码详解 以下…

3个方法教大家如何在Excel表格中添加水印

在Excel表格中添加水印是一种常见的需求&#xff0c;但是Excel并没有像word文档一样的直接添加水印的功能&#xff0c;怎么办&#xff1f; 今天小编来分享一个方法&#xff0c;也能实现Excel表格的添加水印~ 一、使用文本框插入文字水印 1、插入选项卡 进入excel的操作界面&…

Google大数据架构技术栈

数据存储层 Colossus Colossus作为Google下一代GFS&#xff08;Google File System&#xff09;。 GFS本身存在一些不足 单主瓶颈 GFS 依赖单个主节点进行元数据管理&#xff0c;随着数据量和访问请求的增长&#xff0c;出现了可扩展性瓶颈。想象一下&#xff0c;只有一位…

攻防世界 CTF Pwn(一)

前言 攻防世界是一个专注于网络安全的在线学习和竞赛平台&#xff0c;由赛宁网安推出&#xff0c;旨在为网络安全爱好者提供丰富的学习资源和实战竞赛环境。该平台自2018年9月推出以来&#xff0c;已经吸引了超过18万用户注册使用&#xff0c;月活跃用户超过5万。 平台的主要…