日期:2014-05-16  浏览次数:21279 次

Apache2+Tomcat+mod_jk实现负载均衡
Apache(Web Server)负责处理HTML静态内容;

Tomcat(App Server)负责处理动态内容;

其原理如下:

Apache装有一个模块,这个模块叫mod_jk
Apache通过80端口负责解析任何静态web内容
任何不能解析的内容,用表达式告诉mod_jk,让mod_jk派发给相关的app server去解释。

1、环境
Apache  : apache_2.2.17-win32-x86-no_ssl.msi (下载http://httpd.apache.org/download.cgi)
Tomcat :  tomcat6.0.20。建议 下载 http://tomcat.apache.org/download-60.cgi
mod_jk: mod_jk-1.2.31-httpd-2.2.3.so 下载 http://tomcat.apache.org/download-connectors.cgi

2.将下载好的mod_jk-1.2.31-httpd-2.2.3.so放入到${APACHE_HOME}/modules 下

3.装完后,在Apache2.2启动的前提下,打开一个ie输入http://localhost,你将会得到这样的一个页面,就说明你的Apache的安装是成功的。

4.修改${APACHE_HOME}/conf 下的httpd.conf文件

a.修改服务器ip地址 ServerName 10.78.69.87:80

b. 修改apache服务器部署的项目地址 DocumentRoot "F:/apacheserver"

c. 修改访问页面的权限,避免出现403错误
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    allow from all</Directory>


LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so

JKWorkersFile conf/workers.properties

JkLogFile logs/mod_jk.log

<VirtualHost *>  
ServerAdmin localhost
  
DocumentRoot F:/apacheserver
  
ServerName localhost
  
DirectoryIndex index.html index.jsp index.action
  
ErrorLog logs/error_log.txt

CustomLog logs/shsc-access_log.txt common

# Set the jk log level [debug/error/info]    
JkLogLevel info
 
# Set the jk log level [debug/error/info]  
JkLogLevel info

# JkRequestLogFormat  
JkRequestLogFormat "%w %V %T"

JkMount /* tom_control_1  
#静态文件都例外,交给apache来处理  
JkUnMount /*.gif tom_control_1  
JkUnMount /*.jpg tom_control_1  
JkUnMount /*.png tom_control_1  
JkUnMount /*.css tom_control_1  
JkUnMount /*.js tom_control_1  
JkUnMount /*.htm tom_control_1  
JkUnMount /*.html tom_control_1  
JkUnMount /*.xml tom_control_1  
JkUnMount /*.db tom_control_1  
JkUnMount /*.ico tom_control_1  
JkUnMount /*.bmp tom_control_1  
JkUnMount /*.swf tom_control_1  
JkUnMount /*.doc tom_control_1  
JkUnMount /*.ppt tom_control_1  
JkUnMount /*.xls tom_control_1  
JkUnMount /*.docx tom_control_1  
JkUnMount /*.pptx tom_control_1  
JkUnMount /*.xlsx tom_control_1  
#php不交给tomcat1处理  
JkUnMount /*.php tom_control_1  
#jsp不交给tomcat1  
JkUnMount /*.jsp tom_control_1 
 
#tom_control_2只负责jsp的处理  
JkMount /*.jsp tom_control_2 
</VirtualHost>


5.在${APACHE_HOME}/conf目录下新建workers.properties属性文件,添加内容如下:
worker.list=tom_control_1, tom_control_2
#tomcat1实例配置,这里要和tomcat配置文件的service.xml的jvmRoute保持一致
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost 
worker.tomcat1.port=8009  
worker.tomcat1.lbfactor=1 

worker.tomcat2.port=8010 
worker.tomcat2.host=localhost   
worker.tomcat2.type=ajp13  
worker.tomcat2.lbfactor=1

#========tom_control_1负载均衡控制器========   
worker.tom_control_1.type=lb   
worker.tom_control_1.balanced_workers=tomcat1  
worker.tom_control_1.sticky_session_force=1 
#worker.tom_control_1.sticky_session=1