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
获得正常结果