51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1758|回复: 2
打印 上一主题 下一主题

redis集群 共享session解决

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-3-5 09:12:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
java web项目,不依赖于web容器,实现负载均衡,必须解决session共享问题。网上解决方法有很
多,但是我觉得使用

spring-session +redis是最方面快捷的,不用重复造轮子,且不用修改项目的代码,并且使项目使
用的session与web容器解耦,

完全由容器的httpsession转为使用spring提供的session.

具体怎么使用,请访问spring的官方网站。

这里写下我的项目中使用spring session+redis的步骤。

项目使用的是maven结构的web项目

1.pom.xml
  1. <!-- spring-session begin-->  
  2.     <dependency>  
  3.         <groupId>org.springframework.data</groupId>  
  4.         <artifactId>spring-data-redis</artifactId>  
  5.         <version>1.7.6.RELEASE</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.         <groupId>org.springframework.session</groupId>  
  9.         <artifactId>spring-session</artifactId>  
  10.         <version>1.3.0.RELEASE</version>  
  11.     </dependency>  
  12.     <dependency>  
  13.         <groupId>redis.clients</groupId>  
  14.         <artifactId>jedis</artifactId>  
  15.         <version>2.8.1</version>  
  16.     </dependency>  
  17. <dependency>  
  18.     <groupId>org.apache.commons</groupId>  
  19.     <artifactId>commons-pool2</artifactId>  
  20.     <version>2.4.2</version>  
  21.     <scope>compile</scope>  
  22. </dependency>  
  23.     <!-- spring-session end-->  
复制代码
注意:刚开始我的spring框架包(就是好多spring的包)用的是4.0的,但是启动tomcat服务器的时候报错,
说不能初始化redisTemplate,跑去stackofflow上看,有说是因为jar的问题,需要升级spring框架必须高于
4.2.1,因为redistemplate换了构造器。于是将spring框架升到4.3.7.RELEASE。就ok了。

2.配置filter

在web.xml中,加上这段配置 必须位于filter链的最前面
  1. <!-- spring-session -->  
  2. <filter>  
  3.     <filter-name>springSessionRepositoryFilter</filter-name>  
  4.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>springSessionRepositoryFilter</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9.     <dispatcher>REQUEST</dispatcher>  
  10.     <dispatcher>ERROR</dispatcher>  
  11. </filter-mapping>  
复制代码
3.在applicationContext.xml(这是我的spring容器配置文件的名字)中注册需要的bean。(用注解也
行,但我是用的xml配置)
  1. <!-- redis -->  
  2.    <bean id="jedisPoolConfig"  class="redis.clients.jedis.JedisPoolConfig">  
  3.    </bean>  
  4.    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  5.         <property name="hostName" value="localhost" />  
  6.         <property name="port" value="6379" />  
  7.         <property name="password" value="****"/>  
  8.         <property name="usePool" value="true"/>  
  9.         <property name="poolConfig" ref="jedisPoolConfig"/>  
  10.    </bean>  
  11.    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  12.         <property name="connectionFactory" ref="jedisConnectionFactory"/>  
  13.    </bean>  
  14.    <!-- 将session放入redis -->  
  15.    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">  
  16.         <property name="maxInactiveIntervalInSeconds" value="1800"/>  
  17.    </bean>  
复制代码
OK,到这儿就结束了,系统中的代码一点也不用改动。这真的解耦啊,spring这的非常强大!

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /2 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-6-25 20:58 , Processed in 0.067070 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表