Sample 154: Load Balancing with Proxy Services

<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="LBProxy" transports="http" startOnLoad="true"> <target faultSequence="errorHandler"> <inSequence> <send> <endpoint> <session type="simpleClientSession"/> <loadbalance algorithm="org.apache.synapse.endpoints.algorithms.RoundRobin"> <endpoint> <address uri="http://localhost:9001/services/LBService1"> <enableAddressing/> <suspendDurationOnFailure>20</suspendDurationOnFailure> </address> </endpoint> <endpoint> <address uri="http://localhost:9002/services/LBService1"> <enableAddressing/> <suspendDurationOnFailure>20</suspendDurationOnFailure> </address> </endpoint> <endpoint> <address uri="http://localhost:9003/services/LBService1"> <enableAddressing/> <suspendDurationOnFailure>20</suspendDurationOnFailure> </address> </endpoint> </loadbalance> </endpoint> </send> <drop/> </inSequence> <outSequence> <send/> </outSequence> </target> <publishWSDL uri="file:repository/conf/sample/resources/proxy/sample_proxy_2.wsdl"/> </proxy> <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 how to use a proxy service as a load balancer

Pre-requisites

  • Deploy the LoadbalanceFailoverService in the sample Axis2 server (go to samples/axis2Server/src/LoadbalanceFailoverService and run 'ant')
  • Start 3 instances of the Axis2 server on different ports as follows
    ./axis2server.sh -http 9001 -https 9005 -name MyServer1
    ./axis2server.sh -http 9002 -https 9006 -name MyServer2
    ./axis2server.sh -http 9003 -https 9007 -name MyServer3
  • Start Synapse using the configuration numbered 154 (repository/conf/sample/synapse_sample_154.xml)
    Unix/Linux: sh synapse.sh -sample 154
    Windows: synapse.bat -sample 154

Executing the Client

This sample is similar to sample 54. The only notable difference is the use of a proxy service.

Execute the client as follows.

ant loadbalancefailover -Dmode=session -Dtrpurl=http://localhost:8280/services/LBProxy

You will get an output similar to the following.

[java] Request: 1 Session number: 1 Response from server: MyServer3 [java] Request: 2 Session number: 2 Response from server: MyServer2 [java] Request: 3 Session number: 0 Response from server: MyServer1 [java] Request: 4 Session number: 2 Response from server: MyServer2 [java] Request: 5 Session number: 1 Response from server: MyServer3 [java] Request: 6 Session number: 2 Response from server: MyServer2 [java] Request: 7 Session number: 2 Response from server: MyServer2 [java] Request: 8 Session number: 1 Response from server: MyServer3 [java] Request: 9 Session number: 0 Response from server: MyServer1 [java] Request: 10 Session number: 0 Response from server: MyServer1 ...

You can see that session ID 0 is always directed to the server named MyServer1. That means session ID 0 is bound to MyServer1. Similarly session 1 and 2 are bound to MyServer3 and MyServer2 respectively.

Back to Catalog