apache2.4+tomcat8+jk1.2.40集群配置


由於目前很多apache+tomcat集群都是在apache2.2上配置的,Apache2.4的教程幾乎沒有,這里寫一篇記錄下來。

環境:apache2.4.12(Apache Haus編譯版本)、tomcat8.0.15、1.2.40版本的mod_jk.so、jdk-7u71、操作系統winxp

關於這幾個軟件的下載安裝,這里就不多說,注意apache2.4必須安裝在根目錄,mod_jk必須下載Apache對應的版本

軟件

筆者環境

三台xp虛擬機

1.Apache服務器,IP:192.168.116.132

2.tomcat服務器一,IP:192.168.116.133

3.tomcat服務器二,IP:192.168.116.134

 

一、Apache服務器配置

筆者將Apache安裝在了C盤根目錄下

1.將mod_jk.so復制到Apache的modules目錄(筆者為C:\Apache24\modules)

2.在Apache的conf目錄(筆者為C:\Apache24\conf)找到httpd.conf,在文件最后添加

include "C:\Apache24\conf\mod_jk.conf"

3.在Apache的conf目錄(筆者為C:\Apache24\conf)新建文件mod_jk.conf,添加如下內容

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /*.jsp controller

這里強烈建議把JkLogLevel debug加上,否則出錯找不到原因

4.在Apache的conf目錄(筆者為C:\Apache24\conf)新建文件workers.properties,添加如下內容

#server
worker.list = controller     
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.116.133
worker.tomcat1.type=ajp13 
worker.tomcat1.lbfactor=1 

#========tomcat2========
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.116.134
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

#========controller,負載均衡控制器========
worker.controller.type=lb
worker.controller.retries=3
worker.controller.balance_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=false

 

二、tomcat服務器配置

打開tomcat的conf目錄下的server.xml,找到AJP/1.3連接器,端口與workers.properties的worker.[名稱].port設置成一致,默認是8009,在Engine里面添加jvmRoute,名稱與worker.后面的名稱一致,最后去掉<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>兩邊的注釋,兩個服務器配置文件大致一樣,僅jvmRoute設置不一樣。

tomcat服務器一:

 <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>


    <!-- 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).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>

tomcat服務器二:

 <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>


    <!-- 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).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">


      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>

編寫文件部署到tomcat,筆者也懶得寫,大家都抄來抄去的

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.Enumeration"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cluster App Test</title>
</head>
<body>
    Server Info:
    <%
    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.println("<b>Session 列表</b><br>");
        System.out.println("============================");
        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>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>

需要注意的是web.xml里面的必須要加上<distributable/>,然后部署到兩個tomcat服務器中

三、啟動測試運行

先啟動apache服務器,然后啟動2個tomcat服務器

瀏覽器打開http://192.168.116.132/test/index.jsp

然而奇跡並沒有出現,而是出現了404頁面,提示文字為

Not Found

The requested URL /test/index.jsp was not found on this server.

接下來我們要看日志文件,看看問題出在哪里

找到jk的日志文件(C:\Apache24\logs\mod_jk.log)

直接看最后幾行

[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] extension_fix_activation::jk_uri_worker_map.c (485): Checking extension for worker controller of type lb (5)
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] uri_worker_map_dump::jk_uri_worker_map.c (178): uri map dump after extension stripping: id=1, index=0 file='(null)' reject_unsafe=0 reload=60 modified=0 checked=0
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] uri_worker_map_dump::jk_uri_worker_map.c (184): generation 0: size=0 nosize=0 capacity=0
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] uri_worker_map_dump::jk_uri_worker_map.c (184): generation 1: size=1 nosize=0 capacity=4
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] uri_worker_map_dump::jk_uri_worker_map.c (197): NEXT (1) map #0: uri=/*.jsp worker=controller context=/*.jsp source=JkMount type=Wildchar len=6
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] uri_worker_map_switch::jk_uri_worker_map.c (600): Switching uri worker map from index 0 to index 1
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] jk_shm_open::jk_shm.c (168): Shared memory is already opened
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] jk_shm_attach::jk_shm.c (337): Attached shared memory memory [1] size=2560 free=0 addr=0x32bb30
[Sat May 16 20:12:38.718 2015] [2176:1048] [debug] jk_child_init::mod_jk.c (3266): Initialized mod_jk/1.2.40
[Sat May 16 20:12:38.750 2015] [2176:3584] [debug] jk_translate::mod_jk.c (3623): missing uri map for localhost:/test/index.jsp
[Sat May 16 20:12:38.750 2015] [2176:3584] [debug] jk_map_to_storage::mod_jk.c (3791): missing uri map for localhost:/test/index.jsp

通過搜索關鍵字missing uri map for localhost:/找到一篇文章

http://blog.csdn.net/bluecy/article/details/5897103

具體意思是在<VirtualHost>標簽中添加JkMountCopy On,然而作者並沒有說是哪個文件......

筆者也上谷歌搜了,也是添加這個,但就是找不到

筆者最后還是找到了這個文件,文件在C:\Apache24\conf\extra的httpd-vhosts.conf,為什么大家都不說這個啊。。。。。。筆者很是郁悶

默認情況下已經配置了一個虛擬主機

<VirtualHost _default_:80>
DocumentRoot "${SRVROOT}/htdocs"
#ServerName www.example.com:80
</VirtualHost>

然后在中間添加JkMountCopy On就行了(如果有多個要配置多個)

<VirtualHost _default_:80>
DocumentRoot "${SRVROOT}/htdocs"
#ServerName www.example.com:80
JkMountCopy On
</VirtualHost>

如果讀者用的2.2版本,應該不會遇到要配置虛擬主機參數的問題。

保存之后重啟apache服務器,然后在瀏覽器重新打開

http://192.168.116.132/test/index.jsp

F5刷新

可以看到sessionid並沒有發生變化

在名稱和值中分別輸入1、1、2、2、3、3、4、4,可以看到如下結果

上面的測試說明,session已經能夠共享,並且session里面存儲的變量也能夠在集群中復制。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM