/ 中存储网

Nginx + Tomcat6服务器环境如何配置负载均衡

2013-09-02 17:54:34 来源:IT技术网

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文件,

http {

  …

upstream myhost {

server tomcathost1:8080 weight=1;

server tomcathost2:8080 weight=2;

}

server {

location / {

proxy_pass http://myhost;

}

}

  …

  }

 

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",并添加以下内容:

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"

channelSendOptions="6">

<Manager className="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

<!--

<Manager className="org.apache.catalina.ha.session.BackupManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"

mapSendOptions="6"/>

-->

<Channel className="org.apache.catalina.tribes.group.GroupChannel">

<Membership className="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.5"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="4000"

selectorTimeout="100"

maxThreads="6"/>

<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

 

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复制,

<html>

<head>

<title></title>

</head>

<body>

<%

String mydata = request.getParameter("mydata");

if (mydata != null && mydata.length() != 0) {

session.setAttribute("mydata", mydata);

}

out.println("request.getLocalAddr(): " + request.getLocalAddr());

out.println("<br/>");

out.println("request.getLocalPort(): " + request.getLocalPort());

out.println("<br/>");

out.println("Session ID: " + session.getId());

out.println("<br/>");

out.println("mydata: " + session.getAttribute("mydata"));

%>

<form>

<input type=text size=20 name="mydata">

<br>

<input type=submit>

</form>

</body>

</html>

 

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