当我启动程序时,我运行ElasticSearch Service并检查索引是否存在以及是否有任何文档,假设我只是运行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函数中,
OriginalException:Elasticsearch.Net.ElasticsearchClientException:远程服务器返回错误:(503)服务器不可用。调用:状态代码503来自:GET / myindex / _count。 ServerError:类型:search_phase_execution_exception原因:“所有分片均失败” ---> System.Net.WebException:远程服务器返回错误:(503)服务器不可用。
OriginalException:Elasticsearch.Net.ElasticsearchClientException:无法连接到远程服务器。 call :状态代码未知:GET / _cluster / health / myindex?wait_for_status =黄色---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接因为目标计算机主动拒绝它而被执行127.0.0.1:9200
如您所见,我已经尝试了Cluster WaitForStatus,但是没有用。
我的问题:有什么方法可以等待客户端/集群/节点准备好并且没有任何异常?
最佳答案
听起来您在启动程序的同时正在启动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下载,配置和运行Elasticsearchvar 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);
}
关于c# - 避免出现ElasticSearch错误503服务器不可用:使用WaitForStatus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59882075/