- 浏览: 17333 次
- 性别:
- 来自: 西安
文章分类
最新评论
最近闲的蛋疼,顺便研究下apache+tomcat+memcache,顺便记录一下。
准备工作
- apache http://httpd.apache.org/download.cgi(推荐大家选择2.2.2版本,否则还需要装apr,apr-util等)
- tomcat http://tomcat.apache.org/download-60.cgi
- mod_jk http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/jakarta-tomcat-connectors-1.2.15-src.tar.gz
- memcache http://memcached.org/
- xmemcache http://code.google.com/p/xmemcached/downloads/detail?name=xmemcached-1.3.8-bin-with-dependencies.tar.gz&can=2&q=
安装工作
1.apache
# tar xzvf httpd-2.22.tar.gz
# cd httpd-2.22
# ./configure --prefix=/usr/local/apache --enable-so
# make
# make install
2.mod_jk
# tar xzvf jakarta-tomcat-connectors-1.2.15-src.tar.gz
# cd jakarta-tomcat-connectors-1.2.15-src/jk/native
# ./configure --with-apxs=/usr/test/apache/bin/apxs
# make
# cp ./apache-2.0/mod_jk.so /usr/test/apache/modules/
3.memcache
这个有点多 详见http://yu-zhang430.iteye.com/admin/blogs/1601197
配置工作
# cd /usr/test/apache/conf/ 在httpd.conf最后添加一句
Include conf/jk.conf
# touch jk.conf 在jk.conf写入以下配置
LoadModule jk_module modules/mod_jk.so
JkWorkersFIle conf/workers.properties
JkLogFile conf/jk.log
JkMount /*.jsp balancer//这里负责负载均衡
JkMount /jkstatus.jsp jkstatus//这里负责显示状态
#touch jk.log
#touch workers.properties 在workers.properties写入以下配置
worker.list=balancer,jkstatus
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1//设置负载的比重
worker.tomcat2.host=localhost
worker.tomcat2.port=8029
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1//设置负载的比重
注:这里填的port不是tomcat的端口,曾经我填为8080和8081,测试一直不成功,这里的port是2个tomcat的AJP13的端口
worker.balancer.type=lb
worker.balancer.balance_workers=tomcat1,tomcat2
worker.balancer.sticky_session=0这里设置session是否粘帖 0为不粘帖
worker.jkstatus.type=status
到此位置负载均衡完毕。下来我们用Memcached来完成Session同步。
1.修改web.xml添加filter
<filter> <filter-name>session</filter-name> <filter-class>com.filter.SessionFilter</filter-class> </filter> <filter-mapping> <filter-name>session</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.添加filter
package com.filter; import java.io.IOException; import java.util.UUID; 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.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class sessionFilter */ public class SessionFilter implements Filter { private int timout=36000000; @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)servletRequest; HttpServletResponse response=(HttpServletResponse)servletResponse; Cookie[] cookies=request.getCookies();//获取cookie String sessionId=""; if(cookies!=null&&cookies.length>0) { for(Cookie item:cookies) { if(item.getName().equals("sessionId")) { sessionId=item.getValue();//如果Cookie存在sessionId 重新计时 item.setMaxAge(timout); } } } if(cookies==null||sessionId.equals("")) { sessionId=UUID.randomUUID().toString(); Cookie cookie=new Cookie("sessionId", sessionId); cookie.setMaxAge(timout); response.addCookie(cookie);//如果Cookie不存在,创建cookie,生成sessionId添加到Cookie } //MyRequestWrapper装饰了httpservletrequest,我们重写一些request的方法 filterChain.doFilter(new MyRequestWrapper(sessionId,request), servletResponse); } @Override public void init(FilterConfig arg0) throws ServletException { } }
3.MyRequestWarpper.java
package com.filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpSession; public class MyRequestWrapper extends HttpServletRequestWrapper { private String sessionId; public MyRequestWrapper(String sessionId,HttpServletRequest request) { super(request); this.sessionId=sessionId; } @Override public HttpSession getSession() { return new HttpSessionWrapper(sessionId, super.getSession());//重写getSession方法 } @Override public HttpSession getSession(boolean create) { return new HttpSessionWrapper(sessionId, super.getSession(create)); } }
3.HttpsessionWrapper.java
package com.filter; import java.util.Enumeration; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionContext; @SuppressWarnings({"deprecation","rawtypes","static-access"}) public class HttpSessionWrapper implements HttpSession { private String sid = "";//sessionId public static Map map = null;//session private int timeout=1800; private HttpSession session; public HttpSessionWrapper(String sid,HttpSession session) { this.session = session; this.sid = sid; this.map = MyMemcachedCinent.getInstance().getSession(sid,timeout); } public Object getAttribute(String arg0) { return this.map.get(arg0); } public Enumeration getAttributeNames() { return (new Enumerator(this.map.keySet(), true)); } public void invalidate() { this.map.clear(); MyMemcachedCinent.getInstance().removeSession(this.sid); } public void removeAttribute(String arg0) { this.map.remove(arg0); MyMemcachedCinent.getInstance().saveSession(this.sid,timeout, this.map); } @SuppressWarnings("unchecked") public void setAttribute(String arg0, Object arg1) { this.map.put(arg0, arg1); MyMemcachedCinent.getInstance().saveSession(this.sid,timeout, this.map); } public long getCreationTime() { return session.getCreationTime(); } public String getId() { return session.getId(); } public long getLastAccessedTime() { return session.getLastAccessedTime(); } public int getMaxInactiveInterval() { return session.getMaxInactiveInterval(); } public ServletContext getServletContext() { return session.getServletContext(); } public HttpSessionContext getSessionContext() { return session.getSessionContext(); } public Object getValue(String arg0) { return session.getValue(arg0); } public String[] getValueNames() { return session.getValueNames(); } public boolean isNew() { return session.isNew(); } public void putValue(String arg0, Object arg1) { session.putValue(arg0, arg1); } public void removeValue(String arg0) { session.removeValue(arg0); } public void setMaxInactiveInterval(int arg0) { session.setMaxInactiveInterval(arg0); } }
4.Enumerator.java
package com.filter; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.NoSuchElementException; @SuppressWarnings({"rawtypes"}) public class Enumerator implements Enumeration { private Iterator iterator = null; public Enumerator(Collection collection) { this(collection.iterator()); } public Enumerator(Collection collection, boolean clone) { this(collection.iterator(), clone); } public Enumerator(Iterator iterator) { super(); this.iterator = iterator; } @SuppressWarnings("unchecked") public Enumerator(Iterator iterator, boolean clone) { super(); if (!clone) { this.iterator = iterator; } else { List list = new ArrayList(); while (iterator.hasNext()) { list.add(iterator.next()); } this.iterator = list.iterator(); } } public Enumerator(Map map) { this(map.values().iterator()); } public Enumerator(Map map, boolean clone) { this(map.values().iterator(), clone); } public boolean hasMoreElements() { return (iterator.hasNext()); } public Object nextElement() throws NoSuchElementException { return (iterator.next()); } }
5.MyMemcachedCinent
package com.filter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.exception.MemcachedException; import net.rubyeye.xmemcached.utils.AddrUtil; @SuppressWarnings({"rawtypes"}) public class MyMemcachedCinent { private static MyMemcachedCinent myMemcachedCinent; private MemcachedClient client; /** * 初始类的时候启动memcached */ private MyMemcachedCinent(){ MemcachedClientBuilder builder=new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211")); try { client=builder.build(); } catch (IOException e) { e.printStackTrace(); } } /** * MyMemcachedCinent单例 * @author zhangyu DateTime 2012-8-14 上午9:22:51 * @return */ public static synchronized MyMemcachedCinent getInstance() { if(myMemcachedCinent==null) { myMemcachedCinent= new MyMemcachedCinent(); } return myMemcachedCinent; } /** * 获取session * @author zhangyu DateTime 2012-8-14 上午9:18:19 * @param sid * @param timeout * @return */ public Map getSession(String sid,int timeout) { Map map=null; try { map = client.get(sid); if(map==null) { map=new HashMap(); client.set(sid, timeout, map); } } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } return map; } /** * 添加Session或者更新Session * @author zhangyu DateTime 2012-8-14 上午9:18:32 * @param id * @param timeout * @param session */ public void saveSession(String id,int timeout, Map session) { try { client.set(id,timeout, session); } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } } /** * 删除Session * @author zhangyu DateTime 2012-8-14 上午9:19:00 * @param id */ public void removeSession(String id) { try { client.delete(id); } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } } }
大功告成,session是通过Cookie和memcache保存的,如果要更保险点,可以判断下用户的cookie有木有北禁用,吧sessionId拼接到url里
相关推荐
### Apache + Tomcat + Memcached 全配置详解 #### 一、环境搭建概述 本文将详细介绍如何在 Ubuntu 操作系统下使用源代码安装并配置 Apache、Tomcat 和 Memcached,实现集群中的 Session 共享与负载均衡。由于采用...
apache 集群tomcat,session管理使用memcached,可部署javaweb,javaee的项目. 压缩包包含安装配置文档,apache no ssl 32bit 64bit,memcached 32bit 64bit安装文件以及tomcat所需要的jar,以及主要apache的配置文件. ...
里面有tomcat7配置文件和memcached session共享所需jar包.httpd配置文件。和一些文档,我已经测试过了,能够使用,适用系统为windows
标题中的“memcached+tomcat7共享session所需要的jar包”是指使用Memcached作为分布式缓存系统,实现Tomcat7服务器间的Session共享。这通常是为了在集群环境中解决Session复制或粘滞会话的问题,提高Web应用的可扩展...
windows下Apache2.4+Memcached1.4+Tomcat7集群环境 搭建说明。 部署后的部署包另有上传,参见http://download.csdn.net/detail/ahcstone/9246701 开包即用,方便验证
windows下Apache2.4+Memcached1.4+Tomcat7集群环境 搭建后的部署包,开包即用,方便验证
### Nginx+Apache+MySQL+PHP+Memcached+Squid 搭建门户网站 #### 一、前言与架构概述 随着互联网技术的发展,如何构建一个高效、稳定且能够应对高并发访问的Web服务器成为了许多企业和开发者关注的重点。本文将...
- Tomcat可以通过 sticky sessions 或者分布式session存储(如Memcached或Redis)来实现session复制。 通过以上步骤,可以成功地在Apache和Tomcat之间建立集群,并实现负载均衡和session复制。这不仅提高了服务的...
标题中的“tomcat + apache + memcached 集群jar包”揭示了这是一个关于构建分布式系统的技术组合,其中涉及到三个核心组件:Tomcat(一个流行的Java应用服务器)、Apache(一个广泛应用的HTTP服务器)和Memcached...
【Apache+Tomcat+Memcache 集群部署加缓存】 Apache、Tomcat 和 Memcache 的组合可以创建一个高效且可扩展的Web应用程序环境,其中Apache作为前端负载均衡器,Tomcat作为Java应用服务器,而Memcache则作为缓存系统...
Tomcat是Apache软件基金会的Java Servlet和JavaServer Pages (JSP) 应用服务器。在这个架构中,Tomcat主要负责执行Java应用程序和处理由Nginx转发过来的动态请求。它解析Java代码,执行业务逻辑,并将结果返回给...
本知识点聚焦于如何在Windows环境下,利用Nginx作为反向代理,Memcached进行session共享,以及Tomcat作为Java应用服务器来搭建这样一个集群。这个配置确保了用户在集群中的任何一台服务器上的操作都能被其他服务器...
Tomcat是Apache软件基金会的开源Java Servlet容器,支持Servlet和JSP标准。在集群环境中,每个Tomcat实例都需配置为接收Nginx转发的请求。为了实现Session共享,我们需要对Tomcat进行以下配置: 1. **Session复制**...
2. **Tomcat7**:Tomcat是Apache软件基金会的Jakarta项目中的一个核心项目,是一个开源的Servlet容器,用于运行Java Servlets和JSP。在这个集群中,Tomcat7作为Java应用服务器,处理由Nginx转发过来的请求,执行业务...
它的优点是协议简单,内置内存存储,并且他的分布式算法是在客户端完成的,不需要服务器端进行通信,我们当时在做项目的时候因为考虑到项目的高可用性高扩展性,因此在服务器部署方面采用了apache+jk+tomcat这种负载...