当前位置:主页 > java教程 > Spring Boot 2.7.6整合redis

Spring Boot 2.7.6整合redis与低版本的区别

发布:2023-04-15 09:55:01 59


为网友们分享了相关的编程文章,网友黄恨真根据主题投稿了本篇教程内容,涉及到Spring Boot 2.7.6整合redis、Spring Boot 低版本redis、SpringBoot使用Redis、Spring Boot 2.7.6整合redis相关内容,已被436网友关注,下面的电子资料对本篇知识点有更加详尽的解释。

Spring Boot 2.7.6整合redis

Spring Boot 2.7.6整合redis与低版本的区别

最近在写程序的时候参考了之前写过的一篇文章spring boot整合redis中间件与热部署实现在参考方法是出现了很多问题。

对比发现是spring boot的版本问题。在上一篇文章中spring boot是2.1.8REALESE版本,最近项目变成了2.7.6版本。

在这里插入图片描述

主要有一下几点不同:

1.全新的配置方式

# 2.7.6版本
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=root
//2.1.8版本
# Redis服务器地址
spring.data.redis.host=192.168.223.128
# Redis服务器连接端口
spring.data.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.data.redis.password=root

新版本省去了data

2.JedisConnectionFactory的生成方式

//2.1.8
        @Bean
        public JedisConnectionFactory JedisConnectionFactory(){
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
            redisStandaloneConfiguration.setHostName(host);
            redisStandaloneConfiguration.setPort(port);
            redisStandaloneConfiguration.setPassword(password);
            JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
            JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
                    jedisClientConfiguration.build());
            return factory;
        }
//2.7.6
 @Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration ();
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(password);

        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfigurationBuilder = (JedisClientConfiguration.JedisClientConfigurationBuilder) JedisClientConfiguration.builder();

        JedisClientConfiguration jedisClientConfiguration = jedisClientConfigurationBuilder.build();
        return new JedisConnectionFactory(redisStandaloneConfiguration,jedisClientConfiguration);
    }

3.RedisConnectionFactory需要自动装配

    @Bean
    public RedisTemplate makeRedisTemplate(@Autowired RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        //redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

补充:SpringBoot各个版本使用Redis之间的区别

今天在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加个红和粗吧--><depen

到此这篇关于Spring Boot 2.7.6整合redis与低版本的区别的文章就介绍到这了,更多相关Spring Boot 2.7.6整合redis内容请搜索码农之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持码农之家!


参考资料

相关文章

  • spring boot中内嵌redis的使用方法示例

    发布:2022-09-12

    为网友们分享了关于spring boot的教程,这篇文章主要给大家介绍了关于spring boot中内嵌redis使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学


网友讨论