Redis整理篇

编程入门 行业动态 更新时间:2024-10-07 00:27:39

<a href=https://www.elefans.com/category/jswz/34/1771249.html style=Redis整理篇"/>

Redis整理篇

Redis

  • redis整理
  • 理论知识
      • 怎么理解redis?
    • 五种数据类型
      • string
        • 新增、查询、更新、删除
          • 普通
          • 判断是否存在
          • 条件新增
          • 批量添加
        • 有效时间的key
          • 设置
          • 刷新有效时间
          • 过期时间查询
        • 原子级数增减操作
          • 新增
          • 减少
          • 指定新增
          • 指定减少
        • 迭代模糊查询key
          • keys 命令
          • scan 命令
      • set 无序集合
      • zset 有序集合
        • 新增
        • 查询
      • hash
      • list
        • 左边插入,右边弹出,查询全部
        • 右插入,左边弹出,查询全部
        • 修改
        • 阻塞弹出
        • 使用场景
      • stream
    • 大key问题
      • 寻找大key
    • dump文件的导出导入
  • 部署
    • 单节点
      • linux版
        • redis服务端下载
        • yum安装
        • 离线安装
      • wind版
        • 下载
        • 进行解压
        • redis连接客户端(wind免费版)
    • 集群(linux环境下 redis7.0.2)
      • 主从模式1主1从
        • 启动master
        • 启动slave(只读)
        • 说明
          • 参数 replica-read-only yes
      • 主从+哨兵
        • 启动1主2从
        • 启动3哨兵
        • 关闭master,应该6381成为master
      • 集群模式
        • 192.168.1.188 启动 3个节点
        • 192.168.1.136 启动 3个节点
        • 创建集群
  • 使用
    • 与springboot整合

redis整理

github
官网

理论知识

怎么理解redis?

  • 基于内存操作
  • 单线程单核cpu操作,减少了cpu多线程上下文切换时间
  • 支持5种数据类型的 k - v 集合,本质就是map集合
  • 支持两种数据落磁盘存储方式:AOF ,RDP
  • 支持集群部署

五种数据类型

string

新增、查询、更新、删除
普通
[root@localhost bin]# ./redis-cli -p 6379 -a itchun123
127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> set test 432
OK
127.0.0.1:6379> get test
"432"
127.0.0.1:6379> del test
(integer) 1
127.0.0.1:6379> get test
(nil)
127.0.0.1:6379> 
判断是否存在

若key存在返回1,否则返回0

127.0.0.1:6379> exists test 
(integer) 0
127.0.0.1:6379> exists test1
(integer) 1
127.0.0.1:6379> 
条件新增

nx: 不存在则新增,否则返回0

127.0.0.1:6379> setnx test aa
(integer) 1
127.0.0.1:6379> setnx test 123
(integer) 0
批量添加
127.0.0.1:6379> MSET test 333 test1 234
OK
127.0.0.1:6379> get test1
"234"
127.0.0.1:6379> 
有效时间的key
设置

整体命令:set key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]

  • NX: 只有当key不存在的时候,才会创建key的值value
  • XX: 只有当key存在的时候,才会创建key的值value
  • GET:如果key的值是字符串类型,则返回key的旧值,如果key的值不是字符串类型,则报错,如果key不存在,则返回null
  • EX seconds: 有效时间秒
  • PX milliseconds:有效时间毫秒
刷新有效时间

expire key seconds [NX|XX|GT|LT]

127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> expire test 20
(integer) 1
127.0.0.1:6379> PTTL test
(integer) 15929
127.0.0.1:6379> PTTL test
(integer) 14159
127.0.0.1:6379> PTTL test
(integer) 13366
127.0.0.1:6379> PTTL test
(integer) 12731
127.0.0.1:6379> PTTL test
(integer) 12037
127.0.0.1:6379> PTTL test
(integer) 10546
127.0.0.1:6379> PTTL test
(integer) 9730
127.0.0.1:6379> PTTL test
(integer) 8976
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test
(nil)
127.0.0.1:6379> 
过期时间查询

查询key的过期时间还剩余的秒数,不存在的key,返回-2,永久的key返回-1

127.0.0.1:6379> PTTL test
(integer) -1
127.0.0.1:6379>
原子级数增减操作
新增

key的值会先被初始化为0 ,然后再执行 INCR 操作,返回为1。

127.0.0.1:6379> INCR add
(integer) 1
127.0.0.1:6379> get add
"1"
127.0.0.1:6379> 
减少

key的值会先被初始化为 0 ,然后再执行DECR操作,返回为-1。

127.0.0.1:6379> decr add
(integer) 0
127.0.0.1:6379> get add
"0"
127.0.0.1:6379> 
指定新增
127.0.0.1:6379> incrby add 11
(integer) 11
127.0.0.1:6379> get add
"11"
127.0.0.1:6379>
指定减少
127.0.0.1:6379> decrby add 1
(integer) 11
127.0.0.1:6379> get add
"10"
127.0.0.1:6379>
迭代模糊查询key
keys 命令

keys patten patten为正则表达式,redis中的key越多,造成redis阻塞的时间越长

  • *:通配任意多个字符
  • ?::通配单个字符
  • []:通配括号内的某1个字符
127.0.0.1:6379> keys test*
1) "test2"
2) "test1"
127.0.0.1:6379> 
scan 命令

scan cursor [match pattern] [count count] [TYPE type] ,非阻塞的方式遍历,类似的有 sscan 、zscan、hscan

  • cursor:游标数字,从0开始,每次返回下次执行返回下次游标数字,一次全部遍历结束,也返回0
  • match pattern:正则匹配条件
  • count count :扫描哈希槽的数量,不是数据返回的条数,默认10,
  • TYPE type:返回类型
127.0.0.1:6379> scan 0
1) "7"
2)  1) "key:9"2) "key:8"3) "key:10"4) "key:1"5) "key:3"6) "key:4"7) "key:6"8) "key:11"9) "add"10) "key:2"11) "test1"
127.0.0.1:6379> scan 7
1) "0"
2) 1) "key:7"2) "key:5"3) "test2"
127.0.0.1:6379> 
127.0.0.1:6379> scan 0 match key:* count 5
1) "1"
2) 1) "key:9"2) "key:8"3) "key:10"4) "key:1"5) "key:3"6) "key:4"
127.0.0.1:6379> scan 1 match key:* count 5
1) "7"
2) 1) "key:6"2) "key:11"3) "key:2"
127.0.0.1:6379> scan 7 match key:* count 5
1) "0"
2) 1) "key:7"2) "key:5"
127.0.0.1:6379> 

set 无序集合

  • 集合中无重复数据
127.0.0.1:6379> sadd test 123 3434 43534
(integer) 3
127.0.0.1:6379> scard test #统计集合总数
(integer) 3
127.0.0.1:6379> sinter test #返回集合全部成员
1) "123"
2) "3434"
3) "43534"
127.0.0.1:6379> sismember test 123 #判断集合中是否存在key
(integer) 1
127.0.0.1:6379> expire test 20 #给集合设置有效时间
(integer) 1
127.0.0.1:6379> PTTL test #20后查询集合剩余有效时间 -2 代表集合已过期
(integer) -2
127.0.0.1:6379> sinter test #重新查询集合全部成员
(empty array)
127.0.0.1:6379> 

zset 有序集合

新增

zadd key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]

  • XX:仅更新已存在的member
  • NX:仅新增member,这个member是原先不存在的
  • LT:仅更新存在并且新的score小于原来的score的member
  • GT: 仅更新存在并且新的score大于原来的score的member。
  • CH:表示返回值是返回被修改的数量(包括:新增和更新)。默认是仅返回新增的数量
  • INCR: 当ZADD指定这个选项时,成员的操作就等同ZINCRBY命令,对成员的分数进行递增操作。
查询

zrange key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]

127.0.0.1:6379> zrange ztest 0 2
1) "12312"
2) "124"
3) "23"
127.0.0.1:6379> 

hash

存储对象,相比于string json序列化,hash存储更快,占用空间更小,节省了序列化的时间。常用于对象存储,例如购物车、商品等

127.0.0.1:6379> hmset demo id 1 name xiaoming 
OK
127.0.0.1:6379> hmget demo id
1) "1"
127.0.0.1:6379> hmset demo class 01
OK
127.0.0.1:6379> hmget demo class
1) "01"
127.0.0.1:6379> hgetall demo 
1) "id"
2) "1"
3) "name"
4) "xiaoming"
5) "class"
6) "01"
127.0.0.1:6379> hkeys demo
1) "id"
2) "name"
3) "class"
127.0.0.1:6379> hvals demo
1) "1"
2) "xiaoming"
3) "01"
127.0.0.1:6379> 

list

列表中的元素都是有序的,可以两端插入(push)和弹出(pop),可以做消息队列,可以充当栈和队列的角色

左边插入,右边弹出,查询全部
127.0.0.1:6379> lpush ldemo 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange ldemo 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> rpop ldemo 2
1) "1"
2) "2"
127.0.0.1:6379> lrange ldemo 0 -1
1) "5"
2) "4"
3) "3"
127.0.0.1:6379> 
右插入,左边弹出,查询全部
127.0.0.1:6379> rpush ldemo 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange ldemo 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> lpop ldemo 2
1) "1"
2) "2"
127.0.0.1:6379> lrange ldemo 0 -1
1) "3"
2) "4"
3) "5"
127.0.0.1:6379> 
修改

修改指定下标的元素
lset key index newValue:修改指定下标的元素

阻塞弹出

brpop

使用场景
  • 秒杀,将商品对应的数据存储在list中,使用lpop或者rpop弹出数据,在高并发的场景中,依然线程安全,不会出现超卖或者商品数量为负数;
  • 消息队列: lpush + brpop, 生产者使用lpush从列表的左边添加数据,多个消费客户端使用brpop命令阻塞式的争抢列表尾部的元素,因为是阻塞的,在多个消费者客户端的情况下保证了负载均衡和并发安全高可用

stream

大key问题

寻找大key

  1. 自带命令 ./redis-cli -p 6381 -a itchun123 --bigkeys
仅能找出6中数据类型中最大的key


2. rdb_bigkeys工具 - 不支持 rdb8 以上
下载源码:
编译生成:rdb_bigkeys文件

[root@localhost rdb_bigkeys-1.0]# ll
total 2164
-rw-r--r--. 1 root root     107 Aug 23 16:23 bigkeys.csv
-rw-rw-r--. 1 root root    6467 Apr 22  2018 comfuncs.go
-rw-r--r--. 1 root root     111 Aug 23 16:10 go.mod
-rw-r--r--. 1 root root     219 Aug 23 16:10 go.sum
-rw-rw-r--. 1 root root   35147 Apr 22  2018 LICENSE
-rw-rw-r--. 1 root root    4030 Apr 22  2018 memcallback.go
drwxrwxr-x. 3 root root      17 Apr 22  2018 misc
-rwxr-xr-x. 1 root root 2143598 Aug 23 16:10 rdb_bigkeys
-rw-rw-r--. 1 root root    2193 Apr 22  2018 rdb_bigkeys.go
-rw-rw-r--. 1 root root     960 Apr 22  2018 README.md
[root@localhost rdb_bigkeys-1.0]# 

执行命令:./rdb_bigkeys --bytes 1024 --file bigkeys_6379.csv --sep 0 --sorted --threads 4 dump6379.rdb 导出key大于1024bytes的key到excel文件

  1. rdr 工具
    下载地址:

生成json格式

[root@localhost datahome]# ./rdr-linux dump /datahome/redis7.0.2/dump.rdb

生成web

[root@localhost datahome]# ./rdr show -p 8080 /datahome/redis7.0.2/dump.rdb 
start parsing...
parse /datahome/redis7.0.2/dump.rdb  done
parsing finished, please access http://{$IP}:8080

  1. redis-rdb-tools
    源码地址:

dump文件的导出导入

参考文献地址:
github:

部署

单节点

linux版

redis服务端下载

仓库地址
官网下载
百度网盘下载(7.0.2) 提取码:q6uh
华为镜像下载

yum安装
[root@localhost datahome]# yum install gcc-c++
[root@localhost datahome]# wget .0.2.tar.gz
--2022-07-07 14:47:17--  .0.2.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2956784 (2.8M) [application/octet-stream]
Saving to: ‘redis-7.0.2.tar.gz’100%[=====================================================================================================================================================================================>] 2,956,784    293KB/s   in 10s    2022-07-07 14:47:28 (283 KB/s) - ‘redis-7.0.2.tar.gz’ saved [2956784/2956784]
[root@localhost datahome]# tar -zxvf redis-7.0.2.tar.gz
[root@localhost datahome]# cd redis-7.0.2/
[root@localhost redis-7.0.2]# ll
total 256
-rw-rw-r--.  1 root root  32015 Jun 12 21:00 00-RELEASENOTES
-rw-rw-r--.  1 root root     51 Jun 12 21:00 BUGS
-rw-rw-r--.  1 root root   5026 Jun 12 21:00 CONDUCT
-rw-rw-r--.  1 root root   2634 Jun 12 21:00 CONTRIBUTING
-rw-rw-r--.  1 root root   1487 Jun 12 21:00 COPYING
drwxrwxr-x.  7 root root    119 Jun 12 21:00 deps
-rw-rw-r--.  1 root root     11 Jun 12 21:00 INSTALL
-rw-rw-r--.  1 root root    151 Jun 12 21:00 Makefile
-rw-rw-r--.  1 root root   6888 Jun 12 21:00 MANIFESTO
-rw-rw-r--.  1 root root  22435 Jun 12 21:00 README.md
-rw-rw-r--.  1 root root 106547 Jun 12 21:00 redis.conf
-rwxrwxr-x.  1 root root    279 Jun 12 21:00 runtest
-rwxrwxr-x.  1 root root    283 Jun 12 21:00 runtest-cluster
-rwxrwxr-x.  1 root root   1578 Jun 12 21:00 runtest-moduleapi
-rwxrwxr-x.  1 root root    285 Jun 12 21:00 runtest-sentinel
-rw-rw-r--.  1 root root   1695 Jun 12 21:00 SECURITY.md
-rw-rw-r--.  1 root root  13924 Jun 12 21:00 sentinel.conf
drwxrwxr-x.  4 root root   8192 Jun 12 21:00 src
drwxrwxr-x. 11 root root    199 Jun 12 21:00 tests
-rw-rw-r--.  1 root root   3055 Jun 12 21:00 TLS.md
drwxrwxr-x.  8 root root   4096 Jun 12 21:00 utils
[root@localhost redis-7.0.2]# make && make PREFIX=/datahome/redis7.0.2 install
[root@localhost redis-7.0.2]# cp redis.conf /datahome/redis7.0.2/
[root@localhost redis-7.0.2]# mv /datahome/redis7.0.2/redis.conf /datahome/redis7.0.2/redis_6379.conf
[root@localhost redis-7.0.2]# cd ..
[root@localhost datahome]# rm -rf redis-7.0.2
[root@localhost datahome]# rm -rf redis-7.0.2.tar.gz 
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yes
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof

bin目录下脚本文件说明

redis-benchmark    # 性能测试工具,查看本地性能如何
redis-check-aof    # 修复有问题的AOF文件
redis-check-rdb    # 修复有问题的rdb文件
redis-sentinel     # redis 集群使用
redis-server       # redis服务启动命令
redis-cli          # 客服端连接
离线安装

redis服务
官网下载首页
官网下载(7.0.2)
百度网盘下载(7.0.2) 提取码:q6uh
gcc安装包 提取码:hwmj
上传文件

[root@localhost redis-7.0.2]# cd /datahome/install
[root@localhost redis-7.0.2]# tar -zxvf redis-7.0.2.tar.gz
[root@localhost redis-7.0.2]# cd redis-7.0.2
[root@localhost redis-7.0.2]# make && make PREFIX=/datahome/redis7.0.2 install
[root@localhost redis-7.0.2]# cp redis.conf /datahome/redis7.0.2/
[root@localhost redis-7.0.2]# cd /datahome
[root@localhost datahome]# rm -rf install/redis-7.0.2
[root@localhost datahome]# rm -rf install/redis-7.0.2.tar.gz 
[root@localhost datahome]# rm -rf install/gcc离线安装包
[root@localhost redis-7.0.2]# mv /datahome/redis7.0.2/redis.conf /datahome/redis7.0.2/redis_6379.conf
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yes
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof

wind版

下载

github下载

百度云下载 提取码:ddzs

进行解压


双击 【redis-server.exe】进行启动

redis连接客户端(wind免费版)

链接:
提取码:84vd
双击安装

集群(linux环境下 redis7.0.2)

主从模式1主1从

启动master
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yes
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof
[root@localhost bin]# ./redis-cli -p 6379 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6380> 
启动slave(只读)
[root@localhost datahome]# cp /datahome/redis7.0.2/redis_6379.conf /datahome/redis7.0.2/redis_6380.conf 
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6380.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesreplicaof 127.0.0.1 6379masterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6380.conf --port 6380 --pidfile /datahome/redis7.0.2/redis_6380.pid --logfile /datahome/redis7.0.2/redis_6380.log --dbfilename dump_6380.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6380.aof --replica-read-only no
[root@localhost bin]# ./redis-cli -p 6380 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_read_repl_offset:5754slave_repl_offset:5754slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:7cf4022d6b906d0728ffc1578797d4c763eb41f3master_replid2:0000000000000000000000000000000000000000master_repl_offset:5754second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:5754
127.0.0.1:6380> 
说明
参数 replica-read-only yes

说明:如果为yes,slave实例只读,如果为no,slave实例可读可写。

设置 yes:127.0.0.1:6380> set aa aa(error) READONLY You can't write against a read only replica.设置 no :  1. 即使在从节点set 值。也只能在该从节点get到值2. 从节点一旦与主节点进行一次同步,就会被主节点数据覆盖掉

主从+哨兵

启动1主2从
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yes
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof
[root@localhost bin]# ./redis-cli -p 6379 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6380> 
[root@localhost datahome]# cp /datahome/redis7.0.2/redis_6379.conf /datahome/redis7.0.2/redis_6380.conf 
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6380.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesreplicaof 127.0.0.1 6379masterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6380.conf --port 6380 --pidfile /datahome/redis7.0.2/redis_6380.pid --logfile /datahome/redis7.0.2/redis_6380.log --dbfilename dump_6380.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6380.aof --replica-read-only no
[root@localhost bin]# ./redis-cli -p 6380 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_read_repl_offset:5754slave_repl_offset:5754slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:7cf4022d6b906d0728ffc1578797d4c763eb41f3master_replid2:0000000000000000000000000000000000000000master_repl_offset:5754second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:5754
127.0.0.1:6380> 
[root@localhost datahome]# cp /datahome/redis7.0.2/redis_6380.conf /datahome/redis7.0.2/redis_6381.conf 
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6381.conf --port 6381 --pidfile /datahome/redis7.0.2/redis_6381.pid --logfile /datahome/redis7.0.2/redis_6381.log --dbfilename dump_6381.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6381.aof --replica-read-only no --replica-priority 90
[root@localhost bin]# ./redis-cli -p 6381 -a itchun123
127.0.0.1:6381> info replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:3master_sync_in_progress:0slave_read_repl_offset:262122slave_repl_offset:262122slave_priority:90slave_read_only:0replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:bf5382cd09933d7c6f0a83bad8d5edcf6f485a26master_replid2:0000000000000000000000000000000000000000master_repl_offset:262122second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:261885repl_backlog_histlen:238
127.0.0.1:6381> 
启动3哨兵
[root@localhost bin]# mkdir /datahome/redis7.0.2/26379
[root@localhost bin]# vim /datahome/redis7.0.2/sentinel_26379.confdaemonize yesport 26379pidfile /datahome/redis7.0.2/redis-sentinel_26379.piddir /datahome/redis7.0.2/26379logfile /datahome/redis7.0.2/redis-sentinel_26379.logsentinel monitor localhost 192.168.1.188 6379 2sentinel auth-pass localhost itchun123
[root@localhost redis7.0.2]# /datahome/redis7.0.2/bin/redis-sentinel /datahome/redis7.0.2/sentinel_26379.conf
[root@localhost bin]# mkdir /datahome/redis7.0.2/26380
[root@localhost bin]# vim /datahome/redis7.0.2/sentinel_26380.confdaemonize yesport 26380pidfile /datahome/redis7.0.2/redis-sentinel_26380.piddir /datahome/redis7.0.2/26379logfile /datahome/redis7.0.2/redis-sentinel_26380.logsentinel monitor localhost 192.168.1.188 6379 2sentinel auth-pass localhost itchun123
[root@localhost redis7.0.2]# /datahome/redis7.0.2/bin/redis-sentinel /datahome/redis7.0.2/sentinel_26380.conf
[root@localhost bin]# mkdir /datahome/redis7.0.2/26381
[root@localhost bin]# vim /datahome/redis7.0.2/sentinel_26381.confdaemonize yesport 26381pidfile /datahome/redis7.0.2/redis-sentinel_26381.piddir /datahome/redis7.0.2/26381logfile /datahome/redis7.0.2/redis-sentinel_26381.logsentinel monitor localhost 192.168.1.188 6379 2sentinel auth-pass localhost itchun123
[root@localhost redis7.0.2]# /datahome/redis7.0.2/bin/redis-sentinel /datahome/redis7.0.2/sentinel_26381.conf
[root@localhost bin]# ./redis-cli -p 26380
127.0.0.1:26380> info sentinel# Sentinelsentinel_masters:2sentinel_tilt:0sentinel_tilt_since_seconds:-1sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=sdown,address=127.0.0.1:6379,slaves=0,sentinels=1master1:name=localhost,status=ok,address=192.168.1.188:6379,slaves=4,sentinels=3
127.0.0.1:26380>
关闭master,应该6381成为master
[root@localhost ~]# /datahome/redis7.0.2/bin/redis-cli -p 6379 shutdown
[root@localhost ~]# /datahome/redis7.0.2/bin/redis-cli -p 6380 -a itchun123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication# Replicationrole:slavemaster_host:192.168.1.188master_port:6381master_link_status:upmaster_last_io_seconds_ago:17master_sync_in_progress:0slave_read_repl_offset:467082slave_repl_offset:467082slave_priority:100slave_read_only:0replica_announced:1connected_slaves:0master_failover_state:no-failovermaster_replid:aea7c2f4a3aa0ed33ca7d9725d0bda784a320903master_replid2:bf5382cd09933d7c6f0a83bad8d5edcf6f485a26master_repl_offset:467082second_repl_offset:463528repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:467082
127.0.0.1:6380> 
[root@localhost bin]# ./redis-cli -p 26380
127.0.0.1:26380> info sentinel# Sentinelsentinel_masters:2sentinel_tilt:0sentinel_tilt_since_seconds:-1sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=sdown,address=127.0.0.1:6379,slaves=0,sentinels=1master1:name=localhost,status=ok,address=192.168.1.188:6381,slaves=4,sentinels=3
127.0.0.1:26380>

集群模式

192.168.1.188 启动 3个节点
192.168.1.136 启动 3个节点

192.168.1.188 启动 3个节点
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6379.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6379 -a itchun123
127.0.0.1:6379> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6379> 
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6380.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6380.conf --port 6380 --pidfile /datahome/redis7.0.2/redis_6380.pid --logfile /datahome/redis7.0.2/redis_6380.log --dbfilename dump_6380.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6380.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6380.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6380 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6381> [root@localhost datahome]# vim /datahome/redis7.0.2/redis_6381.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6381.conf --port 6381 --pidfile /datahome/redis7.0.2/redis_6381.pid --logfile /datahome/redis7.0.2/redis_6381.log --dbfilename dump_6381.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6381.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6381.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6381 -a itchun123
127.0.0.1:6381> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6381> 
192.168.1.136 启动 3个节点
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6379.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6379.conf --port 6379 --pidfile /datahome/redis7.0.2/redis_6379.pid --logfile /datahome/redis7.0.2/redis_6379.log --dbfilename dump_6379.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6379.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6379.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6379 -a itchun123
127.0.0.1:6379> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6379> 
[root@localhost datahome]# vim /datahome/redis7.0.2/redis_6380.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6380.conf --port 6380 --pidfile /datahome/redis7.0.2/redis_6380.pid --logfile /datahome/redis7.0.2/redis_6380.log --dbfilename dump_6380.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6380.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6380.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6380 -a itchun123
127.0.0.1:6380> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6381> [root@localhost datahome]# vim /datahome/redis7.0.2/redis_6381.confbind 0.0.0.0daemonize yessave 900 1save 300 10save 60 10000requirepass itchun123appendonly yesmasterauth itchun123
[root@localhost datahome]# cd /datahome/redis7.0.2/bin
[root@localhost bin]# ./redis-server /datahome/redis7.0.2/redis_6381.conf --port 6381 --pidfile /datahome/redis7.0.2/redis_6381.pid --logfile /datahome/redis7.0.2/redis_6381.log --dbfilename dump_6381.rdb --dir /datahome/redis7.0.2/ --appendfilename appendonly_6381.aof --cluster-enabled yes --cluster-config-file /datahome/redis7.0.2/nodes-6381.conf --cluster-node-timeout 5000
[root@localhost bin]# ./redis-cli -p 6381 -a itchun123
127.0.0.1:6381> info replication# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:e14422d2b057b4f1559ad28d334c7fc3bab5ea39master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
127.0.0.1:6381> 
创建集群
[root@localhost bin]# ./redis-cli --cluster create 192.168.1.136:6379 192.168.1.136:6380 192.168.1.136:6381 192.168.1.188:6379 192.168.1.188:6380 192.168.1.188:6381 --cluster-replicas 1 -a itchun123
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 192.168.1.188:6381 to 192.168.1.136:6379
Adding replica 192.168.1.136:6381 to 192.168.1.188:6379
Adding replica 192.168.1.188:6380 to 192.168.1.136:6380
M: e9f1425916c6dfbe975ace64f4029385ab48ebbc 192.168.1.136:6379slots:[0-5460] (5461 slots) master
M: 4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89 192.168.1.136:6380slots:[10923-16383] (5461 slots) master
S: 8435c16f3bee6bb16da8f28d222be1b9a6990da5 192.168.1.136:6381replicates d07703ae7eb9568d99f1e8ddc6b31759e647a79c
M: d07703ae7eb9568d99f1e8ddc6b31759e647a79c 192.168.1.188:6379slots:[5461-10922] (5462 slots) master
S: e28ea5e631456b1bb8b2ee47ce56577fadd181d1 192.168.1.188:6380replicates 4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89
S: 918733e63dd8f779a461c0f810612b1cd574ab34 192.168.1.188:6381replicates e9f1425916c6dfbe975ace64f4029385ab48ebbc
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.1.136:6379)
M: e9f1425916c6dfbe975ace64f4029385ab48ebbc 192.168.1.136:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
M: 4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89 192.168.1.136:6380slots:[10923-16383] (5461 slots) master1 additional replica(s)
M: d07703ae7eb9568d99f1e8ddc6b31759e647a79c 192.168.1.188:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: 8435c16f3bee6bb16da8f28d222be1b9a6990da5 192.168.1.136:6381slots: (0 slots) slavereplicates d07703ae7eb9568d99f1e8ddc6b31759e647a79c
S: 918733e63dd8f779a461c0f810612b1cd574ab34 192.168.1.188:6381slots: (0 slots) slavereplicates e9f1425916c6dfbe975ace64f4029385ab48ebbc
S: e28ea5e631456b1bb8b2ee47ce56577fadd181d1 192.168.1.188:6380slots: (0 slots) slavereplicates 4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost bin]# ./redis-cli -c -p 6379 -a itchun123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster nodes
d07703ae7eb9568d99f1e8ddc6b31759e647a79c 192.168.1.188:6379@16379 myself,master - 0 1658320519000 4 connected 5461-10922
918733e63dd8f779a461c0f810612b1cd574ab34 192.168.1.188:6381@16381 slave e9f1425916c6dfbe975ace64f4029385ab48ebbc 0 1658320522341 1 connected
e9f1425916c6dfbe975ace64f4029385ab48ebbc 192.168.1.136:6379@16379 master - 0 1658320521340 1 connected 0-5460
e28ea5e631456b1bb8b2ee47ce56577fadd181d1 192.168.1.188:6380@16380 slave 4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89 0 1658320520538 2 connected
8435c16f3bee6bb16da8f28d222be1b9a6990da5 192.168.1.136:6381@16381 slave d07703ae7eb9568d99f1e8ddc6b31759e647a79c 0 1658320521000 4 connected
4f3ffb96be4ee5cf2e5c19765c8e43bb03661d89 192.168.1.136:6380@16380 master - 0 1658320521000 2 connected 10923-16383
127.0.0.1:6379> set aa test
-> Redirected to slot [1180] located at 192.168.1.136:6379
OK
192.168.1.136:6379> get aa
"test"
192.168.1.136:6379> 
[root@localhost bin]# ./redis-cli -c -p 6379 -a itchun123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get aa
-> Redirected to slot [1180] located at 192.168.1.136:6379
"test"
192.168.1.136:6379> 

使用

与springboot整合

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.nio.charset.StandardCharsets;@Configuration
public class RedisConfigurer {@Beanpublic RedisTemplate<String, ?> redisCacheTemplate(LettuceConnectionFactory factory) {RedisTemplate<String, ?> template = new RedisTemplate<>();// 自定义redis序列化 读出redis反序列化报错配置FastJsonRedisSerializerConfigurer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializerConfigurer<>(Object.class);FastJsonConfig fastJsonConfig = new FastJsonConfig();// fastjson把对象转换成json避免$ref SerializerFeature.DisableCircularReferenceDetect dfastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteClassName);fastJsonConfig.setCharset(StandardCharsets.UTF_8);fastJsonRedisSerializer.setFastJsonConfig(fastJsonConfig);template.setValueSerializer(fastJsonRedisSerializer);template.setHashValueSerializer(fastJsonRedisSerializer);template.setKeySerializer(new StringRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setConnectionFactory(factory);return template;}
}

更多推荐

Redis整理篇

本文发布于:2024-02-19 14:55:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1764735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!