免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1454 | 回复: 0
打印 上一主题 下一主题

JBoss+Apache集群配置 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-04 17:48 |只看该作者 |倒序浏览
搭建环境
两台
电脑
//其实一台也够了,不过需要修改其中一个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(其实应该再找一台电脑来安装的),很
简单

配置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


源码
打印
%@ page contentType="text/html;charset=ISO8859_1" %>                                                                                                         html>                                                                         head>                                                                         title>Testtitle>                                                            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="提交">                                         form>                                                                        center>                                                                      body>                                                                        html>                                                                                                
                                                                             
                                                                       
                                                                       
Test                                                         
         
                                                                     
                                                                             
                                   
                                                                 
                                                                     
The host is :
        
Your session id is :
                                
Your session detail is :
                        
Your session context is :
           
Please input your name:
                                                  
                    
                                            
                                          
                                                                     
                                                                    
                                                                     
                                                                     

test_action.jsp


源码
打印
%@ page contentType="text/html;charset=ISO8859_1" %>                                                                                                                     html>                                                                               head>                                                                               title>Test Actiontitle>                                                           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">herea> to check the session valid or not.       center>                                                                            body>                                                                              html>                                                                                                            
                                                                                   
                                                                             
                                                                             
Test Action                                                         
               
                                                                           
                                                                                 
                                                                             
                                                                              
                                                                              
                                                                           
The host is :
                                                   
Your session id is :
                                      
Your session detail is :
                              
Your session context is :
                 
Your name is :
                                                      
This name is set into the session.
                                             
Please click here to check the session valid or not.     
                                                                          
                                                                           
                                                                           
session.jsp

源码
打印
%@ page contentType="text/html;charset=ISO8859_1" %>                                                                                                                      html>                                                                               head>                                                                               title>Test Actiontitle>                                                           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>                                                                                                            
                                                                                   
                                                                             
                                                                             
Test Action                                                         
               
                                                                           
                                                                                 
                                                                             
                                                                              
                                                                              
                                                                           
The host is :
                                                        
Your session id is :
                                      
Your session detail is :
                              
Your session context is :
                 
                                                                                 
Return!                                                   
                                                                          
                                                                           
                                                                                 
                                                                           
编辑%JBoss_Home%\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\WEB-INF\web.xml在节点下增加如下代码:

源码
打印
distributable/>  
   
完成
后web.xml代码如下:

源码
打印
xml version="1.0" encoding="ISO-8859-1"?>                                                                                                                                  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 JBossdisplay-name>                                        description>                                                                           Welcome to JBoss                                                                  description>                                                                       servlet>                                                                              servlet-name>Status Servletservlet-name>                                          servlet-class>org.jboss.web.tomcat.tc5.StatusServletservlet-class>              servlet>                                                                           servlet-mapping>                                                                      servlet-name>Status Servletservlet-name>                                          url-pattern>/statusurl-pattern>                                                 servlet-mapping>                                                                 web-app>                                                                                                                  
                                                                                   
                                    
                                                                                   
                                                                          
                                                                  
  Welcome to JBoss                                    
                                                                     
     Welcome to JBoss                                                              
                                                                     
                                                                          
    Status Servlet                                    
    org.jboss.web.tomcat.tc5.StatusServlet         
                                                                        
                                                                  
    Status Servlet                                    
    /status                                             
                                                                 
                                                                        

  看到第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,并将如下代码添加进去:

源码
打印
# 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>                                                           # 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                           
                                               
JkMount status                                                      
Order deny,allow                                                   
Deny from all                                                      
Allow from 127.0.0.1                                                
                                                         

   上述代码的第3行就是配置mod_jk.so模块的路径。

    编辑%Apache%\conf\httpd.conf在最后一行添加如下代码:
Include conf/mod-jk.conf  Include conf/mod-jk.conf
  在 %Apache%\conf\目录下添加workers.properties文件,该文件就是配置Apache所要将request转发到的JBoss的路径
信息
,代码如下:

源码
打印
# 定义request所要转发到的节点                             worker.list=loadbalancer,status                         # 定义节点 Node1                                         worker.node1.port=8009                                  worker.node1.host=192.168.0.2                           worker.node1.type=ajp13                                 worker.node1.lbfactor=1                                 worker.node1.cachesize=10                               # 定义节点 Node2                                         worker.node2.port=8009                                  worker.node2.host=192.168.0.3                           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     # 定义request所要转发到的节点                           
worker.list=loadbalancer,status                       
# 定义节点 Node1                                       
worker.node1.port=8009                                
worker.node1.host=192.168.0.2                        
worker.node1.type=ajp13                              
worker.node1.lbfactor=1                              
worker.node1.cachesize=10                             
# 定义节点 Node2                                       
worker.node2.port=8009                                
worker.node2.host=192.168.0.3                        
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\jbossweb-tomcat55.sar\server.xml 看到,如下

源码
打印
                     Connector port="8009" address="${jboss.bind.address}"                 emptySessionPath="true" enableLookups="false"                 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复制情况。
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56731/showart_457823.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP