当前位置: > Linux集群 > Memcached >

nginx+tomcat+memcache集群部署

时间:2016-05-20 20:20来源:linux.it.net.cn 作者:IT

简要说明:使用nginx做负载均衡;memcache做session共享

1.软件版本

 

nginx-1.7.3

 

 

memcached-1.4.22

tomcat-7.0.43

 

 

2.nginx搭建负载均衡配置

编辑nginx/conf/nginx.conf 配置文件

 

添加负载均衡设置

upstream localhost{

server localhost:8082;

server localhost:8083;

}

server 后面tomcat的Ip及端口

 

添加80端口的虚拟目录,配置转发代理

 


  1. server {
  2. listen 80;
  3. server_name localhost;
  4. index index.html index.htm index.jsp;#Default Page
  5. root /home/www/web/ROOT;#WEB LIB DIRECTRY
  6.  
  7. #charset koi8-r;
  8.  
  9. #access_log logs/host.access.log main;
  10.  
  11. #Set JSP Proxy
  12. location /
  13. {
  14. proxy_set_header Host $host;
  15. proxy_set_header X-Real-IP $remote_addr;
  16. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  17. proxy_pass http://localhost;
  18. }
  19. }
3.tomcat 复制一份,设置不同的访问端口和shutdown端口

 

到这一步就可以实现用nginx+tomcat的负载均衡了。。

均衡后还不够,还需要对session进行处理,接下来安装好memcache.并启动

4.修改tomcat/conf/context.xml文件,添加下列代码

 

 


  1. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
  2. memcachedNodes="n1:127.0.0.1:11211"
  3. sticky="false"
  4. lockingMode="auto"
  5. sessionBackupAsync="false"
  6. requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
  7. sessionBackupTimeout="1000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  8. />
5.将msm包放入tomcat/lib/中

 

 

asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.11.jar
memcached-session-manager-1.7.0.jar
memcached-session-manager-tc7-1.8.1.jar
minlog-1.2.jar
msm-kryo-serializer-1.7.0.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar

 

到此session已处理完毕;

如果配置正确。则可以看到启动的日志里面会有如下信息:

 

Mar 09, 2015 11:38:47 PM de.javakaffee.web.msm.MemcachedSessionService startInternal

INFO:  starts initialization... (configured nodes definition n1:10.211.55.6:11211, failover nodes null)

Mar 09, 2015 11:38:47 PM de.javakaffee.web.msm.RequestTrackingHostValve <init>

INFO: Setting ignorePattern to .*\.(ico|png|gif|jpg|css|js)$

Mar 09, 2015 11:38:47 PM de.javakaffee.web.msm.MemcachedSessionService setLockingMode

INFO: Setting lockingMode to AUTO

Mar 09, 2015 11:38:47 PM de.javakaffee.web.msm.MemcachedSessionService createTranscoderFactory

INFO: Creating transcoder factory de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory

 

6.测试session的jsp文件

 


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
  2. <%
  3. HttpSession sess = request.getSession(false);
  4. %>
  5. <br/> session is : <%=sess.getId() %><br/>
  6. <%
  7. String name = request.getParameter("name");
  8. if(name!=null && name.length()>0){
  9. String value = request.getParameter("value");
  10. sess.setAttribute(name,value);
  11. TestTask.infos.add("name="+name+",value = "+value);
  12. }
  13. %>
  14. <b>Session List:</b><br/>
  15. <table >
  16. <tr>
  17. <td>name</td><td>value</td>
  18. </tr>
  19. <%
  20. Enumeration<String> names=sess.getAttributeNames();
  21. while(names.hasMoreElements()){
  22. String sname=names.nextElement();
  23. String value=sess.getAttribute(sname).toString();
  24. %>
  25. <tr>
  26. <td><%= sname %></td><td><%=value %></td>
  27. </tr>
  28. <%
  29. }
  30. %>
  31. </table>
  32. <br />
  33.  



(责任编辑:IT)
------分隔线----------------------------