避免 ElasticSearch 错误 503 服务器不可用:使用 WaitForStatus

Avoiding ElasticSearch error 503 Server Unavailable: Use of WaitForStatus

当我启动我的程序时,我 运行 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 原因:"all shards failed" ---> System.Net.WebException:远程服务器返回错误:(503) 服务器不可用。

  2. 健康也失败了:

    OriginalException: Elasticsearch.Net.ElasticsearchClientException: 无法连接到远程服务器。呼叫:未知的状态代码:GET /_cluster/health/myindex?wait_for_status=黄色 ---> System.Net.WebException:无法连接到远程服务器 ---> System.Net.Sockets.SocketException : 无法建立连接,因为目标机器主动拒绝 127.0.0.1:9200

如您所见,我尝试了集群 WaitForStatus,但它没有用。

我的问题:有什么方法可以等到 client/cluster/nodes 准备好并且不会出现任何异常吗?

听起来您在启动程序的同时启动了 Elasticsearch 进程,但 Elasticsearch 比您的程序准备就绪的时间要长。

如果是这样,您可能有兴趣使用 the same abstractions that the .NET client uses for integration tests against Elasticsearch. The abstractions read output from the Elasticsearch process to know when it is ready, and block until this happens. They're 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.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);
}