spring-boot-starter-data-redis的一个小细节

本文介绍如何在项目中暂时停用Spring Session和Redis,并改为使用JVM内存存储Session信息。主要内容包括更改Session存储方式的配置及解决因依赖冲突导致的启动错误。

在接手一个项目时,为了方便测试要暂停springsession,redis的使用,改用在jvm'中保存。

1.改变session保存方式。


@Configuration
public class SessionConfig {
    @Bean
   /* public CookieHttpSessionStrategy cookieHttpSessionStrategy() {
        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
        defaultCookieSerializer.setCookieName("JSESSIONID");
        CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy();
        cookieHttpSessionStrategy.setCookieSerializer(defaultCookieSerializer);//重定义cookie name jsessionid
        return cookieHttpSessionStrategy;
    }*/
    public MapSessionRepository mapSessionRepository() {
    	DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
        defaultCookieSerializer.setCookieName("JSESSIONID");
        MapSessionRepository mapSessionRepository = new MapSessionRepository(new ConcurrentHashMap<>());
        return mapSessionRepository;
    }
}

2.修改配置文件

### session ###
###spring.session.store-type=redis###
#spring.session.store-type=none

注销redis集群配置
.
.
.
.
.

启动!。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

报错,redis pool 连接不上。什么鬼我根本就没有使用redis好不好。

一发查找,原来是spring-boot-starter-data-redis的鬼,在springboot中,pom中添加spring-boot-starter-data-redis依赖,在启动时会自动创建redis pool 。调用相关配置,而我们什么也没有配所以报错。

3.注释依赖

启动OK。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

spring-boot-starter-data-redisredisson-spring-boot-starter均是Spring Boot里用于和Redis交互的依赖,不过它们有着不同的特点、使用场景和区别。 ### 依赖信息 - spring-boot-starter-data-redis:包含spring-data-redis,当前基本使用的就是它 [^1]。 - redisson-spring-boot-starter:依赖配置示例如下: ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.17.7</version> <exclusions> <exclusion> <groupId>org.redisson</groupId> <artifactId>redisson-spring-data-26</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-data-21</artifactId> <version>3.17.7</version> </dependency> ``` ### 区别 - 功能层面: - spring-boot-starter-data-redis:提供基础的Redis操作,像对字符串、哈希、列表、集合等数据结构的操作;支持多种Redis客户端,例如Lettuce和Jedis。 - redisson-spring-boot-starter:在基础Redis操作之上,提供了分布式锁、分布式集合、分布式对象等分布式相关的功能,适用于复杂的分布式场景。 - 客户端层面: - spring-boot-starter-data-redis:默认采用Lettuce作为Redis客户端,也能配置成Jedis。 - redisson-spring-boot-starter:基于Redisson客户端,其具备更强大的分布式功能。 - 配置层面: - spring-boot-starter-data-redis:配置相对简单,一般在`application.properties`或者`application.yml`里配置Redis连接信息即可。 - redisson-spring-boot-starter:配置会复杂一些,需要配置Redisson的相关参数,例如分布式锁的超时时间等。 ### 使用场景 - spring-boot-starter-data-redis:适用于简单的缓存、数据存储等场景,例如将数据库查询结果缓存到Redis,以减轻数据库压力。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.junit.jupiter.api.Test; public class RedisTest { @Autowired RedisTemplate redisTemplate; @Test void contextLoads1() { redisTemplate.opsForValue().set("ok", "测试ok1"); String ok = (String) redisTemplate.opsForValue().get("ok"); System.out.println(ok); } } ``` - redisson-spring-boot-starter:适用于复杂的分布式场景,例如分布式锁、分布式集合等。比如在分布式系统中,多个服务需要对同一资源进行操作时,可使用Redisson的分布式锁来保证数据的一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值