springboot中各个版本的redis配置问题

本文探讨了Spring Boot不同版本间Redis配置的变化,特别是从1.3.2.RELEASE到2.0.2.RELEASE版本,包括配置属性的调整及依赖项的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在springboot中使用数据库,springboot版本为2.0.2.RELEASE,通过pom引入jar包,配置文件application.properties中的redis配置文件报错,提示例如deprecated configuration property 'spring.redis.pool.max-active',猜想应该是版本不对,发现springboot在1.4前后集成redis发生了一些变化。下面截图看下。

一、不同版本RedisProperties的区别

这是springboot版本为1.3.2RELEASE中的RedisProperties配置文件类,从图片中可以看得出来该本的redis配置文件属性有两个内部静态类分别是Pool和Sentinel,七个属性变量。例如我们想在配置文件中设置redis数据库host地址,则可以这样写

spring.redis.host=localhost    host为属性,配置连接池的最大连接数 spring.redis.pool.max-active=8

这个是redis在application.properties中springboot低版本的配置

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

下图则是springboot版本为2.0.2RELEASE中的RedisProperties配置文件类,从图中可知pool属性则被封装到了内部静态类Jedis和Lettuce中去了,这时我们要是配置连接池的最大连接数,前缀还是spring.redis,有两种途径

spring.redis.jedis.pool.max-active=8  或者 spring.redis.lettuce.pool.max-active=8
 

这个是redis在application.properties中springboot高版本的配置

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

2、maven下pom中的坐标配置

springboot版本1.4以下

<!--引入 spring-boot-starter-redis(1.4版本前)-->
<dependency>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-redis</artifactId>
	<version>1.3.2.RELEASE</version>
</dependency>

springboot版本1.4以上

<!--引入 spring-boot-starter-data-redis(1.4版本后)多了个data加个红和粗吧-->

<dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

### 如何在 Spring Boot 中配置 Redis #### 1. 添加依赖项 为了使 Spring Boot 能够支持 Redis 功能,需要引入 `spring-boot-starter-data-redis` 和 `Jedis` 或者其他客户端库作为连接器。可以在项目的 `pom.xml` 文件中添加以下 Maven 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Jedis 客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 这些依赖会自动加载必要的组件来操作 Redis 数据存储[^1]。 --- #### 2. 配置 Redis 连接参数 通过修改 `application.properties` 文件中的属性设置 Redis 的基本连接信息。以下是常见的配置选项及其含义: ```properties # Redis 主机地址,默认为本地主机 spring.redis.host=127.0.0.1 # Redis 端口号,默认为 6379 spring.redis.port=6379 # 如果有密码保护,则填写对应的密码;如果没有则留空 spring.redis.password= # 使用的数据库编号,默认为 0 spring.redis.database=0 ``` 上述配置定义了应用程序如何访问远程或者本机上的 Redis 实例[^3]。 --- #### 3. 启用缓存功能 如果计划利用 Redis 提供分布式缓存服务,在主程序启动类上加上注解 `@EnableCaching` 来激活基于注解的支持机制。例如: ```java @SpringBootApplication @EnableCaching // 开启缓存支持 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 此步骤确保应用能够识别并处理诸如 `@Cacheable`, `@CachePut`, 及 `@CacheEvict` 等用于管理数据缓存行为的关键字标记[^2]。 --- #### 4. 自定义 RedisTemplate (可选) 对于更复杂的场景可能还需要自定义序列化方式或者其他高级特性。可以通过创建 Bean 方法来自定义 `RedisTemplate` 对象实例: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(factory); return template; } } ``` 这里设置了键和值的不同序列化策略以适应不同的业务需求。 --- #### 5. 测试 Redis 操作 最后可以编写简单的单元测试验证整个流程是否正常工作: ```java @SpringBootTest public class RedisTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test public void testSetAndGet() throws Exception{ redisTemplate.opsForValue().set("test_key", "hello_redis"); System.out.println(redisTemplate.opsForValue().get("test_key")); } @Test public void testDelete(){ redisTemplate.delete("test_key"); } } ``` 以上代码片段展示了基础的数据写入读取以及删除方法调用过程。 --- ### 总结 完成上述几个部分之后就可以成功集成 Redis 到您的 Spring Boot 应用了。这不仅有助于提升系统的性能表现还能增强其扩展能力。
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独步秋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值