Class LBSolrClient

java.lang.Object
org.apache.solr.client.solrj.SolrClient
org.apache.solr.client.solrj.impl.LBSolrClient
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable
Direct Known Subclasses:
LBAsyncSolrClient

public abstract class LBSolrClient extends SolrClient
This "LoadBalanced Http Solr Client" is a load balancing wrapper around an Http Solr Client. This is useful when you have multiple Solr endpoints and requests need to be Load Balanced among them.

Do NOT use this class for indexing in leader/follower scenarios since documents must be sent to the correct leader; no inter-node routing is done.

In SolrCloud (leader/replica) scenarios, it is usually better to use CloudSolrClient, but this class may be used for updates because the server will forward them to the appropriate leader.

It offers automatic failover when a server goes down, and it detects when the server comes back up.

Load balancing is done using a simple round-robin on the list of endpoints. Endpoint URLs are expected to point to the Solr "root" path (i.e. "/solr").

 SolrClient client = new LBSolrClient.Builder(httpSolrClient,
         new LBSolrClient.Endpoint("http://host1:8080/solr"), new LBSolrClient.Endpoint("http://host2:8080/solr"))
     .build();
 
Users who wish to balance traffic across a specific set of replicas or cores may specify each endpoint as a root-URL and core-name pair. For example:
 SolrClient client = new LBSolrClient.Builder(httpSolrClient,
         new LBSolrClient.Endpoint("http://host1:8080/solr", "coreA"),
         new LBSolrClient.Endpoint("http://host2:8080/solr", "coreB"))
     .build();
 

If a request to an endpoint fails by an IOException due to a connection timeout or read timeout then the host is taken off the list of live endpoints and moved to a 'dead endpoint list' and the request is resent to the next live endpoint. This process is continued till it tries all the live endpoints. If at least one endpoint is alive, the request succeeds, and if not it fails.

Dead endpoints are periodically healthchecked on a fixed interval controlled by LBSolrClient.Builder#setAliveCheckInterval(int, TimeUnit). The default is set to one minute.

When to use this?
This can be used as a software load balancer when you do not wish to set up an external load balancer. Alternatives to this code are to use a dedicated hardware load balancer or using Apache httpd with mod_proxy_balancer as a load balancer. See Load balancing on Wikipedia

See Also: