spring-session redis实现session共享
程序员文章站
2022-05-17 10:36:46
...
安装redis 启动,略
引入jar
<!-- redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
配置redis.properties
#主机地址
redis_hostName=172.31.102.91
#端口
redis_port=6379
#连接超时时间
redis_timeout=200000
redis_maxIdle:300
redis_maxActive:600
redis_maxWait:100000
redis_testOnBorrow:true
配置spring-redis
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<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="100" />
<property name="maxIdle" value="10" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis_hostName}"/>
<property name="port" value="${redis_port}"/>
<property name="timeout" value="3000"/>
<property name="usePool" value="true"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
</beans>
配置web.xml 位于最上面
<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>
运行效果截图
推荐阅读
-
PHP实现cookie跨域session共享的方法分析
-
nginx+redis实现session共享
-
php实现session共享的实例方法
-
PHP简单实现HTTP和HTTPS跨域共享session解决办法
-
PHP使用Redis实现Session共享的实现示例
-
nginx+tomcat实现负载均衡,使用redis session共享
-
Spring boot集成spring session实现session共享的方法
-
Nginx+Tomcat8+Memcached实现负载均衡及session共享
-
ASP.NET下跨应用共享Session和使用Redis进行Session托管
-
PHP实现多服务器session共享之NFS共享的方法