4

我目前正在使用 NEST ElasticSearch C# Library 与 ElasticSearch 进行交互。我的项目是一个 MVC 4 WebAPI 项目,它基本上构建了一个用于访问目录辅助信息的 RESTful Web 服务。

我们才刚刚开始使用 NEST,并且一直因缺乏文档而磕磕绊绊。那里的东西很有用,但它有一些非常大的漏洞。目前,我们需要的一切都可以正常工作,但是,我们遇到了连接有时需要一整秒的问题。我们想做的是使用某种连接池,类似于您与 SQL Server 交互的方式。

这是有关如何使用嵌套连接的文档:http: //mpdreamz.github.com/NEST/concepts/connecting.html

以下是我们项目中的相关代码片段:

public class EOCategoryProvider : IProvider
{
    public DNList ExecuteQuery(Query query)
    {
        //Configure the elastic client and it's settings
        ConnectionSettings elasticSettings = new ConnectionSettings(Config.server, Config.port).SetDefaultIndex(Config.index);
        ElasticClient client = new ElasticClient(elasticSettings);

        //Connect to Elastic
        ConnectionStatus connectionStatus;
        if (client.TryConnect(out connectionStatus))
        {
            // Elastic Search Code here ...
        } // end if
    } // end ExecuteQuery
} // end EOCategoryProvider

通过查看文档,我看不到连接池的任何规定。我一直在考虑实现我自己的(例如存储 3 或 4 个 ElasticClient 对象,并以循环方式选择它们),但我想知道是否有人有更好的解决方案。如果没有,是否有人对手动实现连接池的最佳方法有建议?有什么文章可以指点?

感谢你们提出的任何建议。

更新:这似乎与在每个请求上调用 TryConnect 以及特定的网络设置有关。当使用与 Elastic box 处于同一网络的机器时,问题完全消失;我的开发机器(到 Elastic 盒子平均需要 350 毫秒)有时似乎无法建立 http 连接,这导致 TryConnect 时间过长。

4

1 回答 1

20

您不必TryConnect()每次调用 Elasticsearch 时都调用。它基本上是您的应用程序启动时的健全性检查调用。

NEST 是 Elasticsearch 的 C# REST 客户端,默认IConnection使用WebRequest.Create已经汇集了 TCP 连接。

查看实际实现:https ://github.com/elastic/elasticsearch-net/blob/master/src/Elasticsearch.Net/Connection/HttpConnection.cs

Reusing ElasticClient won't offer any performance gains since each call already gets its own HttpWebRequest. The whole client is built stateless on purpose.

I am however very interested in why calls are taking 1 second for you. Could you post the actual NEST code, how you are are measuring the calls and describe your data.

Disclaimer: I'm the author of NEST.

于 2012-07-04T20:39:26.153 回答