歡迎您光臨本站 註冊首頁

linux下apache+tomcat+jk負載均衡配置全過程

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

linux下apache+tomcat+jk負載均衡配置全過程

小弟剛初學配置的,和大家一起分享,學習!

試驗目的和環境:
一共2台伺服器,均為AS3,一台為:192.168.78.88,計劃作為webserver,接受靜態網頁請求,安裝apache,tomcat,jakarta-tomcat-connectors;另外一台為:192.168.71.77,計劃安裝tomcat,若請求非靜態,則負載均衡到兩個機器一起負擔。

試驗所需軟體:
apache-tomcat-5.5.17.tar.gz,httpd-2.2.2.tar.gz,jakarta-tomcat-connectors-1.2.15-src.tar.gz

安裝過程:由於apache和tomcat為以前多次安裝,故基本步驟省略;
jakarta-tomcat-connectors-1.2.15-src.tar.gz的安裝步驟如下:
1,tar -zxvf jakarta-tomcat-connectors-1.2.15-src.tar.gz;
   cd jakarta-tomcat-connectors-1.2.15-src/jk/native;
2,./configure --with-apxs=/usr/local/apache/bin/apxs;
   make;
   su -c 'make install';
3,cp jakarta-tomcat-connectors-1.2.15-src/jk/native/apache-2.0/mod_jk.so /usr/local/apache/modules

APACHE端的配置:
1,對於/usr/local/apache/conf/worker.properties,一般需要自己添加:
worker.master.type=lb(master作為負載均衡)
worker.master.balance_workers=worker1,worker2(兩台機器上的兩個tomcat作為worker)
worker.master.sticky_session=true
worker.master.sticky_session_force=false

worker.worker1.type=ajp13
worker.worker1.host=192.168.78.88
worker.worker1.port=8009
worker.worker1.socket_timeout=300
worker.worker1.recycle_timeout=0
worker.worker1.lbfactor=1(優先順序)

worker.worke2.type=ajp13
worker.worker2.host=192.168.71.77
worker.worker2.port=8009
worker.worker2.socket_timeout=300
worker.worker2.recycle_timeout=0
worker.worker2.lbfactor=1(同上,優先順序,若數值越大,則優先順序越高)
2,對於/usr/local/apache/conf/httpd.conf
1)特殊配置,# LoadModule foo_module modules/mod_foo.so
    LoadModule jk_module modules/mod_jk.so(載入這個模塊)

   JkWorkersFile /usr/local/apache/conf/worker.properties(用worker.properties的規則來實現jk,指定jk workers.properties的位置,如果是相對路徑,則假定在apache的ServerRoot目錄下。)
   JkLogFile /usr/local/apache/logs/jk.log(指定jk log文件的位置,如果是相對路徑,則假定在apache的ServerRoot目錄下。)
   JkLogLevel info (指定jk log的級別有效的選項有:debug/error/info )
   JkRequestLogFormat     "%w %V %T"
   JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "( 指定log中的時間格式,使用c函數strftime()的格式字元串語法)

2),一般配置:
   ServerAdmin zhq@zhq.edu.cn
   ServerName 192.168.78.88:8888
   DocumentRoot "/usr/local/apache/htdocs"

   <Directory />
    Options FollowSymLinks
    AllowOverride None
   </Directory>   說明:以上是對本身默認的主頁的訪問目錄的設置,不難發現,我已經把主頁的埠改成8888,不合下面的虛擬主機衝突,此處不會遵從下面的負載均衡的分配法則。

   <Directory "/www/docs/virtual-hosts">
   Options Indexes FollowSymLinks
   AllowOverride None
   Order allow,deny
   Allow from all
   </Directory> 說明:此處為我對虛擬主機所在目錄的設置。

3),虛擬主機
NameVirtualHost *:80

<VirtualHost *:80>
ServerAdmin zhq@zhq.edu.cn
DocumentRoot /www/docs/virtual-hosts(虛擬主機的根目錄)
ServerName 192.168.78.88
Alias /jingtai "/www/docs/virtual-hosts/jingtai"(若出現對:"http://192.168.78.88/jingtai"頁面的訪問,則跳轉到:"http://192.168.78.88/www/docs/virtual-hosts/jingtai")
Alias /dongtai "/www/docs/virtual-hosts/dongtai"(同上理)
ErrorLog "/usr/local/apache/logs/error_log combined"

JkUnMount /index.html master(若出現對index.html的訪問,則不用jk到master)
JkMount /index.jsp master(若出現對index.jsp的訪問,則要用master來做負載均衡)
</VirtualHost>

TOMCAT端的配置:
僅用分別在兩台機的tomcat里,對/home/zhq/apache-tomcat-5.5.17/conf/server.xml進行修改,具體如下:
<!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="worker1">
    -->
在jvmRoute=「」中填入指定的worker,在本例中,78.88填入:worker1; 71.77填入:worker2;

說明:
在最新的tomcat版本中該Connector默認情況下是啟用的。啟動tomcat,檢查ajp13服務是否運行;
netstat -a | grep 8009
或則查看tomcat標準輸出上是否有類似的輸出:
Jul 18, 2006 7:47:02 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
《解決方案》

不錯
《解決方案》

Connector- -
                                       

A Connector is a component responsible receiving requests from, and returning responses to, a client application. A Connector performs the following general logic:

Receive a request from the client application.
Create (or allocate from a pool) appropriate Request and Response instances, and populate their properties based on the contents of the received request, as described below.
For all Requests, the connector, protocol, remoteAddr, response, scheme, secure, serverName, serverPort and stream properties MUST be set. The contentLength and contentType properties are also generally set.
For HttpRequests, the method, queryString, requestedSessionCookie, requestedSessionId, requestedSessionURL, requestURI, and secure properties MUST be set. In addition, the various addXxx methods must be called to record the presence of cookies, headers, and locales in the original request.
For all Responses, the connector, request, and stream properties MUST be set.
No additional headers must be set by the Connector for HttpResponses.
Identify an appropriate Container to use for processing this request. For a stand alone Catalina installation, this will probably be a (singleton) Engine implementation. For a Connector attaching Catalina to a web server such as Apache, this step could take advantage of parsing already performed within the web server to identify the Context, and perhaps even the Wrapper, to utilize in satisfying this Request.
Call the invoke() method of the selected Container, passing the initialized Request and Response instances as arguments.
Return any response created by the Container to the client, or return an appropriate error message if an exception of any type was thrown.
If utilizing a pool of Request and Response objects, recycle the pair of instances that was just used.
It is expected that the implementation details of various Connectors will vary widely, so the logic above should considered typical rather than normative.




作用:接受來自客戶端的請求信息(request),並返回相應信息(response)。將接受到的request提交給Engine,由Engine決定提交給哪個Host,Host在選擇Context(也就是選擇哪個應用程序)。。經過一系列處理后返回response。

Tomcat 提供了多種Connector,可以在conf/server.xml中看到(有些已註釋掉了)。每個Connector包括了多個processor用來處理多用戶需求。
《解決方案》

好文章先收下!

[火星人 ] linux下apache+tomcat+jk負載均衡配置全過程已經有849次圍觀

http://coctec.com/docs/service/show-post-9248.html