如何为几个Solr核心只创建一个`LBHttpSolrServer`实例?

时间:2015-02-02 18:11:22

标签: solr solrj

我正在使用SolrJ使用LBHttpSolrServer开发Solr客户端,因为我有几个Solr节点。

问题是我的应用程序包含200多个Solr核心。 所以,我为每个Solr核心创建了一个LBHttpSolrServer实例(如文档中所示):

LBHttpSolrServer solrServer = new LBHttpSolrServer(
    "http://host1:8080/solr/mySolrCore",
    "http://host2:8080/solr/mySolrCore"
);

但是,它在我的应用程序中生成了超过200个LBHttpSolrServer个实例。

docs所示,如果某个节点发生故障,SolrJ将为每个LBHttpSolrServer实例创建一个线程,因此会有超过200个额外的线程,并使我的应用程序饱和。

有没有办法只创建一个LBHttpSolrServer的实例,这个实例将由我的所有内核共享?

1 个答案:

答案 0 :(得分:0)

是的,遗憾的是LBHttpSolrServer是如何实现的:

  /** The provided httpClient should use a multi-threaded connection manager */
  public LBHttpSolrServer(HttpClient httpClient, ResponseParser parser, String...solrServerUrl) {
    clientIsInternal = (httpClient == null);
    this.parser = parser;
    if (httpClient == null) {
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set(HttpClientUtil.PROP_USE_RETRY, false);
      this.httpClient = HttpClientUtil.createClient(params);
    } else {
      this.httpClient = httpClient;
    }
    for (String s: solrServerUrl) {
      ServerWrapper wrapper = new ServerWrapper(makeServer(s));
      aliveServers.put(wrapper.getKey(), wrapper);
    }
    updateAliveList();
  }

您可以采取的方法是使用LBHttpSolrServer提供的其他请求方法:

public Rsp request(Req req)

而不是从SolrServer

覆盖的默认值
public NamedList<Object> request(final SolrRequest request)
LBHttpSolrServer提供的

请求(Req req)方法允许通过Req参数

指定Solr URL列表
"@param req contains both the request as well as the list of servers to query"

在这种情况下,LBHttpSolrServer只会保持实例化HttpSolrServers,直到有人工作:

for (String serverStr : req.getServers()) {
  serverStr = normalize(serverStr);
  // if the server is currently a zombie, just skip to the next one
  ServerWrapper wrapper = zombieServers.get(serverStr);
  if (wrapper != null) {
    // System.out.println("ZOMBIE SERVER QUERIED: " + serverStr);
    if (skipped.size() < req.getNumDeadServersToTry())
      skipped.add(wrapper);
    continue;
  }
  rsp.server = serverStr;
  HttpSolrServer server = makeServer(serverStr);

  ex = doRequest(server, req, rsp, isUpdate, false, null);
  if (ex == null) {
    return rsp; // SUCCESS
  }