This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Sample 57: Dynamic load balancing between 3 nodes

Objective: Demonstrate simple dynamic load balancing among a set of nodes.

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <sequence name="main" onError="errorHandler">
        <in>
            <send>
                <endpoint name="dynamicLB">
                    <dynamicLoadbalance failover="true"
                                           algorithm="org.apache.synapse.endpoints.algorithms.RoundRobin">
                        <membershipHandler
                                class="org.apache.synapse.core.axis2.Axis2LoadBalanceMembershipHandler">
                            <property name="applicationDomain" value="apache.axis2.application.domain"/>
                        </membershipHandler>
                    </dynamicLoadbalance>
                </endpoint>
            </send>
            <drop/>
        </in>
        <out>
            <send/>
        </out>
    </sequence>
    <sequence name="errorHandler">
        <makefault response="true">
            <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
            <reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/>
        </makefault>
        <send/>
    </sequence>
</definitions>

Prerequisites:

Enable clustering and group management in the <ESB_HOME>/repository/conf/axis2/axis2.xml file. This can be done by setting the "enable" attribute of the "clustering" and "groupManagement" elements. Also provide the IP address of your machine as the values of the "mcastBindAddress" and "localMemberHost" parameters. For more information on clustering, see the Clustering and Deployment Guide.

Start ESB with sample configuration 57: i.e. wso2esb-samples.sh -sn 57

Deploy the LoadbalanceFailoverService by switching to <ESB_HOME>/samples/axis2Server/src/LoadbalanceFailoverService directory and running ant.

Enable clustering in the <ESB_HOME>/samples/axis2Server/repository/conf/axis2.xml file. This can be done by setting the "enable" attribute of the "clustering" element. Also provide the IP address of your machine as the values of the "mcastBindAddress" and "localMemberHost" parameters. Make sure that the "applicationDomain" of the membershipHandler is the same as the domain name specified in the axis2.xml files of the Axis2 servers. Then start three instances of the sample Axis2 server on HTTP ports 9001, 9002, and 9003, and give unique names to each server.

Example commands to run sample Axis2 servers from the <ESB_HOME>/samples/axis2Server directory in Linux are listed below:

./axis2server.sh -http 9001 -https 9005 -name MyServer1
./axis2server.sh -http 9002 -https 9006 -name MyServer2
./axis2server.sh -http 9003 -https 9007 -name MyServer3

Now we are done with setting up the environment for load balance sample. Start the load balance and failover client using the following command:

ant loadbalancefailover -Di=100

This client sends 100 requests to the LoadbalanceFailoverService, which distributes the load among the three nodes mentioned in the configuration in a round-robin manner. LoadbalanceFailoverService appends the name of the server to the response, so that client can determine which server has processed the message. If you examine the console output of the client, you can see that requests are processed by three servers as follows:

[java] Request: 1 ==> Response from server: MyServer1
[java] Request: 2 ==> Response from server: MyServer2
[java] Request: 3 ==> Response from server: MyServer3
[java] Request: 4 ==> Response from server: MyServer1
[java] Request: 5 ==> Response from server: MyServer2
[java] Request: 6 ==> Response from server: MyServer3
[java] Request: 7 ==> Response from server: MyServer1
...

Now run the client without the -Di=100 parameter (ant loadbalancefailover) to send infinite requests. While running the client, shut down the server named MyServer1. You can observe that requests are only distributed among MyServer2 and MyServer3 after shutting down MyServer1. Console output before and after shutting down MyServer1 is listed below (MyServer1 was shutdown after request 63):

...
[java] Request: 61 ==> Response from server: MyServer1
[java] Request: 62 ==> Response from server: MyServer2
[java] Request: 63 ==> Response from server: MyServer3
[java] Request: 64 ==> Response from server: MyServer2
[java] Request: 65 ==> Response from server: MyServer3
[java] Request: 66 ==> Response from server: MyServer2
[java] Request: 67 ==> Response from server: MyServer3
...

Now restart MyServer1. You can observe that requests will be again sent to all three servers.