一个Java小白的学习之路 个人博客 youngljx.top
文章目录
SpringBoot整合Redis缓存
前提:认识redis
Redis 是一个开源(BSD 许可)、内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串(String)、哈希表(Hash)、列表(List)、集合(Set)、有序集合(Zset)等数据类型。内置复制、Lua 脚本、LRU 收回、事务以及不同级别磁盘持久化功能,同时通过 Redis Sentinel 提供高可用,通过 Redis Cluster 提供自动分区。
Redis知识点详解
了解 Redis 并在 Spring Boot 项目中使用 Redis
Linux CentOS服务器安装和配置redis
安装在线安装或者下载解压或者直接使用宝塔面板安装(路径和前两种不同)
配置redis参数:
daemonize 改为yes 表示允许redis在后台启动
#bind 注释掉bind参数 表示可以外网来连接redis 远程访问
requirespass your password 设置登录redis时的密码
protected-mode no 关闭保护模式
配置CentOS 关闭防火墙 远程连接redis
1:查看防火墙状态
systemctl status firewalld
或
service iptables status
3:永久关闭防火墙
systemctl disable firewalld
或
chkconfig iptables off
启动redis,定位到redis安装目录下
前台启动: ./redis-server
后台启动: 修改daemonize参数
运行redis的时候,指定加载的配置文件redis.conf(将redis.conf复制到redis的安装目录)
./redis-server redis.conf
登录redis
redis-cli -a your password
关闭redis, 控制台输入 shutdown 再使用exit退出或者直接命令
redis-cli -p 6379 -a your password shutdown
本地使用可视化工具 Redis Desktop Manager输入ip地址和密码 远程连接redis
整合方案一:Spring Data Redis
引入redis依赖
<!-- redis依赖包 ->
<!--Spring Boot 2.x以后底层不再使用 Jedis,而是换成了 Lettuce-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--用作 Redis 对象连接池->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
参数配置
具体参数配置参考:org.springframework.boot.autoconfigure.data.redis.RedisProperties
#redis配置
#Redis服务器地址
spring.redis.host= Linux服务器IP地址
#Redis服务器连接端口
spring.redis.port=6379
#redis连接密码
spring.redis.password= your password
#Redis数据库索引(默认为0)
spring.redis.database=0
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1ms
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.lettuce.shutdown-timeout=100ms
Spring Boot 中 Redis 的自动化配置类中默认配置
RedisTemplate<Object,Object> 和StringRedisTemplate,源码如下:
如果开发者没有提供相关的 Bean ,这两个配置就会生效可以直接注入,否则不会生效。
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({
RedisOperations.class})
@EnableConfigurationProperties({
RedisProperties.class})
@Import({
LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {
"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
StringRedisTemplate就相当于RedisTemplate<String, String>的实现。源码如下:
public class StringRedisTemplate extends RedisTemplate<String, String> {
public StringRedisTemplate() {
this.setKeySerializer(RedisSerializer.string());
this.setValueSerializer(RedisSerializer.string());
this.setHashKeySerializer(RedisSerializer.string());
this.setHashValueSerializer(RedisSerializer.string());
}
public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
this();
this.setConnectionFactory(connectionFactory);
this.afterPropertiesSet();
}
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
return new DefaultStringRedisConnection(connection);
}
RedisConfig 自定义序列化的RedisTemplate模板配置
按照自动配置类RedisTemplate修改即可
自定义序列化笔记:RedisTemplate的序列化器
/**
* @description: 自定义序列化的RedisTemplate配置
*/