public class LBHttpSolrClient extends SolrClient
HttpSolrClient
. This is useful when you
have multiple Solr servers and the requests need to be Load Balanced among them.
Do NOT use this class for indexing in master/slave scenarios since documents must be sent to the
correct master; 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 servers.
If a request to a server fails by an IOException due to a connection timeout or read timeout then the host is taken off the list of live servers and moved to a 'dead server list' and the request is resent to the next live server. This process is continued till it tries all the live servers. If at least one server is alive, the request succeeds, and if not it fails.
This detects if a dead server comes alive automatically. The check is done in fixed intervals in a dedicated thread. This interval can be set usingSolrClient lbHttpSolrClient = new LBHttpSolrClient("http://host1:8080/solr/", "http://host2:8080/solr", "http://host2:8080/solr"); //or if you wish to pass the HttpClient do as follows httpClient httpClient = new HttpClient(); SolrClient lbHttpSolrClient = new LBHttpSolrClient(httpClient, "http://host1:8080/solr/", "http://host2:8080/solr", "http://host2:8080/solr");
setAliveCheckInterval(int)
, 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 setup 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
Modifier and Type | Class and Description |
---|---|
static class |
LBHttpSolrClient.Builder
Constructs
LBHttpSolrClient instances from provided configuration. |
static class |
LBHttpSolrClient.Req |
static class |
LBHttpSolrClient.Rsp |
protected static class |
LBHttpSolrClient.ServerWrapper |
Modifier and Type | Field and Description |
---|---|
protected Map<String,LBHttpSolrClient.ServerWrapper> |
zombieServers |
Modifier | Constructor and Description |
---|---|
protected |
LBHttpSolrClient(org.apache.http.client.HttpClient httpClient,
ResponseParser parser,
String... solrServerUrl)
Deprecated.
use
LBHttpSolrClient(Builder) instead, as it is a more extension/subclassing-friendly alternative |
protected |
LBHttpSolrClient(HttpSolrClient.Builder httpSolrClientBuilder,
org.apache.http.client.HttpClient httpClient,
String... solrServerUrl)
Deprecated.
use
LBHttpSolrClient(Builder) instead, as it is a more extension/subclassing-friendly alternative |
protected |
LBHttpSolrClient(LBHttpSolrClient.Builder builder) |
Modifier and Type | Method and Description |
---|---|
void |
addQueryParams(String queryOnlyParam) |
void |
addSolrServer(String server) |
protected Exception |
addZombie(HttpSolrClient server,
Exception e) |
void |
close() |
protected Exception |
doRequest(HttpSolrClient client,
LBHttpSolrClient.Req req,
LBHttpSolrClient.Rsp rsp,
boolean isNonRetryable,
boolean isZombie,
String zombieKey) |
protected void |
finalize() |
org.apache.http.client.HttpClient |
getHttpClient()
Return the HttpClient this instance uses.
|
ResponseParser |
getParser() |
Set<String> |
getQueryParams() |
RequestWriter |
getRequestWriter() |
protected HttpSolrClient |
makeSolrClient(String server) |
static String |
normalize(String server) |
String |
removeSolrServer(String server) |
LBHttpSolrClient.Rsp |
request(LBHttpSolrClient.Req req)
Tries to query a live server from the list provided in Req.
|
NamedList<Object> |
request(SolrRequest request,
String collection)
Tries to query a live server.
|
NamedList<Object> |
request(SolrRequest request,
String collection,
Integer numServersToTry) |
void |
setAliveCheckInterval(int interval)
LBHttpSolrServer keeps pinging the dead servers at fixed interval to find if it is alive.
|
void |
setConnectionTimeout(int timeout)
Deprecated.
since 7.0 Use
LBHttpSolrClient.Builder methods instead. |
void |
setParser(ResponseParser parser)
Changes the
ResponseParser that will be used for the internal
SolrServer objects. |
void |
setQueryParams(Set<String> queryParams)
Expert Method.
|
void |
setRequestWriter(RequestWriter requestWriter)
Changes the
RequestWriter that will be used for the internal
SolrServer objects. |
void |
setSoTimeout(int timeout)
Deprecated.
since 7.0 Use
LBHttpSolrClient.Builder methods instead. |
add, add, add, add, add, add, add, add, add, add, addBean, addBean, addBean, addBean, addBeans, addBeans, addBeans, addBeans, addBeans, addBeans, commit, commit, commit, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, deleteByQuery, deleteByQuery, getBinder, getById, getById, getById, getById, getById, getById, getById, getById, optimize, optimize, optimize, optimize, optimize, optimize, ping, query, query, query, query, queryAndStreamResponse, queryAndStreamResponse, request, rollback, rollback
protected final Map<String,LBHttpSolrClient.ServerWrapper> zombieServers
@Deprecated protected LBHttpSolrClient(HttpSolrClient.Builder httpSolrClientBuilder, org.apache.http.client.HttpClient httpClient, String... solrServerUrl)
LBHttpSolrClient(Builder)
instead, as it is a more extension/subclassing-friendly alternative@Deprecated protected LBHttpSolrClient(org.apache.http.client.HttpClient httpClient, ResponseParser parser, String... solrServerUrl)
LBHttpSolrClient(Builder)
instead, as it is a more extension/subclassing-friendly alternativeprotected LBHttpSolrClient(LBHttpSolrClient.Builder builder)
public void setQueryParams(Set<String> queryParams)
queryParams
- set of param keys to only send via the query stringpublic void addQueryParams(String queryOnlyParam)
protected HttpSolrClient makeSolrClient(String server)
public LBHttpSolrClient.Rsp request(LBHttpSolrClient.Req req) throws SolrServerException, IOException
req
- contains both the request as well as the list of servers to queryIOException
- If there is a low-level I/O error.SolrServerException
protected Exception addZombie(HttpSolrClient server, Exception e)
protected Exception doRequest(HttpSolrClient client, LBHttpSolrClient.Req req, LBHttpSolrClient.Rsp rsp, boolean isNonRetryable, boolean isZombie, String zombieKey) throws SolrServerException, IOException
SolrServerException
IOException
public void addSolrServer(String server) throws MalformedURLException
MalformedURLException
@Deprecated public void setConnectionTimeout(int timeout)
LBHttpSolrClient.Builder
methods instead.@Deprecated public void setSoTimeout(int timeout)
LBHttpSolrClient.Builder
methods instead.public void close()
public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException
request
in class SolrClient
request
- the SolrRequest.collection
- the collection to execute the request againstIOException
- If there is a low-level I/O error.SolrServerException
- if there is an error on the serverpublic NamedList<Object> request(SolrRequest request, String collection, Integer numServersToTry) throws SolrServerException, IOException
SolrServerException
IOException
public void setAliveCheckInterval(int interval)
interval
- time in millisecondspublic org.apache.http.client.HttpClient getHttpClient()
public ResponseParser getParser()
public void setParser(ResponseParser parser)
ResponseParser
that will be used for the internal
SolrServer objects.parser
- Default Response Parser chosen to parse the response if the parser
were not specified as part of the request.SolrRequest.getResponseParser()
public void setRequestWriter(RequestWriter requestWriter)
RequestWriter
that will be used for the internal
SolrServer objects.requestWriter
- Default RequestWriter, used to encode requests sent to the server.public RequestWriter getRequestWriter()
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.