Managing load balancing and fail over

XINS can also manage load balancing and fail over when you want to invoke your function using CAPI. To do so, your function needs to be started on 2 computers. Our code is based on the CAPI example done in the section called “The client API”.

The first possibility would be to use the GroupDescriptor (javadoc) instead combined with a TargetDescriptor (javadoc):

// Create the descriptor for the service
TargetDescriptor server1 = new TargetDescriptor("http://192.168.0.20:8080/my-project/", 20000);
TargetDescriptor server2 = new TargetDescriptor("http://192.168.0.21:8080/my-project/", 20000);
Descriptor[] servers = {server1, server2};

// Create the descriptor for load balancing
GroupDescriptor descriptor = new GroupDescriptor(GroupDescriptor.RANDOM_TYPE, servers);
/* If you wanted to have fail over replace the above line with
   GroupDescriptor descriptor = new GroupDescriptor(GroupDescriptor.ORDERED_TYPE, servers);*/

The drawback of this method is that it's not flexible, any changes of IP addresses, time-out or if you want to add a new server, you would need to adapt the code, recompile and then redeploy it.

A better method would be to have the URLs and the time-out in a property file. The only code you need then to create your descriptor is

// Build the descriptor
Descriptor descriptor = DescriptorBuilder.build(properties, "capis.myproject");

where properties is the PropertyReader object that contains your parameters.

If you only have one server (for example your computer for testing), set the properties as

capis.myproject=service, http://127.0.0.1:8080/my-project/, 20000

If you want to have load balancing, set the properties as

capis.myproject=group, random, server1, server2
capis.myproject.server1=service, http://192.168.0.20:8080/my-project/, 20000
capis.myproject.server2=service, http://192.168.0.21:8080/my-project/, 20000

If you want to have fail over, set the properties as

capis.myproject=group, ordered, server1, server2
capis.myproject.server1=service, http://192.168.0.20:8080/my-project/, 20000
capis.myproject.server2=service, http://192.168.0.21:8080/my-project/, 20000

It's possible to combine load balancing and fail over. For example in the following code the invocation is balanced between server1 and server2 and if one of both is down back-up will be used.

capis.myproject=group, random, system1, system2
capis.myproject.system1=group, ordered, server1, back-up
capis.myproject.system2=group, ordered, server2, back-up
capis.myproject.server1=service, http://192.168.0.20:8080/my-project/, 20000
capis.myproject.server2=service, http://192.168.0.21:8080/my-project/, 20000
capis.myproject.back-up=service, http://192.168.0.22:8080/my-project/, 20000

The fail-over is not always executed when an error occur, the condition upon which the fail-over is executed are listed in the XINSServiceCaller Javadoc.