httpd版本: httpd-2.4.17
jk版本: tomcat-connectors-1.2.41css
參考來源:html
Apache (1) —— Mac下安裝Apache Httpd到自定義路徑(非/etc/apache2)node
Apache (2) —— Mac 下安裝多個Apache Tomcat實例git
Working with mod_jkgithub
How to Compile Tomcat mod_jk connector on Mac OS X Mavericksapache
Installing mod_jk for Apache 2.2 on Mac OS X 10.5 Leopard瀏覽器
Apache負載均衡配置tomcat
Apache配置反向代理、負載均衡和集羣(mod_proxy方式)cookie
首先咱們參照下面兩篇文章配置好httpd和兩個Tomcat實例
其中Tomcat的兩個實例node-a和node-b分別存放與"./servers/cluster/tomcat/node-a"和"/servers/cluster/tomcat/node-b"下
對應的配置分別爲:
另分別爲連個實例添加jvmRoute的配置
node-a
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
node-b
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
httpd負載均衡Tomcat的方式主要能夠分爲兩類
在Apache的網站上下載tomcat-connectors-1.2.41,而後解壓。
運行命令編譯
$ cd native $ ./configure CFLAGS='-arch x86_64' APXSLDFLAGS='-arch x86_64' --with-apxs=/usr/sbin/apxs $ make $ sudo make install
此處我本地的路徑爲
/Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/bin/apxs
若是執行正常不出錯,末尾幾行的輸出爲
... config.status: creating common/list.mk config.status: creating common/jk_types.h config.status: creating common/config.h config.status: executing depfiles commands config.status: executing libtool commands
而後"./native"下運行make
期間會出現warning,能夠暫時忽略不影響
jk_lb_worker.c:1397:36: warning: address of array 'p->worker->session_cookie_path' will always evaluate to 'true' [-Wpointer-bool-conversion] if (p->worker->session_cookie_path && *p->worker->se... ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ ~~
make執行正常的最後幾行輸出爲
... Making all in common make[1]: Nothing to be done for `all'. Making all in apache-2.0 make[1]: Nothing to be done for `all'.
make install若是在非sudo狀態下,最後會出現錯誤
... install: /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.so: Permission denied apxs:Error: Command failed with rc=4653056 . make[1]: *** [install_dynamic] Error 1 make: *** [install-recursive] Error 1
sudo運行make install
... libtool: install: ranlib /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.a chmod 755 /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/modules/mod_jk.so Please be sure to arrange /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf... make[2]: Nothing to be done for `install-exec-am'. make[2]: Nothing to be done for `install-data-am'.
在node-a下新建文件夾"other",並建立文件workers.properties
# Define two status worker: # - jk-status for read-only use # - jk-manager for read/write use worker.list=jk-status worker.jk-status.type=status worker.jk-status.read_only=true worker.list=jk-manager worker.jk-manager.type=status # We define a load balancer worker # with name "balancer" worker.list=balancer worker.balancer.type=lb worker.balancer.error_escalation_time=0 worker.balancer.max_reply_timeouts=10 worker.balancer.sticky_session=true worker.balancer.sticky_session_force=true # Now we add members to the load balancer First member is "tomcat1", most attributes are inherited from the template "worker.template". worker.balancer.balance_workers=tomcat1 worker.tomcat1.reference=worker.template worker.tomcat1.host=127.0.0.1 worker.tomcat1.port=8019 worker.tomcat1.activation=A # Second member is "tomcat2", most attributes are inherited from the template "worker.template". worker.balancer.balance_workers=tomcat2 worker.tomcat2.reference=worker.template worker.tomcat2.host=127.0.0.1 worker.tomcat2.port=8029 worker.tomcat2.activation=A # Finally we put the parameters worker.template.type=ajp13 worker.template.socket_connect_timeout=5000 worker.template.socket_keepalive=true worker.template.ping_mode=A worker.template.ping_timeout=10000 worker.template.connection_pool_minsize=0 worker.template.connection_pool_timeout=600 worker.template.reply_timeout=300000 worker.template.recovery_options=3
並同時在"./node-a/other/"下建立文件uriworkermap.properties
/*.do=balancer /*.jsp=balancer #/*.gif=balancer #/*.jpg=balancer #/*.png=balancer #/*.css=balancer #/*.js=balancer #/*.htm=balancer #/*.html=balancer #/*.txt=balancer # Optionally filter out all .jpeg files inside that context # For no mapping the url has to start with exclamation (!) !/servlets-examples/*.jpeg=lb # # Mount jkstatus to /jkmanager # For production servers you will need to # secure the access to the /jkmanager url # /jk-manager=jk-manager /jk-status=jk-status
爲httpd.conf,添加配置
# 加載jk配置文件 Include conf/mod_jk.conf
從新啓動httpd,並用瀏覽器訪問"localhost:81",提示"Service Unavailable"錯誤
查看"./node-a/log"目錄下"mod_jk.log"文件
[Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] get_most_suitable_worker::jk_lb_worker.c (1119): all workers are in error state for session QUMQZVRPS1-4MX593EAOD71Y6YAFXUP1-U734UPHI-1 [Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] service::jk_lb_worker.c (1664): All tomcat instances failed, no more workers left (attempt=1, retry=1) [Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] service::jk_lb_worker.c (1675): All tomcat instances are busy or in error state [Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [error] service::jk_lb_worker.c (1680): All tomcat instances failed, no more workers left [Wed Dec 09 15:00:31.403 2015] [13735:4417540096] [info] jk_handler::mod_jk.c (2991): Service error=0 for worker=balancer
嘗試訪問
127.0.0.1:81
獲得正常結果