Sample 60: Service dynamic load balancing between 2 Service nodes

<definitions xmlns="http://ws.apache.org/ns/synapse">

    <sequence name="main" onError="errorHandler">
        <in>
            <property name="SERVICE_PREFIX" expression="$axis2:SERVICE_PREFIX"/>
            <send>
                <endpoint name="sdlbEndpoint">
                    <serviceDynamicLoadbalance failover="true"
                                               algorithm="org.apache.synapse.endpoints.algorithms.RoundRobin">
                        <loadBalancerConfig>
                            <services>
                                <service>
                                    <hosts>
                                        <host>test1.synapse.apache.org</host>
                                    </hosts>
                                    <domain>test1.synapse.domain</domain>
                                </service>
                                <service>
                                    <hosts>
                                        <host>test2.synapse.apache.org</host>
                                    </hosts>
                                    <domain>test2.synapse.domain</domain>
                                </service>
                            </services>
                        </loadBalancerConfig>
                    </serviceDynamicLoadbalance>
                </endpoint>
            </send>
            <drop/>
        </in>

        <out>
            <!-- Send the messages where they have been sent (i.e. implicit To EPR) -->
            <send/>
        </out>
    </sequence>

    <sequence name="errorHandler">
        <makefault response="true">
            <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
            <reason value="COULDN'T SEND THE MESSAGE TO THE SERVER."/>
        </makefault>
        <send/>
    </sequence>

</definitions>

Objective: Demonstrate Service-aware dynamic load balancing between a couple of nodes which belong to two different Services. Here, the word Service is used in the context of Cloud Service. A Cloud Service consists of a number of nodes. In this context, a Cloud Service is also associated with a clustering domain. This can be used in place of the DynamicLoadbalanceEndpoint as well.

Prerequisites:
Put the following entries into the /etc/hosts file on your machine.

127.0.0.1   test1.synapse.apache.org
127.0.0.1   test2.synapse.apache.org

Enable clustering in the <Synapse installation directory>/ repository/conf/axis2.xml file. This can be done by setting the "enable" attribute of the "clustering" element.
Enabled groupManagement in the <Synapse installation directory>/ repository/conf/axis2.xml file by copying the following configuration under the clustering section.

<groupManagement enable="true">
    <applicationDomain name="test1.synapse.domain"
                       description="Axis2 group"
                       agent="org.apache.axis2.clustering.management.DefaultGroupManagementAgent"/>
    <applicationDomain name="test2.synapse.domain"
                       description="Axis2 group"
                       agent="org.apache.axis2.clustering.management.DefaultGroupManagementAgent"/>
</groupManagement>

Now we are done with configuring Synapse. Start the Synapse server with sample configuration 60. (i.e. synapse -sample 60)

Now we will look at configuring the Axis2 servers. Deploy the LoadbalanceFailoverService by switching to <Synapse installation directory>/samples/axis2Server/src/LoadbalanceFailoverService directory and running ant. Enable clustering in the <Synapse installation directory>/samples/axis2Server/ repository/conf/axis2.xml by setting the "enable" attribute of the "clustering" element. Set the applicationDomain to test1.synapse.domain. Next Start 2 instances of sample Axis2 server on HTTP ports 9001, 9002 and 9003 and give some unique names to each server.

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

./axis2server.sh -http 9001 -https 9005 -name test1.synapse.apache.org-server1
./axis2server.sh -http 9002 -https 9006 -name test1.synapse.apache.org-server2

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 -Dtrpurl=http://test1.synapse.apache.org:8280/services/LBService1

This client sends 100 requests to the LoadbalanceFailoverService through Synapse. Synapse will route the request to the nodes in the test1.synapse.domain clustering domain 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: test1.synapse.apache.org-server1
[java] Request: 2 ==> Response from server: test1.synapse.apache.org-server2
[java] Request: 3 ==> Response from server: test1.synapse.apache.org-server1
[java] Request: 4 ==> Response from server: test1.synapse.apache.org-server2
[java] Request: 5 ==> Response from server: test1.synapse.apache.org-server1
[java] Request: 6 ==> Response from server: test1.synapse.apache.org-server2
...

Shutdown the Axis2 servers you started in the above steps & edit the <Synapse installation directory>/samples/axis2Server/repository.conf/axis2.xml file, and change the clustering applicationDomain to test2.synapse.domain. Now restart 2 instances of this Axis2 server as follows:

./axis2server.sh -http 9001 -https 9005 -name test2.synapse.apache.org-server1
./axis2server.sh -http 9002 -https 9006 -name test2.synapse.apache.org-server2

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 -Dtrpurl=http://test2.synapse.apache.org:8280/services/LBService1

This client sends 100 requests to the LoadbalanceFailoverService through Synapse. Synapse will route the request to the nodes in the test2.synapse.domain clustering domain in a round-robin manner.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: test2.synapse.apache.org-server1
[java] Request: 2 ==> Response from server: test2.synapse.apache.org-server2
[java] Request: 3 ==> Response from server: test2.synapse.apache.org-server1
[java] Request: 4 ==> Response from server: test2.synapse.apache.org-server2
[java] Request: 5 ==> Response from server: test2.synapse.apache.org-server1
[java] Request: 6 ==> Response from server: test2.synapse.apache.org-server2
...

For the sake of simplicity, we have shown how to run this sample for one clustering domain at a time. However, you can make a copy of the axis2Server directory, and setup both domains simultaneously, and run the sample client to see how the requests are load balanced in a round-robin manner within each clustering domain.