jboss和apache的集群配置

说起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 >
12 The host is :  <% = java.net.InetAddress.getLocalHost().toString() %> < br >
13 Your session id is :  <% = session.getId() %> < br >
14 Your session detail is :  <% = session.toString() %> < br >
15 Your session context is :  <% = session.getSessionContext() %> < br >< br >
16 Please 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 >
17 The host is :  <% = host %> < br >< br >
18 Your session id is :  <% = session.getId() %> < br >
19 Your session detail is :  <% = session.toString() %> < br >
20 Your session context is :  <% = session.getSessionContext() %> < br >< br >
21 Your name is :  <% = name %> < br >
22 This name is set into the session. < br >
23 Please 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 >
18 The host is :  <% = host %> < br >
19 Your session id is :  <% = session.getId() %> < br >
20 Your session detail is :  <% = session.toString() %> < br >
21 Your 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
 3 LoadModule jk_module modules/mod_jk.so
 4 # Where to find workers.properties
 5 JkWorkersFile conf/workers.properties
 6 # Where to put jk logs
 7 JkLogFile logs/mod_jk.log
 8 # Set the jk log level [debug/error/info]
 9 JkLogLevel debug
10 # Select the log format
11 JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
12 # JkOptions indicates to send SSK KEY SIZE
13 JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
14 # JkRequestLogFormat
15 JkRequestLogFormat "%w %V %T"
16 # Mount your applications
17 #JkMount /application/* loadbalancer
18 JkMount /* 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
23 JkMountFile 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
28 JkShmFile logs/jk.shm
29 # Add jkstatus for managing runtime data
30 < Location  /jkstatus />
31 JkMount status
32 Order deny,allow
33 Deny from all
34 Allow from 127.0.0.1
35 </ Location >
   上述代码的第3行就是配置mod_jk.so模块的路径。

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值