tomcat負(fù)載均衡配置代碼大全
前面我們介紹了tomcat負(fù)載均衡配置的安裝過(guò)程,接下來(lái)就進(jìn)入我們的重點(diǎn)了——tomcat負(fù)載均衡配置代碼內(nèi)容。當(dāng)然,我們?cè)谡_安裝之后才能進(jìn)行到這一步。之后還包括tomacat集群配置和應(yīng)用配置。相關(guān)的代碼介紹的比較詳細(xì),希望對(duì)大家有所幫助。
◆tomcat負(fù)載均衡配置過(guò)程
(1)在那臺(tái)要安裝apache的服務(wù)器上安裝apache2.0.55,我的安裝路徑為默認(rèn)C:\Program Files\Apache Group\Apache2
(2)安裝后測(cè)試apache能否正常啟動(dòng),調(diào)試到能夠正常啟動(dòng)http://192.168.0.88
(3)下載jk2.0.4后解壓縮文件
(4)將解壓縮后的目錄中的modules目錄中的mod_jk2.so文件復(fù)制到apache的安裝目錄下的modules目錄中,我的為C:\Program Files\Apache Group\Apache2\modules
(5)修改apache的安裝目錄中的conf目錄的配置文件httpd.conf,在文件中加LoadModule模塊配置信息的最后加上一句LoadModule jk2_module modules/mod_jk2.so
(6)分別修改三個(gè)tomcat的配置文件conf/server.xml,修改內(nèi)容如下
修改前
- <!-- An Engine represents the entry point (within Catalina) that processes
 - every request. The Engine implementation for Tomcat stand alone
 - analyzes the HTTP headers included with the request, and passes them
 - on to the appropriate Host (virtual host). -->
 - <!-- You should set jvmRoute to support load-balancing via AJP ie :
 - <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
 - -->
 - <!-- Define the top level container in our container hierarchy -->
 - <Engine name="Catalina" defaultHost="localhost">
 
#p#修改后
- <!-- An Engine represents the entry point (within Catalina) that processes
 - every request. The Engine implementation for Tomcat stand alone
 - analyzes the HTTP headers included with the request, and passes them
 - on to the appropriate Host (virtual host). -->
 - <!-- You should set jvmRoute to support load-balancing via AJP ie :-->
 - <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
 - <!-- Define the top level container in our container hierarchy
 - <Engine name="Catalina" defaultHost="localhost">
 - -->
 
將其中的jvmRoute="jvm1"分別修改為jvmRoute="tomcat1"和jvmRoute="tomcat2"和jvmRoute="tomcat3"
(7)然后重啟三個(gè)tomcat,調(diào)試能夠正常啟動(dòng)。
(8)在apache的安裝目錄中的conf目錄下創(chuàng)建文件workers2.propertie,寫(xiě)入文件內(nèi)容如下
- # fine the communication channel
 - [channel.socket:192.168.0.1:8009]
 - info=Ajp13 forwarding over socket
 - #配置第一個(gè)服務(wù)器
 - tomcatId=tomcat1 #要和tomcat的配置文件server.xml中的jvmRoute="tomcat1"名稱(chēng)一致
 - debug=0
 - lb_factor=1 #負(fù)載平衡因子,數(shù)字越大請(qǐng)求被分配的幾率越高
 - # Define the communication channel
 - [channel.socket:192.168.0.2:8009]
 - info=Ajp13 forwarding over socket
 - tomcatId=tomcat2
 - debug=0
 - lb_factor=1
 - # Define the communication channel
 - [channel.socket:192.168.0.4:8009]
 - info=Ajp13 forwarding over socket
 - tomcatId=tomcat3
 - debug=0
 - lb_factor=1
 - [status:]
 - info=Status worker, displays runtime information.
 - [uri:/jkstatus.jsp]
 - info=Display status information and checks the config file for changes.
 - group=status:
 - [uri:/*]
 - info=Map the whole webapp
 - debug=0
 
#p#(9)在三個(gè)tomcat的安裝目錄中的webapps建立相同的應(yīng)用,我和應(yīng)用目錄名為T(mén)omcatDemo,在三個(gè)應(yīng)用目錄中建立相同 WEB-INF目錄和頁(yè)面index.jsp,index.jsp的頁(yè)面內(nèi)容如下
- <%@ page contentType="text/html; charset=GBK" %>
 - <%@ page import="java.util.*" %>
 - <html><head><title>Cluster App Test</title></head>
 - <body>
 - Server Info:
 - <%
 - out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
 - <%
 - out.println("<br> ID " + session.getId()+"<br>");
 - // 如果有新的 Session 屬性設(shè)置
 - 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">
 - 名稱(chēng):<input type=text size=20 name="dataName">
 - <br>
 - 值:<input type=text size=20 name="dataValue">
 - <br>
 - <input type=submit>
 - </form>
 - </body>
 - </html>
 
(10)重啟apache服務(wù)器和三個(gè)tomcat服務(wù)器,到此tomcat負(fù)載均衡配置完成。測(cè)試負(fù)載均衡先測(cè)試apache,訪問(wèn)http://192.168.0.88/jkstatus.jsp
能否正常訪問(wèn),并查詢其中的內(nèi)容,有三個(gè)tomcat的相關(guān)配置信息和負(fù)載說(shuō)明,訪問(wèn)http://192.168.0.88/TomcatDemo/index.jsp看能夠運(yùn)行,能運(yùn)行,則已建立負(fù)載均衡。















 
 
 
 
 
 
 