環境信息:web
系統版本:Win7(64位)apache
Tomcat版本:apache-tomcat-8.0.9windows
Apache版本:httpd-2.4.25-x64-vc14-r1.zip瀏覽器
mod_jk版本:tomcat-connectors-1.2.40-windows-x86_64-httpd-2.4.x.ziptomcat
1、Apache下載安裝服務器
官網地址:http://http://www.apachehaus.com/cgi-bin/download.plxsession
2.解壓Apacheapp
3.修改配置文件httpd.confwebapp
把SRVROOT改爲文件夾路徑 (38行)jvm
Define SRVROOT "D:/apache2.4/Apache24" ServerRoot "${SRVROOT}"
4.安裝Apache
進入DOS命令,到D:\apache2.4\Apache24\bin目錄下 輸入安裝命令:
httpd -k install
5.啓動Apache
有兩種方式:
1.安裝路徑下雙擊ApacheMonitor.exe,就會出現圖形化界面(主界面右下角會出現一個apache的圖標),在這裏能夠啓動/中止/從新啓動apache服務器。 2.在命令行裏輸入 httpd -k start
其餘命令: apache -k uninstall //卸載 httpd -k restart //重啓 httpd -k stop //中止
1 建立Tomcat-Cluster目錄
在D盤下新建Tomcat-Cluster文件夾,在Tomcat-Cluster下新建兩個文件夾Tomcat1,Tomcat2
2.將Tomcat(apache-tomcat-8.0.9文件夾)下的文件拷貝到Tomcat1
在D:\ Tomcat-Cluster\Tomcat1\webapps下,建立TestCluster文件夾,而後將ROOT文件夾下的WEB-INF文件夾拷貝到TestCluster下
打開TestCluster\WEB-INF\web.xml
在</web-app>上面添加<distributable/>
(設置<distributable/>,即代表集羣下某一節點生成或改變的Session,將廣播到該集羣的其它節點)
在TestCluster文件夾下新建index.jsp
4.修改Tomcat1的Server.xml(Tomcat1\conf\Server.xml)
<?xml version='1.0' encoding='utf-8'?> <!--將端口號由8005改成7005 --> <Server port="7005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <!--將端口號由8080改成7080 --> <Connector port="7080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!--將端口號由8009改成7009 --> <Connector port="7009" protocol="AJP/1.3" redirectPort="8443" /> <!--在Engine標籤添加jvmRoute屬性--> <Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat1"> <!--將Cluster標籤的註釋去掉--> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server>
5.配置Tomcat2
將Tomcat1下的文件拷貝到Tomcat2
修改Tomcat2的Server.xml
a.將端口7005,7080,7009改成9005,9080,9009
b.Engine標籤內的jvmRoute改成」 Tomcat2」
c.將Cluster標籤的註釋打開
6.啓動Tomcat集羣
(若是以前配置過Tomcat的環境變量,要先將Tomcat的環境變量刪除)
雙擊Tomcat1\bin\startup.bat、Tomcat2\bin\startup.bat
在瀏覽器下輸入:http://localhost:7080/TestCluster/index.jsp,便可訪問集羣下Tomcat1節點的TestCluster
在瀏覽器下輸入:http://localhost:9080/TestCluster/index.jsp,便可訪問集羣下Tomcat2節點的TestCluster
能夠看到Tomcat一、Tomcat2兩個節點的Session同樣
###** 3、整合Apache與Tomcat集羣**
整合Apache與Tomcat集羣可使用mod_proxy_balancer也可使用mod_jk
這裏咱們使用mod_jk。
1.解壓tomcat-connectors-1.2.40-windows-x86_64-httpd-2.4.x.zip,可獲得」mod_jk.so」。將」mod_jk.so」拷到Apache 2.2\modules目錄下
2.在Apache 2.2\conf下新建workers.properties
worker.list=controller,Tomcat1,Tomcat2 #========Tomcat1======== worker.Tomcat1.port=7009 worker.Tomcat1.host=localhost worker.Tomcat1.type=ajp13 worker.Tomcat1.lbfactor=1 #========Tomcat2======== worker.Tomcat2.port=9009 worker.Tomcat2.host=localhost worker.Tomcat2.type=ajp13 worker.Tomcat2.lbfactor=1 #========controller======== worker.controller.type=lb worker.controller.balanced_workers=Tomcat1,Tomcat2 worker.controller.sticky_session=false
(若是worker.controller.sticky_session設爲true,則同一用戶的同一session始終和一個Tomcat節點交互)
3.在Apache 2.2\conf下新建mod_jk.conf
LoadModule jk_module modules/mod_jk.so JkWorkersFile conf/workers.properties JkMount /* controller
LoadModule - 加載modules/mod_jk.so模塊 JkWorkersFile-workers.properties的位置 JkMount-Apache將localhost下的全部請求轉發給controller處理
4.修改httpd.conf
打開httpd.conf(Apache 2.2\conf\httpd.conf),將尾部的include conf/mod_proxy_balancer.conf註釋掉,並添加一行代碼
include conf/mod_jk.conf
5.測試
重啓Apache,而後在瀏覽器下輸入http://localhost/TestCluster/index.jsp,可看到以下頁面
兩次請求,分別轉發到了Tomcat1節點、Tomcat2節點,而且兩個節點的Session同樣
Apache和Tomcat集羣整合後,Apache是接收請求的入口,它根據設置的轉發請求條件,決定將哪些請求轉發個Tomcat處理
一般的應用場景爲:Apache做爲Web Server,它只負責處理靜態頁面,而動態頁面會交給Tomcat(App Server)處理
###4、批處理Tomcat
因爲一個個去啓動tomcat太麻煩,因此這裏寫了一個腳本進行批處理
@echo off title tomcat集羣管理 :allStart cls echo. echo 1.開啓tomcat echo 2.關閉tomcat echo e.退出 echo. set in= set /p in=請輸入: if "%in%"=="1" goto startAllWork if "%in%"=="2" goto endAllWork if "%in%"=="e" goto allClose :startAllWork echo 正在啓動tomcat集羣服務 echo 啓動tomcat1 set JAVA_HOME=D:\Java\jdk1.8.0_45 set CATALINA_HOME=D:\Tomcat-Cluster\tomcat1 call="%CATALINA_HOME%\bin\startup.bat" echo 啓動tomcat2 set JAVA_HOME=D:\Java\jdk1.8.0_45 set CATALINA_HOME=D:\Tomcat-Cluster\tomcat2 call="%CATALINA_HOME%\bin\startup.bat" echo 啓動完畢! echo. pause goto allStart :endAllWork echo 正在關閉tomcat集羣服務 echo 關閉tomcat1 set CATALINA_HOME=D:\Tomcat-Cluster\tomcat1 call="%CATALINA_HOME%\bin\shutdown.bat" echo 關閉tomcat2 set CATALINA_HOME=D:\Tomcat-Cluster\tomcat2 call="%CATALINA_HOME%\bin\shutdown.bat" echo 關閉完畢! echo. pause goto allStart :allClose echo 按任意鍵退出 pause exit
參考文章: