HttpServletRequestWrapper模拟实现分布式Session
HttpSession的内容都放在一个单独的Map中,模拟远程分布式Session。
1.使用HttpServletRequestWrapper创建自定义Request
2.使用动态代理包装自定义Request返回的HttpSession对象
3.创建过滤器,使用自定义Request替换原有的Request对象。
4.在Servlet中得到的HttpSession对象,写入和读取内容都假设通过远程Session服务器。
创建自定义的Request,返回动态代理的HttpSession
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletRequestWrapper;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpServletResponseWrapper;
- import javax.servlet.http.HttpSession;
- public class RemoteSessionRequest extends HttpServletRequestWrapper {
- public RemoteSessionRequest(HttpServletRequest request) {
- super(request);
- }
- @Override
- public HttpSession getSession() {
- return RemoteSessionHandler.getInstance(super.getSession());
- }
- }
- class RemoteSessionHandler implements InvocationHandler {
- //模拟远程Session服务器,Key表示SessionId,Value表示该Session的内容
- private static Map<String, Map<String, Object>> map = new ConcurrentHashMap<String, Map<String, Object>>();
- private HttpSession session = null;
- private RemoteSessionHandler(HttpSession httpSession) {
- this.session = httpSession;
- };
- public static HttpSession getInstance(HttpSession httpSession) {
- InvocationHandler handler = new RemoteSessionHandler(httpSession);
- return (HttpSession) Proxy.newProxyInstance(httpSession.getClass().getClassLoader(), httpSession.getClass().getInterfaces(), handler);
- }
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if ("setAttribute".equals(method.getName())) {
- String id = session.getId();
- Map<String, Object> m = map.get(id);
- if (m == null) {
- m = new HashMap<String, Object>();
- map.put(id, m);
- }
- m.put((String) args[0], args[1]);
- System.out.println("[存入]key:" + args[0] + ",value:" + args[1]);
- return null;
- } else if ("getAttribute".equals(method.getName())) {
- String id = session.getId();
- Map<String, Object> m = map.get(id);
- if (m == null) {
- return null;
- }
- Object result = m.get(args[0]);
- System.out.println("[取出]key:" + args[0] + ",value:" + result);
- return result;
- }
- return method.invoke(session, args);
- }
- }
使用过滤器替换原有的Request
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.annotation.WebFilter;
- import javax.servlet.http.HttpServletRequest;
- @WebFilter("/*")
- public class SessionFilter implements Filter {
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- chain.doFilter(new RemoteSessionRequest((HttpServletRequest) request), response);
- }
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
- }
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
- }
- }
在Servlet中按照原有方式使用HttpSession。
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- HttpSession session = request.getSession();
- session.setAttribute("name", "Hello");
- session.getAttribute("name");
- session.getAttribute("other");
- }
结果可以看到,他已经模拟从远程服务器存取数据
[存入]key:name,value:Hello
[取出]key:name,value:Hello
[取出]key:other,value:null
HttpServletRequestWrapper模拟实现分布式Session的更多相关文章
- SpringBoot 分布式session
SpringBoot 分布式session实现 1. 什么是分布式session 在集群环境中,不得不考虑的一个问题是用户访问产生的session如何处理.如过不做任何处理,用户将出现频繁俸禄的现象, ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- Spring Session解决分布式Session问题的实现原理
使用Spring Session和Redis解决分布式Session跨域共享问题 上一篇介绍了如何使用spring Session和Redis解决分布式Session跨域共享问题,介绍了一个简单的案例 ...
- 分布式session的实现
一.分布式Session的几种实现方式 1.基于数据库的Session共享 2.基于NFS共享文件系统3.基于memcached 的session,如何保证 memcached 本身的高可用性?4. ...
- 可扩容分布式session方案
分布式session有以下几种方案: 1. 基于nfs(net filesystem)的session共享 将共享服务器目录mount各服务器的本地session目录,session读写受共享服务器i ...
- NoSQL-Redis【2】-实现分布式Session
经过一周紧张的开发和调试,终于把Redis实现的分布式Session发布到了生产环境.我在本地测试了100万的数据,Redis的速度确实让我满意,期待在线上有更好的表现. 一.配置windows-se ...
- [Node.js] Node + Redis 实现分布式Session方案
原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...
- 基于ZooKeeper的分布式Session实现(转)
1. 认识ZooKeeper ZooKeeper—— “动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而不是像走在原始丛林里,心惊胆颤的被 ...
- 微服务架构下分布式Session管理
转载本文需注明出处:EAII企业架构创新研究院(微信号:eaworld),违者必究.如需加入微信群参与微课堂.架构设计与讨论直播请直接回复此公众号:“加群 姓名 公司 职位 微信号”. 一.应用架构变 ...
随机推荐
- 【霍夫曼树】poj 1339 poker card game (数组排序+辅助队列的方法,预处理O(nlogn),构造霍夫曼树O(n))
poj.org/problem?id=1339 #include<iostream> #include<cstdio> #include<string> #incl ...
- Java中NIO、BIO、AIO相关概念及应用场景
1.同步阻塞IO(JAVA BIO):同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时,服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可以通 ...
- mysql/oracle 连接参数中文变问号
jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf8&useSSL=true
- spring boot -- 无法读取html文件,碰到的坑
碰到的坑,无法Controller读取html文件 1. Controller类一定要使用@Controller注解,不要用@RestController 2. resource目录下创建templa ...
- 最短路中部分点只能从中任意选取K个问题
题意:给N个点,还有另外m个点(其中只能选K个),求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其他点了),若该点是m个点中的,则count[i]=原来的+1:若不是,则 ...
- LeetCode OJ--Evaluate Reverse Polish Notation
http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 栈使用 #include <iostream> #inc ...
- LeetCode OJ--Partition List
http://oj.leetcode.com/problems/partition-list/ 链表的处理 #include <iostream> using namespace std; ...
- HTML 中 SELECT 选项分组
<select name="viewType"> <option value selected>选择排序/显示方式</option> <o ...
- CentOS6、CentOS7配置Base源和epel源
1.用yum安装软件报错 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=x86_64&a ...
- Codevs1062路由选择
/* #include<iostream> #include<cstdio> #include<cstring> #define MAXN 301 using na ...