配置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
修改下面代码:
修改为:
其中 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(稍后介绍)下的配置一致。
然后,对于集群中的每一个节点,我们必须通知它添加一个jvmRoute值到会话cookies中,以便mod_jk可以路由随后的请求。编辑 JBOSS_HOME/server/all/deploy/jbossweb-tomcat50.sar/META-INF/jboss-service.xml 文件,定义UseJK的<attribute></attribute>元素,设置值为true:
xml 代码
<attribute name="UseJK">trueattribute>
到此为止,我们已经成功设置Apache_mod_jk使用sticky-session方式的负载平衡
在%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\目录下添加一个新文件夹\test,并在里面添加如下3个jsp文件:
index.jsp
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
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 < a href ="session.jsp" > here </ a > to check the session valid or not.
24 </ center >
25 </ body >
26 </ html >
27
28
session.jsp
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 < a 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>节点下增加如下代码:
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,并将如下代码添加进去:
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