JBoss EAP 6.x (Enterprise Application Platform) 提供 Cluster 的機制,但需要 apache 的 mod-jk.conf 配合,現在以 Standalone Mode 作為 HA 來測試看看。

Step 01: apache 的設定

APACHE_HOME/conf/httpd.conf

# Include mod_jk's specific configuration file  
Include conf/mod-jk.conf

<VirtualHost 192.168.2.4:80>
    JkMount /* loadbalancer  
</VirtualHost>

 

APACHE_HOME/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

# 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/mod_jk.shm

# Where to put jk logs
JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Mount your applications
JkMount /* loadbalancer

<Location /jkstatus/>  
    JkMount status  
    Order deny,allow  
    Deny from all  
    Allow from all  
</Location> 

 

APACHE_HOME/conf/workers.properties

# Define list of workers that will be used  
# for mapping requests  
worker.list=loadbalancer,status  
 
# Define Node1  
# modify the host as your host IP or DNS name.  
worker.node1.port=8009  
worker.node1.host=192.168.2.4
worker.node1.type=ajp13  
worker.node1.ping_mode=A  
worker.node1.lbfactor=1   
 
# Define Node2  
# modify the host as your host IP or DNS name.  
worker.node2.port=8009
worker.node2.host=192.168.3.118
worker.node2.type=ajp13  
worker.node2.ping_mode=A  
worker.node2.lbfactor=1  
 
# Load-balancing behavior  
worker.loadbalancer.type=lb  
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1  
 
# Status worker for managing load balancer  
worker.status.type=status

 

APACHE_HOME/conf/uriworkermap.properties

# Simple worker configuration file  
 
# Mount the Servlet context to the ajp13 worker  
/jmx-console=loadbalancer  
/jmx-console/*=loadbalancer  
/web-console=loadbalancer  
/web-console/*=loadbalancer  
#/printSession=loadbalancer  
#/printSession/*=loadbalancer

作好設定後,就可以啟動 apache Server。

 

Step 02: 設定第一個 JBoss Standalone : node1 (worker.node1.host=192.168.2.4 )

JBOSS_HOME/standalone/configuration/standalone-ha.xml

<subsystem xmlns="urn:jboss:domain:web:1.5" default-virtual-server="default-host" instance-id="node1" native="false">
    <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
    <connector name="ajp" protocol="AJP/1.3" scheme="http" socket-binding="ajp"/>
    <virtual-server name="default-host" enable-welcome-root="true">
        <alias name="localhost"/>
        <alias name="example.com"/>
    </virtual-server>
</subsystem>

 

Step 03: 設定第二個 JBoss Standalone : node2 (worker.node2.host=192.168.3.118 )

JBOSS_HOME/standalone/configuration/standalone-ha.xml

<subsystem xmlns="urn:jboss:domain:web:1.5" default-virtual-server="default-host" instance-id="node2" native="false">
    <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
    <connector name="ajp" protocol="AJP/1.3" scheme="http" socket-binding="ajp"/>
    <virtual-server name="default-host" enable-welcome-root="true">
        <alias name="localhost"/>
        <alias name="example.com"/>
    </virtual-server>
</subsystem>

 

Step 04: 分別啟動 node1 & node2

standalone.bat -c standalone-ha.xml -Djobss.node.name=node1 -b 192.168.2.4

standalone.bat -c standalone-ha.xml -Djobss.node.name=node2 -b 192.168.3.118

 

Step 05: 建立一個 example.war 檔來測試

WebContent\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>example</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
 <distributable/>
    <description>session test</description>  
</web-app>

 

WebContent\sessionTest.jsp

<%@page    import="java.util.*, java.lang.reflect.*, javax.servlet.http.HttpSession, java.net.*"%>
<%
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort();
    String cPath = request.getContextPath();
    String sPath = request.getServletPath();
    String nodeId = System.getProperty("jboss.server.name");
    String nodeName = System.getProperty("jboss.node.name");
    //String hostName = System.getProperty("jboss.host.name");
    String hostName = System.getProperty("java.rmi.server.hostname");
    String variableString = (String) session
            .getAttribute("SESSION_TEST_VARIABLE");
    int count = 0;

    if (variableString != null) {
        count = Integer.parseInt(variableString);
        count++;
    }

    session.setAttribute("SESSION_TEST_VARIABLE", String.valueOf(count));
%>

<html>
<body bgcolor=white>

    Edit By Polin
    <table columns=1>
        <tr>
            <td><font color=blue>Edit By Polin</td>/
        </tr>
    </table>
    <table columns=2>
        <tr>
            <td><font color=blue>The sessionId is: </font></td>
            <td><%=session.getId()%></td>
        </tr>
        <tr>
            <td><font color=blue>The sessionObj is: </font></td>
            <td><%=session%></td>
        </tr>
        <tr>
            <td><font color=blue>The nodeId is: </font></td>
            <td><%=nodeId%></td>
        </tr>
        <tr>
            <td><font color=blue>The nodeName is: </font></td>
            <td><%=nodeName%></td>
        </tr>
        <tr>
            <td><font color=blue>The server hostName is: </font></td>
            <td><%=hostName%></td>
        </tr>
        <tr>
            <td><font color=blue># of requests placed on session: </font></td>
            <td><%=count%></td>
        </tr>
    </table>
</body>
</html>

 

這樣就可以在 https://192.168.2.4/example 作訪問驗證,apache 會自動將 Session 分配到 192.168.2.4 & 192.168.3.118

 

參考:

http://docs.jboss.org/jbossas/docs/Clustering_Guide/5/html/clustering-http-modjk.html

http://blog.c2b2.co.uk/2013/01/setting-up-jboss-eap-5-cluster-with.html

https://docs.jboss.org/jbossas/docs/Clustering_Guide/5/html_single/#d0e1257

文章標籤
創作者介紹
MIS

MISTECH 技術手抄本

MIS 發表在 痞客邦 PIXNET 留言(0) 人氣()