Nginx + Tomcat6配置负载均衡

 

1. 安装Nginx和Tomcat,这里假定nginx-0.7.65,Tomcat6.x。(Nginx可以从http://nginx.org/en/download.html下载)

  安装Nginx

  # ./configure

  # make

  # make install

  在这里假定Nginx安装在nginxhost,tomcat分别安装在tomcathost1和tomcathost2上;

2. 修改/usr/local/nginx/conf/nginx.conf文件,

<textarea cols="50" rows="15" name="code" class="xhtml"> http { ... upstream myhost { server tomcathost1:8080 weight=1; server tomcathost2:8080 weight=2; } server { ... location / { proxy_pass http://myhost; } ... } ... }</textarea> 

3. 确定nginxhost和两台tomcathost可以互访,并且两个tomcat机器在一个网段内;

4. 确定两台tomcat host多播已经打开,在Linux机器上可以使用cat /proc/net/dev_mcast检查,如果文件存在基本上就是打开了;另外确定两台tomcat机器的/etc/hosts文件中包含“xxx.xxx.xxx.xxx    hostname”比如“172.17.1.101    tomcathost1”或“172.17.1.102    tomcathost2”;

5. 修改两个tomcat的conf/server.xml文件,对Engine节点分别添加jvmRoute="tomcat1"和jvmRoute="tomcat2",并添加以下内容:

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"&gt; &lt;Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/&gt; &lt;!-- &lt;Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/&gt; --&gt; &lt;Channel className="org.apache.catalina.tribes.group.GroupChannel"&gt; &lt;Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.5" port="45564" frequency="500" dropTime="3000"/&gt; &lt;Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" selectorTimeout="100" maxThreads="6"/&gt; &lt;Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"&gt; &lt;Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/&gt; &lt;/Sender&gt; &lt;Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/&gt; &lt;Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/&gt; &lt;/Channel&gt; &lt;Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/&gt; &lt;ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/&gt; &lt;ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/&gt; &lt;/Cluster&gt;</textarea> 

 

6. 创建一个web app,这里为了简单使用tomcat自带的examples web app(webapps/examples),修改其中的WEB-INF/web.xml文件,在其中<display-name>Servlet and JSP Examples</display-name>节点后添加<distributable/>表明此应用与集群服务器复制Session;

7. 在两个Tomcat的webapps/examples目录下各创建一个test.jsp文件,用来测试Cluster中两个Tomcat的Session复制,代码如下:

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% String mydata = request.getParameter("mydata"); if (mydata != null &amp;&amp; mydata.length() != 0) { session.setAttribute("mydata", mydata); } out.println("request.getLocalAddr(): " + request.getLocalAddr()); out.println("&lt;br/&gt;"); out.println("request.getLocalPort(): " + request.getLocalPort()); out.println("&lt;br/&gt;"); out.println("Session ID: " + session.getId()); out.println("&lt;br/&gt;"); out.println("mydata: " + session.getAttribute("mydata")); %&gt; &lt;form&gt; &lt;input type=text size=20 name="mydata"&gt; &lt;br&gt; &lt;input type=submit&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</textarea> 

8. 启动Tomcat和Nginx,访问http://nginxhost/examples/,可以修改其中Session中的值,并且可以看到两个tomcat的Session中的值是一样的;

 

参考资料:

http://nginx.org/en

http://wiki.nginx.org/Main

 

你可能感兴趣的:(tomcat,nginx,manager,session,server,Interceptor)