JBoss,Apache配置集羣

在網上看到一位網友的文章,講述其配置過程,逐步做下來,就完成了配置,爲了以後查詢方便,自己也把實現的步驟記錄下來了。:)

 

軟件環境:

jboss-<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />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 onload="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複製情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章