分布式集群环境下,如何实现session共享五(spring-session+redis 实现session共享)
这是分布式集群环境下,如何实现session共享系列的第五篇。在上一篇:分布式集群环境下,如何实现session共享四(部署项目测试)中,针对nginx不同的负载均衡策略:轮询、ip_hash方式,测试了session的不同使用情况,并且留下了一个问题:有没有可能针对nginx负载均衡策略(轮询)的基础上,对session实现共享呢???
本篇在nginx负载均衡策略(轮询的基础上),通过spring-session将session存储到redis,实现session共享。
1.改造项目
1.1.导入依赖
<!--spring 版本--> <spring.version>5.0.2.RELEASE</spring.version> <spring.session.data.redis.version>1.3.1.RELEASE</spring.session.data.redis.version> <lettuce.version>3.5.0.Final</lettuce.version> <!-- spring web包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>${spring.session.data.redis.version}</version> </dependency> <dependency> <groupId>biz.paluch.redis</groupId> <artifactId>lettuce</artifactId> <version>${lettuce.version}</version> </dependency>
1.2.编写redis配置文件
redis.host=192.168.80.22 redis.pass=myredis redis.port=6379 redis.session.timeout=600
1.3.编写spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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"> <context:annotation-config /> <!-- 加载properties文件 --> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:session-redis.properties</value> </list> </property> </bean> <!-- RedisHttpSessionConfiguration --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <!-- session过期时间,单位是秒 --> <property name="maxInactiveIntervalInSeconds" value="${redis.session.timeout}" /> </bean> <!--LettuceConnectionFactory --> <bean class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" /> </beans>
1.4.配置web.xml
<!--加载spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-session.xml</param-value> </context-param> <!--配置监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置过滤器(该过滤器要配置在第一的位置)--> <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> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
1.5.启动redis
#启动redis服务端
[root@hadoop02 bin]# ./redis-server
#客户端连接操作
[root@hadoop02 bin]# ./redis-cli
#设置允许远程访问
127.0.0.1:6379> CONFIG SET protected-mode no
OK
2.nginx配置
#添加tomcat列表,真实应用服务器都放在这 upstream tomcat_pool{ #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大; server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s; server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s; }
3.测试
3.1.谷歌浏览器测试
3.2.火狐浏览器测试
3.3.查看redis中存储的session
[root@hadoop02 bin]# ./redis-cli
127.0.0.1:6379> keys *
1) "spring:session:expirations:1558614360000"
2) "spring:session:sessions:af12f7a7-3385-44dc-93b2-a3fa82026b25"
3) "spring:session:sessions:abc97363-8f1d-4d1c-a9df-e2a78628af05"
4) "site"
5) "spring:session:sessions:expires:abc97363-8f1d-4d1c-a9df-e2a78628af05"
127.0.0.1:6379>
总结:通过spring-session把session存储到redis中,即便nginx负载均衡策略为轮询方式,也一样实现了session的共享,这才是我们的终极目标,真好!!!
我们唯一能够控制的是自己的脾气和努力