redis实现session共享

本文详细介绍使用Redis实现跨服务器Session共享的方法。包括环境搭建、代码实现及常见问题解决。

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


这几天在做session共享这么一个小模块,也查了好多资料,给我的感觉,就是太乱了,一直找不到我想要的东西,几乎全部实现方法都与我的想法不一样,在这里,我总结一下自己是如何用Redis实现session共享的,方便自己以后查询,也希望能给有这方面需求的朋友一些帮助(此句可忽略不看,嘎嘎)


先说一下我的开发环境:nginx、redis、tomcat,用moven构建项目,jetty服务器运行,所以在这里,下面也会涉及一下如何用maven打war包,部署在tomcat上运行。

redis是一个key-value数据库,存值取值,全靠这个key了,这里啰嗦一句,因为原创,专业的介绍我就不粘贴了,想了解的官方介绍的可以自行search.

 pom.xml中配置:

[html]  view plain  copy
  1. <!-- redis -->    
  2. <dependency>    
  3. <groupId>redis.clients</groupId>    
  4. <artifactId>jedis</artifactId>    
  5. <version>2.8.1</version>    
  6. </dependency>    
  7. <dependency>    
  8. <groupId>org.springframework.data</groupId>    
  9. <artifactId>spring-data-redis</artifactId>    
  10. <version>1.7.2.RELEASE</version>    
  11. </dependency>    


aplicationContext-redis.xml中配置


[html]  view plain  copy
  1. <!-- redis 客户端配置 -->    
  2.    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">    
  3.        <property name="maxTotal" value="${jedisPoolConfig.maxTotal}"/>    
  4.        <property name="maxIdle" value="${jedisPoolConfig.maxIdle}"/>    
  5.        <property name="maxWaitMillis" value="${jedisPoolConfig.maxWaitMillis}"/>    
  6.        <property name="testWhileIdle" value="true"/>    
  7.        <property name="testOnBorrow" value="false"/>    
  8.        <property name="testOnReturn" value="false"/>    
  9.    </bean>    
  10.    <bean id="readJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    
  11.        <property name="hostName" value="${jedis.read.host}" />    
  12.        <property name="port" value="${jedis.read.port}" />    
  13.        <property name="password" value="${jedis.read.password}" />    
  14.        <property name="timeout" value="${jedis.read.timeout}" />    
  15.        <property name="database" value="${jedis.read.database}" />    
  16.        <property name="poolConfig" ref="jedisPoolConfig" />    
  17.    </bean>    
  18.    <bean id="writeJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    
  19.        <property name="hostName" value="${jedis.write.host}" />    
  20.        <property name="port" value="${jedis.write.port}" />    
  21.        <property name="password" value="${jedis.write.password}" />    
  22.        <property name="timeout" value="${jedis.write.timeout}" />    
  23.        <property name="database" value="${jedis.write.database}" />    
  24.        <property name="poolConfig" ref="jedisPoolConfig" />    
  25.    </bean>    
  26.    <bean id="readRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">    
  27.        <property name="connectionFactory" ref="readJedisConnectionFactory" />    
  28.    </bean>    
  29.    <bean id="writeRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">    
  30.        <property name="connectionFactory" ref="writeJedisConnectionFactory" />    
  31.    </bean>    


配置完毕后,开始代码实现:

在LoginController里:

第一步,引入RedisTemplate


[html]  view plain  copy
  1. @Autowired    
  2. @Qualifier("writeRedisTemplate")    
  3. private StringRedisTemplate writeTemplate;   



这里只需要引入writeRedisTemplate即可,在登陆的时候,只负责写,只有在再次刷新的时候,经过过滤器,才需要读

第二步,正常登陆流程,登陆成功之后,request还要保存session信息

第三步,设置cookie值,把作为保存userSession信息在redis中的key值存入cookie,刷新浏览器的时候,过滤器可以从cookie中取到key值,进而去redis取对应的value值,即userSession

[java]  view plain  copy
  1. String domain = request.getServerName();    
  2.        String cookieId=MD5Util.MD5Encode("uasLoginer""UTF-8");    
  3.        //生成token,用作session在redis存储中的key值        
  4.        StringredisSessionKey= UUID.randomUUID().toString();    
  5.        Cookie uasLoginer = new Cookie(cookieId, redisSessionKey);    
  6.        if (domain.startsWith("uas.")) {    
  7.     uasLoginer.setDomain(domain.substring(4,domain.length()));    
  8. }else {    
  9.     uasLoginer.setDomain(domain);    
  10. }    
  11.        uasLoginer.setMaxAge(60000);    
  12.        uasLoginer.setPath("/");    
  13.        response.addCookie(uasLoginer);    



这里cookie跨域setDomain和setPath设置

第四步,把userSession信息存入redis中

RedisTemplate中写入redis的值要为String类型,需要把userSession对象转成Json字符串


 

[java]  view plain  copy
  1. userSessionString = JSON.toJSONString(userSession);    

在转Json的时候,遇到问题,导入import com.alibaba.fastjson.JSON;一直失败,发现pom中没有依赖Json的关系,如果有遇到相同的问题,可以检查下在pom.xml中是否有关于json的依赖关系,没的话,在pom.xml中导入json的依赖关系,如下:


[html]  view plain  copy
  1. <dependency>    
  2.     <groupId>net.sf.json-lib</groupId>    
  3.     <artifactId>json-lib</artifactId>    
  4.     <version>2.3</version>    
  5.     <classifier>jdk15</classifier>    
  6. </dependency>    



写入redis的代码如下:


[html]  view plain  copy
  1. writeTemplate.opsForHash().put(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey,redisSessionKey, userSessionString);    
  2. writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey, 1800L, TimeUnit.SECONDS);//设置redis中值的有效期    



完成这一操作,用户的session信息已经存入到redis中,可在redis中查看是否存入。

第五步:进入页面后,刷新页面,请求会经过过滤器,在Filter.Java中读取redis的值并进行一些处理

在过滤器这里,就无法通过注解的方式引入redisTemplate,可以通过如下的方式引入:


[html]  view plain  copy
  1. BeanFactory beans = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());    
  2.      StringRedisTemplate readTemplate = (StringRedisTemplate) beans.getBean("readRedisTemplate");    
  3.      StringRedisTemplate writeTemplate = (StringRedisTemplate) beans.getBean("writeRedisTemplate");    


过滤器从cookie中取出redis的key值,用readTemplate读出value值


[java]  view plain  copy
  1. String cookid=MD5Util.MD5Encode("uasLoginer""UTF-8");    
  2. Cookie[] cookies = req.getCookies();    
  3. String redisSessionKey = "";    
  4. if(cookies != null){    
  5.     for (Cookie cookie : cookies) {    
  6.         if(cookie.getName().equals(cookid)){    
  7.             redisSessionKey = cookie.getValue() ;    
  8.         }    
  9.     }    
  10. }    
  11. UserSession userSession = null;    
  12. String userSessionString = (String) readTemplate.boundHashOps(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey).get(redisSessionKey);    
  13. if(null != userSessionString ){    
  14.     @SuppressWarnings("static-access")    
  15.     JSONObject obj = new JSONObject().fromObject(userSessionString);//将json字符串转换为json对象    
  16.     userSession = (UserSession)JSONObject.toBean(obj,UserSession.class);    
  17.     writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey, 1800L, TimeUnit.SECONDS);    
  18.     request.getSession().setAttribute(UasContants.USER_SESSION, userSession);    
  19. }    
  20. if (userSession != null) {    
  21.     chain.doFilter(req, res);    
  22.     return;    
  23. }else {    
  24.     res.sendRedirect(UasContants.LOGIN_URL);    
  25.     return;    
  26. }    



在这里,另外附上关于web.xml关于LoginFilter的配置,有需要的可以参考下:


[html]  view plain  copy
  1. <!-- Spring监听器 -->    
  2. <listener>    
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  4. </listener>    
  5. <filter>    
  6.     <filter-name>loginFilter</filter-name>    
  7.     <filter-class>com.sfbest.uas.filter.LoginFilter</filter-class>    
  8.     <init-param>    
  9.         <param-name>excludePaths</param-name>    
  10.         <param-value>/login,/user/login,/user/auth</param-value>    
  11.     </init-param>    
  12. </filter>    
  13. <filter-mapping>    
  14.     <filter-name>loginFilter</filter-name>    
  15.     <url-pattern>/*</url-pattern>    
  16. </filter-mapping>    



按照上面的配置,就可以用redis实现session共享的功能,但我在开发的时候,遇到一个蛋疼的问题,测试环境上,

把项目部署在两台tomcat服务器上的时候,cookie里一直存不进去redis的key值,单台可以存进去,经过长期的检测,

终于发现是nginx配置出的问题,引以为戒,深深的阴影。下面我贴出我正常运行时nginx的配置代码


[html]  view plain  copy
  1. <span style="color:#555555;">upstream uassessiontest.d.com {    
  2.         server 10.103.16.226:8088;    
  3.         server 10.103.16.226:8089;    
  4.         }    
  5.        server {    
  6.                  </span><span style="color:#ff0000;">log_format  sf_uastest  '$remote_addr - $remote_user [$time_local] "$request" '    
  7.                                         '$status $body_bytes_sent "$http_referer" '    
  8.                                         '"$http_user_agent" $http_cookie';</span><span style="color:#555555;">    
  9.                 listen 80;    
  10.                 server_name uassessiontest.d.com;    
  11.                 </span><span style="color:#ff0000;">access_log /var/log/nginx/uassessiontest.log sf_uastest;</span><span style="color:#555555;">   
  12.     
  13.                 location / {    
  14.                         rewrite ^/$ /uas/ break;    
  15.                         proxy_pass http://uassessiontest.d.com;    
  16.                 }    
  17.         }  </span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值