SolrServer SolrRequest

SolrServer实现类

HttpSolrServer

HttpSolrServer uses the Apache Commons HTTPClient to connect to solr.

HttpSolrServer is thread-safe and if you are using the following constructor, you must re-use the same instance for all request.

if instances are created on the fly, it can cause a connection leak. The recommended practice is to keep a static instance of HttpSolrServer per solr server url and share it for all requests.

LBHttpSolrServer

LBHttpSolrServer or "LoadBalanced HttpSolrServer" is a load balancing wrapper around org.apache.solr.client.solrj.impl.HttpSolrServer. This is useful when you have multiple SolrServers 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, this class may be used for updates since updates will be forwarded to the appropriate leader. Also see the wiki page.

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.

 SolrServer lbHttpSolrServer = new LBHttpSolrServer("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();
 SolrServer lbHttpSolrServer = new LBHttpSolrServer(httpClient,"http://host1:8080/solr/","http://host2:8080/solr","http://host2:8080/solr");
 

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 using setAliveCheckInterval , 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

CloudSolrServer

SolrJ client class to communicate with SolrCloud. Instances of this class communicate with Zookeeper to discover Solr endpoints for SolrCloud collections, and then use the LBHttpSolrServer to issue requests. This class assumes the id field for your documents is called 'id' - if this is not the case, you must set the right name with setIdField(String).

-- SolrJ includes a 'smart' client for SolrCloud, which is ZooKeeper aware. This means that your Java application only needs to know about your Zookeeper instances, and not where your Solr instances are, as this can be derived from Zookeeper.

ConcurrentUpdateSolrServer

ConcurrentUpdateSolrServer buffers all added documents and writes them into open HTTP connections. This class is thread safe. Params from UpdateRequest are converted to http request parameters. When params change between UpdateRequests a new HTTP request is started. Although any SolrServer request can be made with this implementation, it is only recommended to use ConcurrentUpdateSolrServer with /update requests. The class HttpSolrServer is better suited for the query interface.

SolrServer主要方法

主要方法add、delete、query、commit、optimize等。

这些方法都是根据请求构建对应的SolrRequest,然后执行SolrRequest的process方法。

public abstract SolrResponse process( SolrServer server ) throws SolrServerException, IOException;

SolrRequest的类结构

例如,QueryRequest的process重载:

@Override
  public QueryResponse process( SolrServer server ) throws SolrServerException 
  {
    try {
      long startTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
      QueryResponse res = new QueryResponse( server.request( this ), server );
      long endTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
      res.setElapsedTime(endTime - startTime);
      return res;
    } catch (SolrServerException e){
      throw e;
    } catch (SolrException s){
      throw s;
    } catch (Exception e) {
      throw new SolrServerException("Error executing query", e);
    }
  }

-----------------------------

http://wiki.apache.org/solr/Solrj

http://wiki.apache.org/solr/LBHttpSolrServer

原文地址:https://www.cnblogs.com/huangfox/p/4347147.html