温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用spring+redis实现session共享

发布时间:2021-07-02 16:02:12 来源:亿速云 阅读:202 作者:chen 栏目:大数据

本篇内容介绍了“如何用spring+redis实现session共享”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

使用Nginx+Tomcat进行负载均衡时,希望使用轮询方式进行负载。但是如果使用轮询方式的话,可能会访问不同的Tomcat,此时如果不进行Session共享,则相当于是一个新的Session。就比如现有系统都是需要认证登录的系统,如果没有Session共享,则会导致用户退出登录

目前实现 session 共享的方式有以下几种:

1、使用Tomcat内置的Session复制方案
   只适合Tomcat小集群,不适合大集群,因为session复制是all to all的方式
2、使用第三方(个人)基于Tomcat实现的Session管理
    第三方支持,支持力度不够,尤其是不能提供对Tomcat8的支持
3、使用Spring Session实现

本文介绍的是第三种解决办法,即使用 spring session + redis 的方案

一、引入 pom 

<!-- 使用Spring Session来解决Session共享问题  --> <dependency>   <groupId>org.springframework.session</groupId>   <artifactId>spring-session-data-redis</artifactId>   <version>1.3.0.RELEASE</version>   <type>pom</type> </dependency> <dependency>   <groupId>biz.paluch.redis</groupId>   <artifactId>lettuce</artifactId>   <version>3.5.0.Final</version> </dependency> <dependency>   <groupId>redis.clients</groupId>   <artifactId>jedis</artifactId>   <version>2.9.0</version> </dependency>

二、配置 web.xml

在web.xml中加入以下过滤器,注意如果web.xml中有其他过滤器,一般情况下Spring 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>

三、配置 spring-mvc.xml

<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> <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="50" />     <property name="maxIdle" value="10" />     <property name="testOnBorrow" value="false" /> </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>

四、编辑 redis.properties

redis.ip=127.0.0.1 redis.port=6379 redis.password=123456

五、测试代码

写一个 请求方法 转发到 session_share.jsp

@RequestMapping("/session") public String session(HttpSession session, HttpServletRequest request){     request.setAttribute("id", request.getSession().getId());     return "session_share"; }

接着 编写 session_share.jsp

tomcat1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head>     <title>session共享</title> </head> <body> 1 我的session:${id} <br>sessionid=<%=session.getId()%> </body> </html> ------------------------------------------------------- tomcat2 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head>     <title>session共享</title> </head> <body> 2 我的session:${id} <br>sessionid=<%=session.getId()%> </body> </html>

六、配置 nginx 负载均衡

upstream load_balance_server {         #weigth参数表示权值,权值越高被分配到的几率越大         server 192.168.0.131:8080   weight=1;         server 192.168.0.167:8090   weight=1;     }	server {         listen       80;         server_name  localhost;         #charset koi8-r;         #access_log  logs/host.access.log  main;         location / {               proxy_pass http://load_balance_server;               index  dashboard index;               proxy_set_header Host       $http_host;               proxy_set_header X-Real-IP $remote_addr;               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                proxy_set_header X-Forwarded-Proto $scheme;	    } 	 }

nginx 所在 IP 为 192.168.0.131

连续多次访问  192.168.0.131:80/session     页面上的 sessionId  没有发生变化 , 1 和 2 在不停切换

“如何用spring+redis实现session共享”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI