【问题标题】:Avoiding ElasticSearch error 503 Server Unavailable: Use of WaitForStatus避免 ElasticSearch 错误 503 服务器不可用:使用 WaitForStatus
【发布时间】:2020-01-23 15:32:21
【问题描述】:

当我启动我的程序时,我运行 ElasticSearch 服务并检查索引是否存在以及是否有任何文档,假设我只是运行 ES 服务并且我有这两个功能:

public ElasticClient getElasticSearchClient()
{
    ConnectionSettings connectionSettings = new Nest.ConnectionSettings(new Uri("http://localhost:9200"))
                                                    .DefaultIndex("myindex")
                                                    .DisableDirectStreaming();
    ElasticClient client = new ElasticClient(connectionSettings);
    //var health = client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)).Timeout(50));
    return client;
}

public void checkElasticsearchIndex()
{
    var client = getElasticSearchClient();

    var health = this.client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)));

    CountResponse count = client.Count<myobject>();

    if (!client.Indices.Exists("myindex").IsValid || count.Count == 0)
    {
        BulkWriteAllToIndexES(client);
    }
}

在 checkElasticsearchIndex 函数内部,

  1. 计数操作失败并显示以下错误消息:

    OriginalException:Elasticsearch.Net.ElasticsearchClientException:远程服务器返回错误:(503)服务器不可用。调用:状态码 503 来自:GET /myindex/_count。 ServerError:类型:search_phase_execution_exception 原因:“所有分片失败”---> System.Net.WebException:远程服务器返回错误:(503)服务器不可用。

  2. Health 也失败了:

    OriginalException:Elasticsearch.Net.ElasticsearchClientException:无法连接到远程服务器。调用:状态码未知来自:GET /_cluster/health/myindex?wait_for_status=yellow ---> System.Net.WebException:无法连接到远程服务器 ---> System.Net.Sockets.SocketException:无法连接是因为目标机器主动拒绝了它 127.0.0.1:9200

如您所见,我尝试了 Cluster WaitForStatus,但没有成功。

我的问题:有什么方法可以等到客户端/集群/节点准备好而不出现任何异常?

【问题讨论】:

    标签: c# elasticsearch nest elasticsearch-7


    【解决方案1】:

    听起来您在启动程序的同时启动了 Elasticsearch 进程,但 Elasticsearch 需要比您的程序更长的时间才能准备好。

    如果是这种情况,您可能有兴趣使用 .NET 客户端用于针对 Elasticsearch 进行集成测试的 the same abstractions。抽象从 Elasticsearch 进程读取输出以了解它何时准备就绪,并阻塞直到发生这种情况。他们是available on an AppVeyor CI package feed(计划在未来将它们发布到 Nuget)。

    some examples of how to spin up a cluster with the abstractions。对于单个节点,它将类似于

    using System;
    using Elastic.Managed.Configuration;
    using Elastic.Managed.ConsoleWriters;
    using Elastic.Managed.FileSystem;
    
    namespace Elastic.Managed.Example
    {
        class Program
        {
            static void Main(string[] args)
            {
                var version = "7.5.1";
                var esHome = Environment.ExpandEnvironmentVariables($@"%LOCALAPPDATA%\ElasticManaged\{version}\elasticsearch-{version}");
    
                using (var node = new ElasticsearchNode(version, esHome))
                {
                    node.SubscribeLines(new LineHighlightWriter());
                    if (!node.WaitForStarted(TimeSpan.FromMinutes(2))) throw new Exception();
    
                    // do your work here
                }
            }
        }
    }
    

    这假设 Elasticsearch 7.5.1 zip 已经下载,并且存在于 %LOCALAPPDATA%\ElasticManaged\7.5.1\elasticsearch-7.5.1。还有更复杂的examples of how to integrate this into tests with xUnit.

    您可以use the EphemeralCluster components下载、配置和运行Elasticsearch

    var plugins = new ElasticsearchPlugins(ElasticsearchPlugin.RepositoryAzure, ElasticsearchPlugin.IngestAttachment);
    var config = new EphemeralClusterConfiguration("7.5.1", ClusterFeatures.XPack, plugins, numberOfNodes: 1);
    using (var cluster = new EphemeralCluster(config))
    {
        cluster.Start();
    
        var nodes = cluster.NodesUris();
        var connectionPool = new StaticConnectionPool(nodes);
        var settings = new ConnectionSettings(connectionPool).EnableDebugMode();
        var client = new ElasticClient(settings);
    
        Console.Write(client.CatPlugins().DebugInformation);
    }
    

    【讨论】:

    • 别担心,需要让他们在 nuget 上起来 :)
    猜你喜欢
    • 2011-10-01
    • 2017-01-14
    • 2012-04-23
    • 2011-08-04
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多