架构设计之Spring-Session的分布式集群会话管理
前言
通常在web开发中,回话管理是很重要的一部分,用于存储与用户相关的一些数据。对于JAVA开发者来说,项目中的session一般由Tomcat或者jetty容器来管理。
特点介绍
尽管使用特定的容器可以很好地实现会话管理,但是独立容器挂掉或者由于其他原因重启会导致用户信息丢失,并且无法支持分布式集群会话管理。
上图举例:

这是一个简单的负载均衡集群架构模型,后端三台Tomcat服务,假设每台服务都使用自己的会话管理,而集群策略是基于加权轮询的方式实现。试想一下,用户是不是永远无法登陆系统?
当然,你可能会想,我可以使用基于IP_hash的方式实现负载均衡嘛。但是如果地区分布相对单一,产生的hash值分布可能也不会太均匀,那就起不到负载均衡的作用了。
一般来说,有两种解决方案,session复制和session统一管理。对于session复制,简单的几台还是可以的,但是如果上百台甚至上千台就要考虑复制成本问题了。
对于统一session管理可以是关系型数据库,比如MySql(基本不用,考虑到效率问题);非关系型数据库 redis,memcache等等。
解决方案
基于Tomcat的会话插件实现tomcat-redis-session-manager 和tomcat-memcache-session-manager,会话统一由NoSql管理。对于项目本身来说,无须改动代码,只需要简单的配置Tomcat的server.xml就可以解决问题。但是插件太依赖于容器,并且对于Tomcat各个版本的支持不是特别的好
重写Tomcat的session管理,代码耦合度高,不利于维护。
使用开源的session管理框架,比如spring_session,既不需要修改Tomcat配置,又无须重写代码,只需要配置相应的参数即可。
功能实现
下面,主要是基于spring_session实现的分布式集群会话管理案例。
项目需要使用到spring_Mvc4.2.5,spring_session-1.2.2和redis-3.2.8(需要自行安装redis服务)。
下载相关JAR包:
<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId><version>1.2.2.RELEASE</version></dependency>
spring-mvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><description>Spring MVC Configuration</description><!-- 加载配置属性文件 --><context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" /><!-- 使用Annotation自动注册Bean,只扫描@Controller --><context:component-scan base-package="com.itstyle.web" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><mvc:annotation-driven/><!--启动Spring MVC的注解功能,设置编码方式,防止乱码--><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><bean class = "org.springframework.http.converter.StringHttpMessageConverter"><property name = "supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean></list></property></bean><!-- REST中根据URL后缀自动判定Content-Type及相应的View --><bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"><property name="mediaTypes" ><map><entry key="xml" value="application/xml"/><entry key="json" value="application/json"/></map></property><property name="ignoreAcceptHeader" value="true"/><property name="favorPathExtension" value="true"/></bean><!-- 定义视图文件解析 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="${web.view.prefix}"/><property name="suffix" value="${web.view.suffix}"/></bean><!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 --><mvc:default-servlet-handler /><!-- 静态资源映射 SpringMVC会自动给静态资源Response添加缓存头Cache-Control和Expires值 cache-period="31536000"--><mvc:resources mapping="/static/**" location="/static/" /><!-- 定义无Controller的path<->view直接映射(首页或者登陆页) --><mvc:view-controller path="/" view-name="redirect:${web.view.login}"/></beans>
config.properties
#============================##===== System settings ======##============================##产品信息设置productName=科帮网 srping sessioncopyrightYear=2017version=V1.0.0#分页配置page.pageSize=10#索引页路径web.view.index=/index#登陆页面web.view.login=/login#视图文件存放路径web.view.prefix=/WEB-INF/views/web.view.suffix=.jsp#静态文件后缀web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk
spring-redis.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName" default-lazy-init="true"><!-- 加载资源文件 其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载--><context:property-placeholder location="classpath:redis.properties" /><!-- redis --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.host}" /><property name="port" value="${redis.port}" /><property name="password" value="${redis.password}" /><property name="timeout" value="${redis.timeout}" /><property name="poolConfig" ref="jedisPoolConfig" /><property name="usePool" value="true" /></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /></bean><!-- 将session放入redis --><context:annotation-config/><bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"><property name="maxInactiveIntervalInSeconds" value="1800" /></bean></beans>
redis.properties
#redis中心redis.host=127.0.0.1redis.port=6379redis.password=123456redis.maxIdle=100redis.maxActive=300redis.maxWait=1000redis.testOnBorrow=trueredis.timeout=100000
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>spring_session</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-redis.xml</param-value></context-param><!-- 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><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>springServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/spring-mvc*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>login</welcome-file></welcome-file-list></web-app>
测试功能
最后,启动项目访问http://localhost:8080/spring_session/login

登录redis服务,执行以下命令:
KEYS *

注意,对比sessionId是一致的。
架构设计之Spring-Session的分布式集群会话管理的更多相关文章
- 架构设计之Spring-Session分布式集群会话管理
前言 通常在web开发中,回话管理是很重要的一部分,用于存储与用户相关的一些数据.对于JAVA开发者来说,项目中的session一般由Tomcat或者jetty容器来管理. 特点介绍 尽管使用特定的容 ...
- 基于puppet分布式集群管理公有云多租户的架构浅谈
基于puppet分布式集群管理公有云多租户的架构浅谈 一.架构介绍 在此架构中,每个租户的业务集群部署一台puppet-master作为自己所在业务集群的puppet的主服务器,在每个业务集群所拥 ...
- 2017最新技术java高级架构、千万高并发、分布式集群、架构师入门到精通视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...
- GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台
部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...
- Spring Session实现分布式session的简单示例
前面有用 tomcat-redis-session-manager来实现分布式session管理,但是它有一定的局限性,主要是跟tomcat绑定太紧了,这里改成用Spring Session来管理分布 ...
- Spring Session解决分布式Session问题的实现原理
使用Spring Session和Redis解决分布式Session跨域共享问题 上一篇介绍了如何使用spring Session和Redis解决分布式Session跨域共享问题,介绍了一个简单的案例 ...
- 使用Spring Session做分布式会话管理
在Web项目开发中,会话管理是一个很重要的部分,用于存储与用户相关的数据.通常是由符合session规范的容器来负责存储管理,也就是一旦容器关闭,重启会导致会话失效.因此打造一个高可用性的系统,必须将 ...
- 抄来的,占个位儿【百度架构师是怎样搭建MySQL分布式集群】
1.准备集群搭建环境 使用6台虚拟机来搭建MySQL分布式集群,相应的实验环境与对应的MySQL节点之间的对应关系如下图所示: 管理节点(MGM):这类节点的作用是管理MySQLCluster ...
- 『设计』Slithice 分布式架构设计-支持一体式开发,分布式发布
项目原因: 参与过各种 分布式项目,有 Socket,Remoting,WCF,当然还有最常用的可以跨平台的 WebService. 分布式编码的时间浪费: 但是,无一例外的,开发分布式程序的开发遵循 ...
随机推荐
- 移植QT5.6到嵌入式开发板(史上最详细的QT移植教程)
目前网上的大多数 QT 移植教程还都停留在 qt4.8 版本,或者还有更老的 Qtopia ,但是目前 Qt 已经发展到最新的 5.7 版本了,我个人也已经使用了很长一段时间的 qt5.6 for w ...
- 一道有序洗牌的笔试题,阿里\UC等都用过
题目:给定一个已经降序排好序的正数数组,要求按「最小.最大.次小.次大……」的顺序重新排序.期望的时间复杂度为O(n),空间复杂度为O(1),即不能申请额外的数组. 例如:输入[7,6,5,4,3,2 ...
- python 安装whl文件
对于安装whl格式的文件,首先要安装wheel包 利用 pip install wheel或下载再安装: 下载地址: https://pypi.python.org/pypi/wheel 解压后安装: ...
- Oracle日期周具体解释以及周開始结束时间计算
1 ORACLE中周相关知识描写叙述 1.1 日期格式化函数 TO_CHAR(X [,FORMAT]):将X按FORMAT格式转换成字符串. X是一个日期,FORMAT是一个规定了 ...
- 怎样更爽地看PDF杂志
下载了一些PDF的杂志,想着要是全屏双页显示,应该是很爽的,但是,下载了应该foxit reader,还是遇到一些问题: 1.全屏:F11即可 2.全屏后不双页:在选项中,"全屏" ...
- DIV CSS布局中绝对定位和浮动用法
转自:http://developer.51cto.com/art/201009/223337_1.htm 你对DIV CSS布局中绝对定位和浮动的概念及使用是否熟悉,这里和大家分享一下,CSS中,实 ...
- (剑指Offer)面试题13:在O(1)时间内删除链表结点
题目: 在给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点.链表结点与函数的定义如下: struct ListNode{ int val; ListNode* next; } ...
- 推荐8款HTML5相关的特殊效果
HTML5是HTML的升级版,HTML5有两大特点:首先,强化了 Web 网页的表现性能.其次,追加了本地数据库等 Web 应用的功能.广义论及HTML5时,实际指的是包括HTML.CSS和JavaS ...
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...
- 全栈project师体能备战--知识面(10--20)
WCF Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,能够翻译为Windows 通讯开发平台. 对于 WCF 的client ...