Redis(Redis配置和订阅发布)

发布于:2024-05-08 ⋅ 阅读:(33) ⋅ 点赞:(0)

文章目录

1.Redis配置

1.网络配置
1.配置文件位置 /etc/redis.conf
2.bind(注销支持远程访问)
1.默认情况bind = 127.0.0.1 只能接受本机的访问
2.首先编辑配置文件
vim /etc/redis.conf

image-20240429113924283

3.进入命令模式输入/bind定位,输入n查找下一个,shift + n查找上一个,找到bind注释掉

image-20240429114040993

4.重启redis,别忘记指定配置文件
redis-server /etc/redis.conf

image-20240429114202757

5.查看进程的情况,6379前面变成星号了,表示看,可以接受外部连接

image-20240429114820752

6.如果没有变成星号,就需要先杀死该进程然后在启动一下

image-20240429115010845

3.protected-mode(设置no支持远程访问)

image-20240429115616881

4.port(可以修改启动端口)

image-20240429115731835

5.timeout(一个空闲的客户端维持多少秒会超时,默认0不会超时)

image-20240429130714769

6.tcp-keepalive(对访问客户端的一种心跳检测, 默认300,建议设置60)

image-20240429131100730

2.通用配置
1.daemonize(是否以守护进程启动)
1.介绍

image-20240429131303512

2.设置为no测试

image-20240429131404508

3.保存之后杀死一下进程然后再重启,就会发现此时整个窗口都被redis占用

image-20240429132219182

4.ctrl + c退出,则服务也关闭了

image-20240429132302574

2.pidfile(记录了该进程的进程号)
1.查看pidfile的位置

image-20240429132629382

2.看看

image-20240429132716188

3.loglevel(日志级别默认notice在测试时可以使用debug)

image-20240429132909405

4.logfile(日志文件位置,默认为空即不保留日志,可自定义日志文件位置)
1.找到logfile位置

image-20240429133456181

2.设置日志文件位置

image-20240429133602109

3.创建日志文件

image-20240429133807541

4.重启redis,查看日志文件
5.databases(设置redis共有几个库默认16)

image-20240429134443668

3.安全设置
1.requirepass (设置密码)
1.配置文件找到对应位置

image-20240429134811737

2.设置密码为root

image-20240429134840493

3.重启redis

image-20240429134955436

4.再次连接redis进行操作,使用auth进行登录,用户名用默认的直接输入密码root

image-20240429135148895

5.可以使用acl whoami来查看当前用户为default

image-20240429135315627

6.使用acl list可以查看所有用户

image-20240429135404677

2.使用config get命令可以获取配置信息

image-20240429143110884

4.限制配置
1.maxclients(设置redis最大客户端连接数),如果超过连接则会报错Connection reset by peer

image-20240429143515488

2.maxmemory(最大内存)
1.基本介绍

image-20240429144115441

2.所在位置

image-20240429144326342

3.maxmemory-policy(最大内存策略)

image-20240429144947472

4.maxmemory-samples(样本数量,一般设置3到7的数字,数值越小样本越不准确,但性能消耗越小)

image-20240429145343135

2.发布订阅

1.发布和订阅是什么?
1.示意图

image-20240429145722602

image-20240429145941693

2.如何理解发布和订阅模式
1.任务队列

image-20240429150238877

2.从生活中理解

image-20240429150311100

2.发布订阅分类
1.一个发布者,多个订阅者

image-20240429150426023

2.多个发布者,一个订阅者

image-20240429150544102

3.多个发布者,多个订阅者

image-20240429150639781

3.命令行实现发布和订阅
1.命令介绍

image-20240429151515451

image-20240429151521552

2.一个发布者,多个订阅者
1.三个连接都进入redis

image-20240429152318113

2.让2和3订阅频道chanenl1和chanenl2
subscribe chanenl1 chanenl2

image-20240429152500148

3.让1发布消息到chanenl1
publish chanenl1 hello,world

image-20240429152902644

3.多个发布者,一个订阅者
1.让1和2发布

image-20240429153151172

2.让3订阅(刚才订阅了)

image-20240429153237433

4.多个发布者,多个订阅者
1.让3,4订阅

image-20240429153636146

2.让1,2发布

image-20240429153656779

3.最终3,4都接收到了

image-20240429153722727