Making Cluster in JBoss AS 7 1

Lets try to make cluster in JBoss AS 7.

First of all What is Clustering?

If there are 3-4 nodes that can communicate with each other with the help of some application or by some other mean, we can say these are in a cluster. Or Some load balancing scenario or failOver scenarios in which if one node fails then all other subseqent request will go to another node in that cluster.

Below are the steps which we will be going to follow by which we will make a cluster:

1. Folder Structure

2. Running JBoss7 through standalone.sh

3. Lets make two nodes.

4. How to run these two nodes seperately on different ports.

5. Lets deploy one wb application which makes use of Replicated Ehcache.

    Lets Start:

I have copied a fresh new Jboss7.

1. Below is the Folder Structre:

|_appclient
|_bin
|_bundles
|_docs
|_domain
|_modules
|_standalone
|_welcome-content

We will be making use of Standalone for making the cluster.

2. Running JBoss7 through standalone.sh

Now if i need to run the Jboss, then i simply run the standalone.sh in Terminal.
By default it run on port 8080. And the debug port is 8787.
We can chage the debug port by editing the file standalone.conf

3. Making two nodes.

Now we will going to make 2 Nodes. Just copy the standalone folder and paste it two times. Rename the two pasted standalone to Node1 and another one to Node2. So now the folder structure will get changed to below:

|_appclient
|_bin
|_bundles
|_docs
|_domain
|_modules
|_standalone
|_node1
|_node2
|_welcome-content

Fine upto here. Now we have two nodes, now the question is we have to start these two nodes on different http port i.e. they should listen on different port and also there debig port should be different.

So for this in bin folder make 4 files:

|_node1.sh
|_node1.conf
|_node2.sh
|_node2.conf

How to make above files: Just copy the standalone.sh and standalone.conf two times and changed the name as above. Just edit the node1.conf and node2.conf file and make the debug ports different.

4. How to run these two nodes seperately on different ports.

Now we will need two .sh files for staring the node1 and node2. Since we have created two different folders so we need to tell where they reside.
So make two files named as :

node1_startup.sh and node2_startup.sh

These are new files and below is the source code for them:

node1_startup.sh

#!/bin/sh

./node1.sh -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=/var/opt/jboss-as-7.1.1.Final/node1 -Djboss.node.name=node1 -Djboss.socket.binding.port-offset=100 -Djgroups.udp.mcast_addr=228.7.9.4  -Djgroups.bind_addr=127.0.0.1

node2_startup.sh

#!/bin/sh

./node2.sh -c standalone-ha.xml -b 127.0.0.1 -u 230.0.0.4 -Djboss.server.base.dir=/var/opt/jboss-as-7.1.1.Final/node2 -Djboss.node.name=node2 -Djboss.socket.binding.port-offset=200 -Djgroups.udp.mcast_addr=228.7.9.4  -Djgroups.bind_addr=127.0.0.1

When we start node1 it will listen on port 8180 and node 2 will listen on 8280 since offset for node1 is 100 and node2 is 200.

Now just run theses in terminal.

5. Deploying one wb application which makes use of Replicated Ehcache.

I made one web app which makes use of ehcahce. Below is the ehcache.xml file for that:

<?xml  version="1.0"  encoding="UTF-8"?>
<ehcache>
<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" />
<cacheManagerPeerProviderFactory
    class="com.pract.cluster.cache.EhcacheCacheManagerPeerProviderFactory" />    
    
 <cache
    name="GeneralCache"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="false"
    timeToIdleSeconds="0"
    timeToLiveSeconds="0"
    diskPersistent="false" > 
     <cacheEventListenerFactory
      class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
      <bootstrapCacheLoaderFactory
      class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
      properties="bootstrapAsynchronously=true" />
     </cache> 
</ehcache>

Here the main file is EhcacheCacheManagerPeerProviderFactory. Below is the code for that:

package com.pract.cluster.cache;

import java.io.IOException;
import java.util.Properties;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.distribution.CacheManagerPeerProvider;
import net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory;

public class EhcacheCacheManagerPeerProviderFactory extends RMICacheManagerPeerProviderFactory{

	@Override
    public CacheManagerPeerProvider createCachePeerProvider(CacheManager cacheManager, Properties properties) throws CacheException {
      
        if (properties == null) {
            properties = new Properties();
        }

        String mcastAddr = "230.0.0.4";
        String mcastPort = "47713";

        properties.setProperty("multicastGroupAddress", mcastAddr);
        properties.setProperty("multicastGroupPort", mcastPort);
        properties.setProperty("socketTimeoutMillis", "8000");

        try {
            return createAutomaticallyConfiguredCachePeerProvider(cacheManager, properties);
        } catch (IOException e) {
            throw new CacheException("Failed to create multicast peer provider from Jboss GMS configuration", e);
        }
    }
	
}

I made the build and copy the war file in node1 and node2 deployment folder.
Below are the screen shots.
Opening Web App on node1 and setting the key and value
Opening Web App on node2 and giving key
Web App on node2 after pressing get

One comment on “Making Cluster in JBoss AS 7

  1. Reply eebest8 Feb 20,2015 9:15 pm

    Major thanks for the blog.Much thanks again. Really Great.

Leave a Reply