安装Apache
1 、下载 Apache 的安装程序 apache_2.2.3-win32-x86-no_ssl.exe 后,安装很简单,一直回车。
2 、安装完毕后,将下载的 mod_jk-1.2.20-apache-2.2.3.so改名为mod_jk.so,并复制到 Apache 安装目录下的 modules 目录中。
httpd.conf 配置
3 、然后进入 Apache 安装目录下的 conf 子目录中,打开 httpd.conf 配置文件,在最后插入以下一行: Include conf/mod_jk.conf
4.修改httpd.conf 配置文件Listen 80
,改为Listen 81
,因为80端口号容易冲突。
mod_jk.conf配置
4、 在 conf 子目录下,建立一个新的配置文件:mod_jk.conf ,此文件为 Apache 加载连接器的配置文件,文件名可修改,但要与 httpd.conf 中 Include 的文件名一致,在logs子目录下建立日志文件mod_jk.log,mod_jk.conf内容如下:
# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [ debug/error/info ]
JkLogLevel info
# Select the log format
JkLogStampFormat " [%a %b %d %H:%M:%S %Y] "
# JkOptions indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat
JkRequestLogFormat " %w %V %T "
# Mount your applications
JkMount /* loadbalancer
# You can use external file for mount points.
# It will be checked for updates each 60 seconds.
# The format of the file is: /url = worker
# /examples/* = loadbalancer
JkMountFile conf/uriworkermap.properties
# Add shared memory.
# This directive is present with 1.2.10 and
# later versions of mod_jk , and is needed for
# for load balancing to work properly
JkShmFile logs/jk.shm
# Add jkstatus for managing runtime data
<Location /jkstatus/>
JkMount status
Order deny , allow
Deny from all
Allow from 127.0.0.1
</Location>
注: 该文件中,有两处设置比较重要:LoadModule与JkMount。LoadModule中要正确设置mod_jk的路径;JkMount指明Apache需要将哪些url进行转发,在上面 的设置中,Apache将会把url路径为 /application/* 的请求发送到mod_jk负载均衡。通过该方式,可以配置Apache提供静态Web页面服务,并把动态页面功能转发到JBOSS服务器。如果希望所有的服务都由JBOSS集群服务提供,则把JKMount设置为JkMount /* loadbalancer即可。
workers.properties配置
6、 在conf子目录下,建立一个新的配置文件:workers.properties,此文件为负载平衡的配置文件,文件名不能修改,这是JK默认的名字,内容如下:
# Define list of workers that will be used
# for mapping requests
worker.list=loadbalancer,status
# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8081
worker.node1.host=localhost
worker.node1.type=ajp13
worker.node1.lbfactor=1
#worker.node1.cachesize=1000
# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8081
worker.node2.host=10.225.69.1
worker.node2.type=ajp13
worker.node2.lbfactor=1
#worker.node2.cachesize=1000
#worker.node2.stopped=1
# Define Node3
# modify the host as your host IP or DNS name.
worker.node3.port=8082
worker.node3.host=10.225.69.2
worker.node3.type=ajp13
worker.node3.lbfactor=1
#worker.node3.cachesize=1000
#worker.node3.stopped=1
# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2,node3
worker.loadbalancer.sticky_session=1
#worker.list=loadbalancer
# Status worker for managing load balancer
worker.status.type=status
注:如果不想配置集群,可以删除配置文件中的其他节点的配置,worker.loadbalancer.balance_workers=node1
此配置只写一个节点,这样请求只能转发到此服务器
uriworkermap.properties配置
除了通过 JKMount 设置转发路径外,你可以通过 JkMountFile 详细设置哪些 url 需要通过 mod_jk 进行负载均衡转发。通过在 APACHE_HOME/conf 目录下创建文件 uriworkermap.properties 进行控制,文件格式为
/url=worker_name:
# Simple worker configuration file
# Mount the Servlet context to the ajp13 worker
/*=loadbalancer
!/*.gif=loadbalancer
!/*.jpg=loadbalancer
!/*.png=loadbalancer
!/*.css=loadbalancer
!/*.js=loadbalancer
!/*.htm=loadbalancer
!/*.html=loadbalancer
/prweb/*=loadbalancer
#/jmx-console/*=loadbalancer
#/web-console=loadbalancer
#/web-console/*=loadbalancer
效果
配置完成,访问localhost:8081/status
查看你的集群,页面status状态显示ok,证明tomcat连接成功,记得起tomcat,配置没问题的前提下,tomcat必须启动