The Connections.newLoadBalancer()
method returns a
load balancer based on the algorithm you choose. Algorithms include both
round robin for equitably sharing load across local directory servers, and
also failover usually used for switching automatically from an unresponsive
server group to an alternative server group. The algorithms take collections
of connection factories, such as those that you set up for connection
pooling.
The following excerpt shows how to set up round robin load balancing across directory servers.
final List<ConnectionFactory> factories = new LinkedList<ConnectionFactory>(); // Set up a ConnectionFactory for each directory server in the pool as shown in // the previous example, and then set up a load balancer. final RoundRobinLoadBalancingAlgorithm algorithm = new RoundRobinLoadBalancingAlgorithm(factories); final ConnectionFactory factory = Connections.newLoadBalancer(algorithm);
With multiple pools of directory servers, for example in a deployment across multiple data centers, also use fail over load balancing. Fail over load balancing directs all requests to the first (preferred) pool of servers until problems are encountered with the connections to that pool. Then it fails over to the next pool in the list. Therefore in each data center you can set up round robin load balancing, and then set up fail over load balancing across data centers.
// localFactory: ConnectionFactory to servers in the local data center // remoteFactory: ConnectionFactory for servers in a remote data center // localFactory and remoteFactory use round robin load balancing "internally". final List<ConnectionFactory> factories = Arrays.asList(localFactory, remoteFactory); final FailoverLoadBalancingAlgorithm algorithm = new FailoverLoadBalancingAlgorithm(factories); final ConnectionFactory factory = Connections.newLoadBalancer(algorithm);
The algorithms also include constructors that let you adjust timeouts and so forth.