1,spring 必须是4.3以上版本的
2,maven配置
添加两个重要的依赖
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.2.RELEASE</version> <type>pom</type></dependency>
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version></dependency> 3,spring.xml 文件配置
<!--分布式session--><bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="600"/></bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${jedis.pool.maxTotal}" /> <property name="maxIdle" value="${jedis.pool.maxIdle}" /></bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="${redis.ip}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.password}" /> <property name="timeout" value="3000"/> <property name="usePool" value="true"/> <property name="poolConfig" ref="jedisPoolConfig"/></bean> 4,redis配置
#redis配置#*****************jedis\u8FDE\u63A5\u53C2\u6570\u8BBE\u7F6E*********************##redis\u670D\u52A1\u5668ip ##redis.ip=47.92.128.123redis.ip=192.168.9.168#redis\u670D\u52A1\u5668\u7AEF\u53E3\u53F7#redis.port=6379redis.password=123456#************************jedis\u6C60\u53C2\u6570\u8BBE\u7F6E*******************##jedis\u7684\u6700\u5927\u5206\u914D\u5BF9\u8C61#jedis.pool.maxActive=3000#jedis\u6700\u5927\u4FDD\u5B58idel\u72B6\u6001\u5BF9\u8C61\u6570 #jedis.pool.maxIdle=1000#jedis\u6C60\u6CA1\u6709\u5BF9\u8C61\u8FD4\u56DE\u65F6\uFF0C\u6700\u5927\u7B49\u5F85\u65F6\u95F4 #jedis.pool.maxTotal=1500#jedis\u8C03\u7528borrowObject\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u68C0\u67E5#jedis.pool.testOnBorrow=true#jedis\u8C03\u7528returnObject\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u68C0\u67E5 #jedis.pool.testOnReturn=true 5,web.xml 配置
<!--spring redis 共享session 拦截器--><filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping> 经过以上配置,就可以使用了要想实现session共享还需要访问的时候域名一致,如果域名不一致是不能够共享的例如 : 负载均衡两台服务器A: 192.168.1.101 : 8080B: 192.168.1.102 : 8081在浏览器中分别用IP地址去访问两台服务器下的服务是不会实现session共享的只有通过nginx做负载均衡两台服务器绑定同一域名如 www.test.com通过域名去访问这时不管请求分发到哪一台机器都可以实现session的共享
时间: 2024-10-22 08:25:14