redis订阅发布模式
好处: 异步 ,解耦 , 削峰
redis订阅发布(生产/消费)模式具体实现:
启动redis服务端:
/usr/redis/bin/redis-server /usr/redis/bin/redis.conf
启动redis客户端:
/usr/redis/bin/redis-cli -h 192.168.170.31
模拟订阅者,订阅某一个或者多个管道(订阅2个频道(管道)):
192.168.170.31:6379> subscribe channela channelb
新建会话,再启动redis一个客户端:
/usr/redis/bin/redis-cli -h 192.168.170.31
模拟发布者,发布消息到一个管道:
192.168.170.31:6379> publish channela hello1
多个发布者和订阅者:
启动多个会话 模拟多个订阅者:
/usr/redis/bin/redis-cli -h 192.168.170.31192.168.170.31:6379> subscribe channela channelb/usr/redis/bin/redis-cli -h 192.168.170.31 192.168.170.31:6379> subscribe channela channelc/usr/redis/bin/redis-cli -h 192.168.170.31192.168.170.31:6379> subscribe channelb channeld
启动多个会话 模拟多个发布者:
/usr/redis/bin/redis-cli -h 192.168.170.31192.168.170.31:6379> publish channela hello1(integer) 1192.168.170.31:6379> publish channela hello2(integer) 1/usr/redis/bin/redis-cli -h 192.168.170.31192.168.170.31:6379> publish channela hi1(integer) 2192.168.170.31:6379> publish channelb hi11(integer) 2192.168.170.31:6379> publish channelc hi111(integer) 1/usr/redis/bin/redis-cli -h 192.168.170.31192.168.170.31:6379> publish channela helloqy14811(integer) 2192.168.170.31:6379> publish channelb helloqy14822(integer) 2192.168.170.31:6379> publish channelc helloqy14833(integer) 1192.168.170.31:6379> publish channeld helloqy14844
数据库(默认分为16个库)和作用域:
启动客户端:
/usr/redis/bin/redis-cli -h 192.168.170.31
切换数据库:
select 10
在第11库订阅频道:
192.168.170.31:6379[10]> subscribe channela
发布者使用第1库和第3库 发布消息:
192.168.170.31:6379> publish channela hello1111(integer) 2 192.168.170.31:6379> select 2OK192.168.170.31:6379[2]> publish channela hello2222(integer) 2
在第1库和第3库 发送的消息,在第11库订阅中都能看到
模糊匹配订阅:
psubscribe p=pattern 模式
/usr/redis/bin/redis-cli -h 192.168.170.31
192.168.170.31:6379> psubscribe news.*
/usr/redis/bin/redis-cli -h 192.168.170.31
192.168.170.31:6379[2]> publish news.a news111(integer) 1192.168.170.31:6379[2]> publish news.a news222(integer) 1192.168.170.31:6379[2]> publish news.b news333(integer) 1192.168.170.31:6379[2]> publish news.c news444(integer) 1192.168.170.31:6379[2]> publish news.d news555(integer) 1
持久化
具体实现:
RDB方式:
vim /usr/redis/bin/redis.conf
/usr/redis/bin/redis-server /usr/redis/bin/redis.conf
AOF方式:
:699 appendonly yes 开启aof方式 默认没有开始,默认使用 rdb方式
:693 RDB和AOF方式可以同时开启,同时开启时,redis优先使用AOF方式。
:703 appendonly.aof
Redis supports three different modes:# no: don't fsync, just let the OS flush the data when it wants. Faster.从来不执行文件同步 只有当系统主动刷新数据才会。速度最快,最不安全的# always: fsync after every write to the append only log. Slow, Safest.每次写命令执行完毕都会追加命令到文件中。速度最慢,最安全的。# everysec: fsync only one time every second. Compromise.一秒钟执行一次文件同步。速度适中,安全性适中。:728-730# appendfsync alwaysappendfsync everysec# appendfsync no默认方式为 一秒钟同步一次
/usr/redis/bin/redis-server /usr/redis/bin/redis.conf
查看当前所在目录
pwd
redis 事务
Redis事务具体命令及用法
Multi: 开启事务
Exec (execute): 提交事务
Discard: 回滚事务
/usr/redis/bin/redis-server /usr/redis/bin/redis.conf
/usr/redis/bin/redis-cli -h 192.168.170.31
multi和exec用法:
192.168.170.31:6379> flushdbOK192.168.170.31:6379> set aaa 111OK192.168.170.31:6379> set bbb 222OK192.168.170.31:6379> set ccc 333OK192.168.170.31:6379> keys *1) "aaa"2) "bbb"3) "ccc"开启一个事务(相当于数据库事务中的begin trasication),总是返回O K192.168.170.31:6379> multiOK这些命令不会立即被执行, 而是被放到一个队列中192.168.170.31:6379> incr aaaQUEUED192.168.170.31:6379> incr bbbQUEUED192.168.170.31:6379> incr cccQUEUED43 # 提交事务44 commit; 当 EXEC命令被调用时, 所有队列中的命令才会被执行192.168.170.31:6379> exec1) (integer) 1122) (integer) 2233) (integer) 334192.168.170.31:6379> mget aaa bbb ccc1) "112"2) "223"3) "334"192.168.170.31:6379>
192.168.170.31:6379> mget aaa bbb ccc1) "112"2) "223"3) "334"开启一个事务192.168.170.31:6379> multiOK这些命令不会立即被执行, 而是被放到一个队列中192.168.170.31:6379> decr aaaQUEUED192.168.170.31:6379> decr bbbQUEUED192.168.170.31:6379> decr cccQUEUED事务会被放弃, 事务队列会被清空192.168.170.31:6379> discardOK查看数据,没有被修改192.168.170.31:6379> mget aaa bbb ccc1) "112"2) "223"3) "334"事务的特殊情况演示(事务中出现错误操作):192.168.170.31:6379> set ddd 'ddd'OK192.168.170.31:6379> mget aaa bbb ccc ddd1) "112"2) "223"3) "334"4) "ddd"开启一个事务192.168.170.31:6379> multiOK这些命令不会立即被执行, 而是被放到一个队列中192.168.170.31:6379> decr aaaQUEUED192.168.170.31:6379> decr bbbQUEUED 192.168.170.31:6379> decr dddQUEUED192.168.170.31:6379> decr cccQUEUED当 EXEC命令被调用时, 所有队列中的命令才会被执行192.168.170.31:6379> exec1) (integer) 1112) (integer) 2223) (error) ERR value is not an integer or out of range4) (integer) 333