持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第2天,点击查看活动详情
1. redis.conf 配置文件
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
# linux服务上,若值为 no,表示一旦客户端退出,redis服务就停止,若设置为 yes,则redis-server就会是一个进程在后台一直执行
daemonize no
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
# 默认为 yes ,默认情况下,只允许本地链接,若希望其它ip的客户端链接,设置为 no
protected-mode no
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
# 设置链接的端口
port 6698
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
# 设置连接 redis 服务的密码
requirepass 15672605239@zp56
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 若指定ip,表示只有指定ip能够访问,此行注释,表示任何ip都可以访问
# bind 127.0.0.1 -::1
# bind 0.0.0.0
# redis 最大内存
maxmemory
2. 安装redis相关
- 报错:缺少 gcc 的编译环境
- 若在安装 gcc之前先执行 make 编译命令的话,会报错,安装完 gcc 后,执行make 命令
致命错误:jemalloc/jemalloc.h:没有那个文件或目录
如上错误,执行
make MALLOC=libc
- 操作命令
- make:编译命令,执行可能会报错,提示缺少gcc编译环境,使用命令安装 yum install gcc-c++
- make install:安装命令,安装完成之后,默认会在目录 /usr/local/bin 下生成可执行文件
-
若执行 make install prefix=/usr/local/myredis 指定安装的目录,会在 myredis 目录中生成可执行文件
-
- 个人习惯就是将初始的配置文件备份,然后使用备份的配置文件
- 运行命令
- ./redis-server ./myredis/redis.conf 启动redis服务的时候,使用指定的配置文件
- redis-cli 客户端密码连接: ./redis-cli -h localhost -p 6698 -a 15672605239@zp56: -h ip地址 -p 端口 -a 密码
- centos7 开启指定端口
- 查看已经开放的端口: firewall-cmd --list-ports
- 开启端口:firewall-cmd --zone=public --add-port=80/tcp --permanent 命令含义: --zone #作用域 --add-port=80/tcp #添加端口,格式为:端口/通讯协议 --permanent #永久生效,没有此参数重启后失效
- 开启端口后,重启防火墙: firewall-cmd --reload
3. redis数据类型
五种类型: String List Hash Zset Set
3.1. String 数据类型
3.1.2 典型使用场景
- 计数
由于Redis单线程的特点,我们不用考虑并发造成计数不准的问题,通过 incrby 命令,我们可以正确的得到我们想要的结果。
- 限制次数
比如登录次数校验,错误超过三次5分钟内就不让登录了,每次登录设置key自增一次,并设置该key的过期时间为5分钟后,每次登录检查一下该key的值来进行限制登录。 3.1.3 String 命令
- INCR 自增
- DCER 自减
3.2 Set 数据类型 String 类型的无序集合
3.2.1 典型使用场景 利用集合的交并集特性,比如在社交领域,我们可以很方便的求出多个用户的共同好友,共同感兴趣的领域等
3.3 Zset 数据类型 String 类型的有序集合
3.2.1 典型使用场景 和set数据结构一样,zset也可以用于社交领域的相关业务,并且还可以利用zset 的有序特性,还可以做类似排行榜的业务。
4. redis 常用命令
- save:将当前redis数据库的数据同步保存到磁盘文件
- bgsave:后台异步将当前数据库的数据保存到磁盘文件
- config set parameter:动态调整 redis 服务器的配置参数无需重启,立即生效
- config get parameter: 获取redis的配置参数
- flushAll : 清空整个redis数据库的数据
- flushdb:情况当前数据库的数据
5. redis 连接
- redis 有 jedis 与 lettuce( spring-data-redis 项目集成) 连接管理工具,包括连接池pool的管理,springboot 中集成封装了 spring-data-redis,springboot2.x 默认 lettuce 连接池;
- RedisTemplate提供了redis各种操作、异常处理及序列化;
- StringRedisTemplate 继承 RedisTemplate,StringRedisTemplate 针对 <String,String> 值
若需要连接池
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis pool 连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
spring-data-redis 引入步骤
- 引入依赖 spring-boot-starter-data-redis,如果需要管理连接池,还需要添加 commons-pool2 以依赖
- 在 application.yml 写入配置文件
spring:
redis:
host: 192.168.150.101
port: 6379
password: 123321
lettuce:
pool:
max-active: 8 # 最大连接
max-idle: 8 # 最大空闲连接
min-idle: 0 # 最小空闲连接
max-wait: 100 # 连接等待时间
- 注入 RedisTemplate
6. redis 序列化
redis默认采用的 JdkSerializationRedisSerializer 序列类进行序列化,存入数据会将数据先序列化成字节数组然后再存入Redis数据库,可读性差,内存占用较大
- 自定义 SpringDataRedis 序列化
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
// 创建Template
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置序列化工具
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// key和 hashKey采用 string序列
redisTemplate.setKeySerializer(RedisSerializer.string()); redisTemplate.setHashKeySerializer(RedisSerializer.string());
// value和 hashValue采用 JSON序列化
redisTemplate.setValueSerializer(jsonRedisSerializer); redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
尽管JSON的序列化方式可以满足我们的需求,但依然存在一些问题
为了在反序列化时知道对象的类型,JSON序列化器会将类的class类型写入json结果中,存入Redis,会带来额外的内存开销。
- StringRedisTemplate 为了节省内存空间,我们并不会使用JSON序列化器来处理value,而是统一使用String序列化器,要求只能存储String类型的key和value。当需要存储Java对象时,手动完成对象的序列化和反序列化。
Spring默认提供了一个StringRedisTemplate类,它的key和value的序列化方式默认就是String方式。省去了我们自定义RedisTemplate的过程:
RedisTemplate的两种序列化实践方案: 方案一:
- 自定义RedisTemplate
- 修改RedisTemplate的序列化器为GenericJackson2JsonRedisSerializer 方案二:
- 使用StringRedisTemplate
- 写入Redis时,手动把对象序列化为JSON
- 读取Redis时,手动把读取到的JSON反序列化为对象