似水无痕 ^o^ 迪迪派

^o^ 努力学习 ^o^ 努力工作 ^o^ 努力养老婆 ^o^

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  5 随笔 :: 0 文章 :: 35 评论 :: 0 Trackbacks

   说起JBoss集群好像很高深的样子,其实一点也不恐怖,建立一个集群很简单,难的是怎么针对具体的应用优化服务器和实现负载均衡(学习中^o^)。本文将带领大家在JBoss的default目录下建立一个简单的Web方面的JBoss集群。

      搭建环境

   两台电脑 //其实一台也够了,不过需要修改其中一个JBoss监听的端口,要不然有冲突
   jboss-4.0.4.GA
   apache_2.0.59 //我使用的是apache_2.0.59-win32-x86-no_ssl,其实无所谓的
   mod_jk-apache-2.0.59.so //Apache请求分发的模块,官网上可以找到

   首先安装JBoss,我用的是zip格式的,直接解压缩就可以了,在两台电脑里分别安装。

   接下来在其中一台电脑上(这里是192.168.0.2)安装Apache(其实应该再找一台电脑来安装的),很简单,略过^o^(偶是不是很懒^o^)

      配置JBoss

   
假设两台电脑的IP分别为192.168.0.2和192.168.0.3。我们这里使用JBoss的default目录。

   将如下文件从%JBoss_Home%\server\all\lib里面拷到%JBoss_Home%\server\default\lib目录下:

   jbossha.jar(加载org.jboss.ha.framework.server.ClusterPartition)
   jgroups.jar(JBoss集群底层通信协议)
   jboss-cache.jar(加载org.jboss.cache.aop.TreeCacheAop)

   还要从%JBoss_Home%\server\all\deploy里把cluster-service.xml和tc5-cluster.sar拷贝到%JBoss_Home%\server\default\deploy里面。

   编辑192.168.0.2的%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\server.xml 
   修改下面代码:
<Engine name="jboss.web" defaultHost="localhost">

   修改为:
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">

   其中 jvmRoute是用来让apache识别的节点名称,一个节点一个名称,注意不要有重复的(可以结合IP设置)。

   同理编辑192.168.0.3的%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\server.xml
注意把jvmRoute设置为node2,可以设置成别的只要和192.168.0.2的不重复就行,但是要和Apache的workers.properties(稍后介绍)下的配置一致。

   在%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\目录下添加一个新文件夹\test,并在里面添加如下3个jsp文件:

index.jsp
 1<%@ page contentType="text/html;charset=ISO8859_1" %> 
 2
 3<html>
 4<head>
 5<title>Test</title>
 6<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 7</head>
 8
 9<body onload="document.form.name.focus()">
10<br><br><br>
11<center>
12The host is : <%=java.net.InetAddress.getLocalHost().toString()%><br>
13Your session id is : <%=session.getId()%><br>
14Your session detail is : <%=session.toString()%><br>
15Your session context is : <%=session.getSessionContext()%><br><br>
16Please input your name:<br>
17<form action="test_action.jsp" method="POST" name="form">
18    <input type="input" name="name"/>
19    <input type="submit" value="提交">
20</form>
21</center>
22</body>
23</html>
24
25

test_action.jsp
 1<%@ page contentType="text/html;charset=ISO8859_1" %> 
 2
 3<html>
 4<head>
 5<title>Test Action</title>
 6<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 7</head>
 8<%
 9    String name = request.getParameter("name");
10    session.setAttribute("name",name);
11    String host = java.net.InetAddress.getLocalHost().toString();
12
%>
13<body>
14<br>
15<br>
16<center>
17The host is : <%=host%><br><br>
18Your session id is : <%=session.getId()%><br>
19Your session detail is : <%=session.toString()%><br>
20Your session context is : <%=session.getSessionContext()%><br><br>
21Your name is : <%=name%><br>
22This name is set into the session.<br>
23Please click <href="session.jsp">here</a> to check the session valid or not.
24</center>
25</body>
26</html>
27
28

session.jsp
 1<%@ page contentType="text/html;charset=ISO8859_1" %> 
 2
 3<html>
 4<head>
 5<title>Test Action</title>
 6<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 7</head>
 8<%
 9    String name = null;
10    if(session.getAttribute("name")!=null)
11        name = (String)session.getAttribute("name");
12    String host = java.net.InetAddress.getLocalHost().toString();
13
%>
14<body>
15<br>
16<br>
17<center>
18The host is : <%=host%><br>
19Your session id is : <%=session.getId()%><br>
20Your session detail is : <%=session.toString()%><br>
21Your session context is : <%=session.getSessionContext()%><br><br>
22<%
23    if(name!=null){
24        out.print("Your name is "+name+"<br>");
25        out.print("The session is valid.");
26    }
27    else{
28        out.print("The session is invalid!!!");
29    }
30
%>
31<href="index.jsp">Return!</a>
32</center>
33</body>
34<%
35    if(session.getAttribute("name")!=null)
36        session.invalidate();
37
%>
38</html>
39
40

   编辑%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\WEB-INF\web.xml在<web-app>节点下增加如下代码:
1<distributable/>
   完成后web.xml代码如下:
 1<?xml version="1.0" encoding="ISO-8859-1"?>
 2
 3<!DOCTYPE web-app
 4    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 5    "http://java.sun.com/dtd/web-app_2_3.dtd">
 6
 7<web-app>
 8  <distributable/>
 9  <display-name>Welcome to JBoss</display-name>
10  <description>
11     Welcome to JBoss
12  </description>
13  <servlet>
14    <servlet-name>Status Servlet</servlet-name>
15    <servlet-class>org.jboss.web.tomcat.tc5.StatusServlet</servlet-class>
16  </servlet>
17  <servlet-mapping>
18    <servlet-name>Status Servlet</servlet-name>
19    <url-pattern>/status</url-pattern>
20  </servlet-mapping>
21</web-app>
22
   看到第8行了吗?^o^

   到这里JBoss就配置完成了^o^

      配置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保存到%Apache%\modules\目录下,并去掉版本号重命名为“mod_jk.so”,如果不改也可以在mod-jk.conf文件(稍后介绍)里修改配置。

   在%Apache%\conf\目录下新建mod-jk.conf,并将如下代码添加进去:
 1# Load mod_jk module
 2# Specify the filename of the mod_jk lib
 3LoadModule jk_module modules/mod_jk.so
 4# Where to find workers.properties
 5JkWorkersFile conf/workers.properties
 6# Where to put jk logs
 7JkLogFile logs/mod_jk.log
 8# Set the jk log level [debug/error/info]
 9JkLogLevel debug
10# Select the log format
11JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
12# JkOptions indicates to send SSK KEY SIZE
13JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
14# JkRequestLogFormat
15JkRequestLogFormat "%w %V %T"
16# Mount your applications
17#JkMount /application/* loadbalancer
18JkMount /* loadbalancer
19# You can use external file for mount points.
20# It will be checked for updates each 60 seconds.
21# The format of the file is: /url=worker
22# /examples/*=loadbalancer
23JkMountFile conf/uriworkermap.properties
24# Add shared memory.
25# This directive is present with 1.2.10 and
26# later versions of mod_jk, and is needed for
27# for load balancing to work properly
28JkShmFile logs/jk.shm
29# Add jkstatus for managing runtime data
30<Location /jkstatus/>
31JkMount status
32Order deny,allow
33Deny from all
34Allow from 127.0.0.1
35</Location>
   上述代码的第3行就是配置mod_jk.so模块的路径。

   编辑%Apache%\conf\httpd.conf在最后一行添加如下代码:
1Include conf/mod-jk.conf

   在 %Apache%\conf\目录下添加workers.properties文件,该文件就是配置Apache所要将request转发到的JBoss的路径信息,代码如下:
 1# 定义request所要转发到的节点
 2worker.list=loadbalancer,status
 3# 定义节点 Node1
 4worker.node1.port=8009
 5worker.node1.host=192.168.0.2
 6worker.node1.type=ajp13
 7worker.node1.lbfactor=1
 8worker.node1.cachesize=10
 9# 定义节点 Node2
10worker.node2.port=8009
11worker.node2.host=192.168.0.3
12worker.node2.type=ajp13
13worker.node2.lbfactor=1
14worker.node2.cachesize=10
15# 负载配置
16worker.loadbalancer.type=lb
17worker.loadbalancer.balance_workers=node1,node2
18worker.loadbalancer.sticky_session=0
19worker.status.type=status

   上述代码中:
   port是配置JBoss AJP所监听的端口号,可以在%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\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复制有影响)

   到这里所有的配置就全部完成啦^o^

   启动192.168.0.2上的apache和两台机器上的JBoss。
   打开浏览器,输入网址访问apache:http://192.168.0.2/test/index.jsp,回车!看到了吧?

   页面上有JBoss所在机器的IP地址,刷新页面还能看到IP在变化,说明apache将request分发到了不同的JBoss上,在输入框中输入随便一个字符串并提交还能测试JBoss 的session复制情况。

^o^
posted on 2007-05-17 21:33 ddpie 阅读(6611) 评论(8)  编辑  收藏 所属分类: JBoss

评论

# re: 在default目录下快速配置JBoss集群(Web方面) 2007-12-20 16:15 Alan_xu
很好的文章,uriworkermap.properties文件没有创建,需要创建.不然会报错.  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面)[未登录] 2008-02-20 15:58 aa
e   回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面)[未登录] 2008-02-20 15:59 x小邱
恩 ,不错  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面)[未登录] 2009-02-18 10:34 joe
谢谢你的文章  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面)[未登录] 2009-08-23 15:06
很详细的介绍,谢谢分享,我决定自己试一试
  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面) 2011-01-03 14:50 网络时空
楼主的文章介绍得太好了,我正需要这篇文章啊。。我的qq号是172145286,想加楼主为好友,交流一下,行否?谢谢了  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面) 2011-01-03 18:09 网络时空
我看在有一篇文章中提到还要进行下面的处理,你的上面文章中,怎么没有这一点呢?另外如下jboss-web.xml文件是在哪个具体目录中呢?我是用的jboss4.2.GA?

在jboss-web.xml中增加以下内容:

<?xml version="1.0"?>

<jboss-web>

<context-root>trms</context-root>

<replication-config>

<replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger>

<replication-granularity>SESSION</replication-granularity>

<replication-field-batch-mode>true</replication-field-batch-mode>

</replication-config>

</jboss-web>  回复  更多评论
  

# re: 在default目录下快速配置JBoss集群(Web方面)[未登录] 2012-12-04 09:15 至尊宝
我按照你的操作还是没有集群成功  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: