Apache与JBoss AS 5 HTTP集群配置

所需组件

  • Apache 2.2.22
  • mod_jk 1.2.3
  • JBOSS 5.1.0GA

安装步骤

  1. 安装Apache2.2.22
  2. mod_jk-1.2.30-httpd-2.2.3.so重命名为mod_jk.so,复制到%APACH_HOME%/modules
  3. 解压缩jboss 5.1.0.GA

配置

%APACHE_HOME%\conf\http.conf文件中添加如下内容:

LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties

# where to find the log file
JkLogFile logs/mod_jk.log
# set the log level  set the log level
JkLogLevel info    
# map to the status server
JkMount /private/admin/mystatus mystatus
# router the request to the right host
JkMount /* balance 

然后再%APACHE_HOME%\conf\下新建workers.properties文件,并添加如下内容:

# define list of workers that will be used
# for mapping requests
worker.list=balance,mystatus

# Define Node1
worker.node1.port=8009
worker.node1.host=xxx.xxx.xxx.xxx
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.socket_timeout=30
worker.node1.socket_keepalive=1

# Define Node2
worker.node2.port=8009
worker.node2.host=yyy.yyy.yyy.yyy
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.socket_timeout=30
worker.node2.socket_keepalive=1

# Load-balancing behaviour
worker.balance.type=lb
worker.balance.balance_workers=node1,node2
worker.balance.sticky_session=1

# Status worker for managing load balancer
worker.mystatus.type=status

以上内容用于配置mod_jk,使其监听两个servlet容器(如JBOSS实例)node1和node2。

在node1中,打开%JBOSS_HOME%/server/all/deploy/jbossweb.sar/server.xml,修改Engine节点:

<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">

同样,在node2中,将该文件的Engine节点改为:

<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node2">

JBOSS默认的session是共享的。

在部署Web应用时,需要在应用程序的web.xml中加入<distributable/>节点,如下所示:

<?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>
    <display-name>Welcome to JBoss</display-name>
    <description>
        Welcome to JBoss
    </description>
    <distributable/>
    <!-- ……其他配置 -->
</web-app>

启动

启动Apache

打开控制台,定位到%APACHE_HOME%,输入如下命令启动Apache服务器:

httpd.exe -k start

输入如下命令可以关闭Apache服务器:

httpd.exe -k shutdown

以集群方式启动JBOSS

以集群方式启动JBOSS的命令如下:

run -c all -g DocsPartition -u 239.255.100.100 -b 0.0.0.0 -Djboss.messaging.ServerPeerID=1

在集群的其他JBOSS实例上,要修改jboss.messaging.ServerPeerID所对应的值,该值要求在集群中是唯一的。

测试

新建一个test.war文件夹,在该文件夹下创建index.jsp文件和WEB-INF文件夹。在WEB-INF文件夹下创建如前面所示的web.xml文件。在index.jsp文件中加入如下内容:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <%@page import="java.util.*"%>
  <%@page import="java.net.InetAddress;"%>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Cluster App Test</title>
 </head>
 <body>
  <%
 InetAddress ip = InetAddress.getLocalHost();
 //out.println(ip.getHostAddress());
 %>
 This is responsed by <font color="red"> <%=ip.getHostAddress() %></font><br>
 Host Name : <font color="red"><%=ip.getHostName() %></font><br>
 Time : <font color="red"><%=new Date() %></font><br>
  <%
 ip = null;
 %>
<br/> <br/> <br/> <br/> 
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
  out.println("<br>Session ID " + session.getId()+"<br>");
  // 如果有新的 Session 属性设置
  String dataName = request.getParameter("dataName");
  if (dataName != null && dataName.length() > 0) {
     String dataValue = request.getParameter("dataValue");
     session.setAttribute(dataName, dataValue);
  }
  out.print("<br/> <b>Session 列表</b>");
  Enumeration e = session.getAttributeNames();
  while (e.hasMoreElements()) {
     String name = (String)e.nextElement();
     String value = session.getAttribute(name).toString();
     out.println( name + " = " + value+"<br>");
         System.out.println( name + " = " + value);
   }
%>
  <form action="index.jsp" method="POST">
    名称:<input type=text size=20 name="dataName">
     <br>
    &nbsp;&nbsp;值:<input type=text size=20 name="dataValue">
     <br>
    <input type=submit>
   </form>  
 </body>
 </html>

将该文件夹复制到集群中所有JBOSS实例的%JBOSS_HOME%/server/all/deploy下,以集群方式重启所有JBOSS实例。

在浏览器中输入http://localhost:8080/test,观察结果。

参考资料

posted @ 2012-11-09 10:33  麒麟.NET  阅读(1894)  评论(0编辑  收藏  举报