设为首页 收藏本站
查看: 674|回复: 0

[经验分享] Apache+Tomcat负载均衡和集群的尝试

[复制链接]

尚未签到

发表于 2017-1-10 07:22:14 | 显示全部楼层 |阅读模式
  1 各软件版本
Apache 2.2.4 因为jd_mod 用的是2.2.4的,所以这里也用这个版本,以免有问题
Tomcat 5.5.26
jd-mod: mod_jk-1.2.26-httpd-2.2.4.so

2 配置Apache

引用:



LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so
JkWorkersFile D:/Apache2.2/conf/workers.properties
JkLogFile d:/Apache2.2/logs/mod_jk.log
JkLogLevel info
#JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /servlet/* myloadbalancer
JkMount /*.jsp myloadbalancer



  

3 workers.properties

引用:

worker.list=myloadbalancer

worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.lbfactor=1

worker.tomcat2.type=ajp13
worker.tomcat2.host=localhost
worker.tomcat2.port=9009
worker.tomcat2.lbfactor=1

worker.tomcat3.type=ajp13
worker.tomcat3.host=localhost
worker.tomcat3.port=9019
worker.tomcat3.lbfactor=1

worker.myloadbalancer.type=lb
worker.myloadbalancer.balance_workers=tomcat1,tomcat2,tomcat3
worker.myloadbalancer.sticky_session=false


  

使用了3个tomcat,其侦听端口分别为 8009,9009和9019
worker.myloadbalancer.sticky_session=false 声明即使是同一个SESSIONID,也不用同一个Worker进行处理,这样更有利于负载均衡。
如果为true,则如果SESSIONID相同,则会自动使用上一次对应的worker

4 apache-tomcat-5.5.26-1\conf\server.xml

查看复制到剪切板打印





  • <Server port="8005" shutdown="SHUTDOWN">  
  • ....   

  • <Connector port="8080" maxHttpHeaderSize="8192"  
  • ....   

  • <Connector port="8009"  
  • ........   

  • <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  
  • ....   

  • <!-- 这里删除注释的起始标志 -->  

  • <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  •   ........   

  •             <Receiver    

  •                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  

  •                 tcpListenAddress="192.168.0.1"  

  •                 tcpListenPort="4001"  

  •                 tcpSelectorTimeout="100"  

  •                 tcpThreadCount="6"/>  
  • ........   

  •         </Cluster>  

  • <!-- 这里删除注释的终止标志 -->  



<Server port="8005" shutdown="SHUTDOWN">
....
<Connector port="8080" maxHttpHeaderSize="8192"
....
<Connector port="8009"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
........
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="192.168.0.1"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
........
</Cluster>
<!-- 这里删除注释的终止标志 -->
  

5 apache-tomcat-5.5.26-2\conf\server.xml

查看复制到剪切板打印





  • <Server port="9005" shutdown="SHUTDOWN">  
  • ....   

  • <Connector port="9080" maxHttpHeaderSize="8192"  
  • ....   

  • <Connector port="9009"  
  • ........   

  • <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">  
  • ....   

  • <!-- 这里删除注释的起始标志 -->  

  • <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  •   ........   

  •             <Receiver    

  •                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  

  •                 tcpListenAddress="192.168.0.1"  

  •                 tcpListenPort="4002"  

  •                 tcpSelectorTimeout="100"  

  •                 tcpThreadCount="6"/>  
  • ........   

  •         </Cluster>  

  • <!-- 这里删除注释的终止标志 -->  



<Server port="9005" shutdown="SHUTDOWN">
....
<Connector port="9080" maxHttpHeaderSize="8192"
....
<Connector port="9009"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
........
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="192.168.0.1"
tcpListenPort="4002"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
........
</Cluster>
<!-- 这里删除注释的终止标志 -->
  

6 apache-tomcat-5.5.26-3\conf\server.xml

查看复制到剪切板打印





  • <Server port="9006" shutdown="SHUTDOWN">  
  • ....   

  • <Connector port="9088" maxHttpHeaderSize="8192"  
  • ....   

  • <Connector port="9019"  
  • ........   

  • <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">  
  • ....   

  • <!-- 这里删除注释的起始标志 -->  

  • <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  •   ........   

  •             <Receiver    

  •                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  

  •                 tcpListenAddress="192.168.0.1"  

  •                 tcpListenPort="4003"  

  •                 tcpSelectorTimeout="100"  

  •                 tcpThreadCount="6"/>  
  • ........   

  •         </Cluster>  

  • <!-- 这里删除注释的终止标志 -->  



<Server port="9006" shutdown="SHUTDOWN">
....
<Connector port="9088" maxHttpHeaderSize="8192"
....
<Connector port="9019"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
........
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="192.168.0.1"
tcpListenPort="4003"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
........
</Cluster>
<!-- 这里删除注释的终止标志 -->
  

7 测试用的程序
使用了网上普遍采用的一段代码
webapps/test/index.jsp

查看复制到剪切板打印





  •     
  • <%@ page c %>   

  • <%@ page import="java.util.*" %>   
  • <html><head><title>Cluster App Test</title></head>   
  • <body>   
  • Server Info:   
  • <%   

  • System.out.println("================");   

  • out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>   
  • <%   

  •   out.println("<br> ID " + session.getId()+"<br>");   

  •   // 如果有新的 Session 属性设置   

  •   String dataName = request.getParameter("dataName");   

  •   if (dataName != null && dataName.length() > 0) {   

  •      String dataValue = request.getParameter("dataValue");   
  •      session.setAttribute(dataName, dataValue);   
  •   }   

  •   out.print("<b>Session 列表</b>");   
  •   Enumeration e = session.getAttributeNames();   

  •   while (e.hasMoreElements()) {   
  •      String name = (String)e.nextElement();   
  •      String value = session.getAttribute(name).toString();   

  •      out.println( name + " = " + value+"<br>");   

  •          System.out.println( name + " = " + value);   
  •    }   
  • %>   

  •   <form action="index.jsp" method="POST">   

  •     名称:<input type=text size=20 name="dataName">   
  •      <br>   

  •     值:<input type=text size=20 name="dataValue">   
  •      <br>   
  •     <input type=submit>   
  •    </form>   
  • </body>   
  • </html>  




<%@ page c %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
System.out.println("================");
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

  
webapps/test/WEB-INF/web.xml

查看复制到剪切板打印





  •     

  • <?xml version="1.0" encoding="ISO-8859-1"?>  

  • <web-app 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"  

  •     version="2.4">  

  •   <display-name>Tomcat Simple Load Balancer Example App</display-name>  

  •   <description>  
  •     Tomcat Simple Load Balancer Example App   

  •   </description>  

  •   <distributable/>  

  • </web-app>  




<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
version="2.4">
<display-name>Tomcat Simple Load Balancer Example App</display-name>
<description>
Tomcat Simple Load Balancer Example App
</description>
<distributable/>
</web-app>

  
其中的 distributable 是关键

8 遇到的问题
1)启动时产生 Error receiving mcast package
经测试发现,我用的ADSL,如果我联网,则会报这个异常,如果断网则不会产生。看来那个
tcpListenAddress="192.168.0.1"
需要使用外网的IP地址才可以。
2)启动异常,一般是端口被占用
请仔细看各个server.xml配置文件,各个端口不能相同

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326189-1-1.html 上篇帖子: no segments* file found in org.apache.lucene.store 下篇帖子: apache 防盗链,限带宽和连接数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表