spring session整合

花了大半天时间,解决了springMVC项目增加spring-session共享session报了异常

java.lang.ClassNotFoundException:com.lambdaworks.redis.AbstractRedisClient
java.lang.ClassNotFoundException:com.lambdaworks.redis.RedisException

前情

项目做了前后端分离,springMVC项目部署在三台tomcat上,前端部署在另三台tomcat上,然后HA做了分发处理,使一个用户访问后,后面的访问都会是其中的某一台tomcat... 理想是美好的,现实中,会出现登录前后会有连接是访问了不同的tomcat,我没有运维权限,也不想接这个坑,所以就从代码上处理吧。
代码上怎么处理呢,我觉得很简单,session共享不就行了,使用spring-session超简单,几行代码就搞定了。注意:这里的超简单是在spring-boot中使用才超简单。spring-boot中使用redis共享session配置:
1、pom.xml加依赖

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

2、application.properties加配置:

spring.session.store-type=redis

搞定
但是,我的项目不是spring-boot项目,所以参考官网说的配吧。我的部署环境jdk是1.7、tomcat7,所以sprint-session 2.0.x是肯定不能支持了,所以还是使用1.3.3吧,官网手册
很简单,有web.xml,所以配置分2步:
1、 在spring-context.xml中加上配置:

<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<bean class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"/>

2、在web.xml加上

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/*.xml
    </param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

当然,要在pom.xml上加上依赖包

<dependency>
    <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.0.RELEASE</version>
        </dependency>

然后...
就是报了异常

java.lang.ClassNotFoundException:com.lambdaworks.redis.AbstractRedisClient

java.lang.ClassNotFoundException:com.lambdaworks.redis.RedisException

我知道是会有版本兼容的问题,所以不断切换版本试试看,然后就试了大半天,结果再一仔细看配置:知道com.lambdaworks是哪个包不:io.lettuce。所以的,其实是因为我使用的是jedis包,所以根本不需要这一行代码

<bean class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"/>

解决

因为我使用的是jedis包,而不是也不需要用到io.lettuce包,所以在spring-context.xml中配置应该是这样的:

    <!-- Redis 线程池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxActive}" />
        <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <bean id="jedisConnFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostname}" />
        <property name="password" value="${redis.password}" />
        <property name="port" value="${redis.port}" />
        <property name="usePool" value="${redis.usePool}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <!-- spring-session共享支持 -->
    <context:annotation-config/>
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />
Spring SessionSpring框架提供的一个应用程序级会话管理解决方案。它为应用程序提供了一种无状态的方式来管理用户会话。Spring Session提供了多种后端存储方式来存储用户会话信息,其中Redis是其中的一个存储后端。 Spring Session与Redis整合可以使得应用程序的会话信息存储在Redis中,从而可以实现分布式应用程序的会话管理。下面是Spring Session与Redis整合的步骤: 1. 添加Spring Session和Redis的依赖: ```xml <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 配置Redis连接信息: ```java @Configuration @EnableRedisHttpSession public class RedisSessionConfig { @Bean public LettuceConnectionFactory connectionFactory() { return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379)); } } ``` 3. 启用Spring Session: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public HttpSessionIdResolver httpSessionIdResolver() { return HeaderHttpSessionIdResolver.xAuthToken(); } } ``` 4. 在需要使用Spring Session的Controller或Service中,注入HttpSession对象即可: ```java @RestController public class UserController { @GetMapping("/user") public String getUser(HttpSession session) { String userId = (String) session.getAttribute("userId"); return "User Id: " + userId; } @PostMapping("/user") public void createUser(HttpSession session, @RequestParam String userId) { session.setAttribute("userId", userId); } } ``` 通过以上步骤,就可以实现Spring Session与Redis的整合,从而实现分布式应用程序的会话管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值