1.1. 配置 Apache 装载 mod_jk
Ø a) 修改 APACHE_HOME/conf/httpd.conf ,在该文件的末尾增加下面的命令:
# Include mod_jk's specific configuration file
Include conf/mod-jk.conf
Ø b) 创建文件 APACHE_HOME/conf/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 /application/* 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 即可。
除了通过 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
/jmx-console=loadbalancer
/jmx-console/*=loadbalancer
/web-console=loadbalancer
/web-console/*=loadbalancer
上面是文件 uriworkermap.properties 的示例内容,该配置表明 mod_jk 负载平衡将把 /jmx-console 和 /web-console 请求转发给 JBOSS 集群服务器。
2. 2. 配置 mod_jk 中的集群工作节点
创建文件 conf/workers.properties ,内容如下 :
# 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=8009
worker.node1.host=172.16.11.206
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.cachesize=10
# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host= 172.16.11.207
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.cachesize=10
# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1
#worker.list=loadbalancer
# Status worker for managing load balancer
worker.status.type=status
该文件详细定义了负载均衡将要转发到的目标节点。其中对于节点的命名规则是 worker. 节点名 .xxxx 。所以上述文件定义了两个节点: node1 和 node2 。 8009 端口是 jboss 默认的 ajp 端口,另外需要注意的是 worker. 节点名 .lbfactor 参数,它是节点的负载加权,它的值越大,获得负载的机会就越大。可以根据 node 的硬件性能进行调整。 worker.loadbalancer.balance_workers 列出所有转发的目标节点,即 JBOSS HTTP 集群的所有节点, worker.loadbalancer.sticky_session 参数是指定是否使用粘性 session ,当设置为 0 时,是基于请求的负载均衡,为 1 时是基于用户的负载均衡。
为了使 JBOSS 集群中的每个节点能够接受 mod_jk 转发来的请求,需要在每个节点上配置 jvm ,步骤如下:
1. 1. 编辑文件 JBOSS_HOME/server/all/deploy/jbossweb-tomcat50.sar/server.xml ,找到 <Engine> 元素,增加 jvmRoute 属性,如下:
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">
其中 jvmRoute 的值要与在 Apache 中 workers.properties 定义的名称一致
2. 2. 编辑 JBOSS_HOME/server/all/deploy/jbossweb-tomcat50.sar/META-INF/jboss-service.xml ,找到 name 为 UseJK 的 <attribute> 元素,将其值设置为 True ,如下:
<attribute name="UseJK">true</attribute>