在网上看到一位网友的文章,讲述其配置过程,逐步做下来,就完成了配置,为了以后查询方便,自己也把实现的步骤记录下来了。:)
软件环境:
jboss-4.2.2.GA
apache_2.0.59-win32-x86-no_ssl.msi
mod_jk-apache-2.0.59.so
Jdk1.6
配置过程:
2.1 JBoss安装
在两台电脑上分别将JBoss-4.2.2GA.zip文件解压到要安装的位置即可。不需要其他设置。假设两台电脑的IP分别为192.168.0.9,192.168.0.71
2.2 Apache安装
将Apache安装软件apache_2.0.59-win32-x86-no_ssl.msi在一台服务器上,双击安装。本文安装在192.168.0.9的那台电脑上。
3 配置过程
3.1 配置JBoss
分别在两台服务器将如下文件从%JBoss_Home%\server\all\lib里面拷到%JBoss_Home%\server\default\lib目录下:
jbossha.jar(加载org.jboss.ha.framework.server.ClusterPartition)
jgroups.jar(JBoss集群底层通信协议)
jboss-cache-jdk50.jar (加载org.jboss.cache.aop.TreeCacheAop)
再从%JBoss_Home%\server\all\deploy里把cluster-service.xml和jboss-web-cluster.sar
拷贝到%JBoss_Home%\server\default\deploy里面。
编辑192.168.0.71的%JBoss_Home%\server\default\deploy\ jboss-web.deployer\server.xml
修改下面代码:
<Engine name="jboss.web" defaultHost="localhost">
修改为:
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node2">
其中 jvmRoute是用来让apache识别的节点名称,一个节点一个名称,注意不要有重复的(可以结合IP设置)。
同理编辑192.168.0.9的%JBoss_Home%\server\default\deploy\ jboss-web.deployer\server.xml文件,注意把jvmRoute设置为node1,可以设置成别的只要和192.168.0.71的不重复就行,但是要和Apache的workers.properties(稍后介绍)下的配置一致。
编辑%JBoss_Home%\server\default\deploy\jboss-web.deployer\ROOT.war\WEB-INF\web.xml在<web-app>节点下增加如下代码:
<distributable/>
完成后的web.xml文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<distributable/>
<display-name>Welcome to JBoss</display-name>
<description>
Welcome to JBoss
</description>
<servlet>
<servlet-name>Status Servlet</servlet-name>
<servlet-class>org.jboss.web.tomcat.service.StatusServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Status Servlet</servlet-name>
<url-pattern>/status</url-pattern>
</servlet-mapping>
</web-app>
3.2 配置Apache
JBoss的Web集群使用apache的mod_jk,浏览器请求apache服务器,apache服务器根据workers.properties中的配置进行request分发,apache服务器和JBoss中的Tomcat可以用ajp1.3进行通信的,request通过ajp1.3协议的包装被发送到JBoss,JBoss执行后返回结果。
将下载到的mod_jk-apache-2.0.59.so保存到192.168.0.9那台服务器的%Apache%\modules\目录下,并去掉版本号重命名为“mod_jk.so”,
在%Apache%\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 debug
# 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
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>
上述代码的第3行就是配置mod_jk.so模块的路径。
在 %Apache%\conf\目录下添加workers.properties文件,该文件就是配置Apache所要将request转发到的JBoss的路径信息,代码如下:
#
worker.list=loadbalancer,status
# Node1
worker.node1.port=8009
worker.node1.host=192.168.0.9
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.cachesize=10
# Node2
worker.node2.port=8009
worker.node2.host=192.168.0.71
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.cachesize=10
#
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=0
worker.status.type=status
上述代码中:
port是配置JBoss AJP所监听的端口号,可以在%JBoss_Home%\server\default\deploy\jboss-web.deployer \server.xml 看到,如下
1<!-- A AJP 1.3 Connector on port 8009 -->
2<Connector port="8009" address="${jboss.bind.address}"
3 emptySessionPath="true" enableLookups="false"
4 redirectPort="8443" protocol="AJP/1.3"/> host定义JBoss所在机器的IP地址
lbfactor定义该节点的权重,数字越大分发到该节点的request越多
cachesize是servlet线程池的大小(对session复制有影响)
4 测试
在%JBoss_Home%\server\default\deploy\jboss-web.deployer\ROOT.war\目录下添加一个新文件夹\test,并在里面添加如下3个jsp文件:
index.jsp:
<%@ page contentType="text/html;charset=ISO8859_1" %>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body οnlοad="document.form.name.focus()">
<br><br><br>
<center>
The host is : <%=java.net.InetAddress.getLocalHost().toString()%><br>
Your session id is : <%=session.getId()%><br>
Your session detail is : <%=session.toString()%><br>
Your session context is : <%=session.getSessionContext()%><br><br>
Please input your name:<br>
<form action="test_action.jsp" method="POST" name="form">
<input type="input" name="name"/>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>
test_action.jsp:
<%@ page contentType="text/html;charset=ISO8859_1" %>
<html>
<head>
<title>Test Action</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%
String name = request.getParameter("name");
session.setAttribute("name",name);
String host = java.net.InetAddress.getLocalHost().toString();
%>
<body>
<br>
<br>
<center>
The host is : <%=host%><br><br>
Your session id is : <%=session.getId()%><br>
Your session detail is : <%=session.toString()%><br>
Your session context is : <%=session.getSessionContext()%><br><br>
Your name is : <%=name%><br>
This name is set into the session.<br>
Please click <a href="session.jsp">here</a> to check the session valid or not.
</center>
</body>
</html>
session.jsp:
<%@ page contentType="text/html;charset=ISO8859_1" %>
<html>
<head>
<title>Test Action</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<%
String name = null;
if(session.getAttribute("name")!=null)
name = (String)session.getAttribute("name");
String host = java.net.InetAddress.getLocalHost().toString();
%>
<body>
<br>
<br>
<center>
The host is : <%=host%><br>
Your session id is : <%=session.getId()%><br>
Your session detail is : <%=session.toString()%><br>
Your session context is : <%=session.getSessionContext()%><br><br>
<%
if(name!=null){
out.print("Your name is "+name+"<br>");
out.print("The session is valid.");
}
else{
out.print("The session is invalid!!!");
}
%>
<a href="index.jsp">Return!</a>
</center>
</body>
<%
if(session.getAttribute("name")!=null)
session.invalidate();
%>
</html>
启动192.168.0.9上的apache和两台机器上的JBoss。
打开浏览器,输入网址访问apache:http://192.168.0.9/test/index.jsp,回车!可以看到
页面上有JBoss所在机器的IP地址,刷新页面还能看到IP在变化,说明apache将request分发到了不同的JBoss上,在输入框中输入随便一个字符串并提交还能测试JBoss 的session复制情况。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nmgfire007/archive/2008/08/08/2788273.aspx