如何从我们用spring Boot 编写的java应用程序中监控ElasticSearch服务器的健康状况?最新版本任何想法/参考?

wecizke3  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(85)

我已经给出了一个任务,我必须从java spring Boot 应用程序(使用java ElasticSearch异步客户端从我的原始应用程序)监控健康状况(ElasticSearch服务器的状态是否是绿色/红色/黄色),我必须使用这个应用程序客户端连接到服务器,然后找到ElasticSearch服务器的状况。
我是ElasticSearch的新手

h6my8fg2

h6my8fg21#

可以执行一个GETHttp请求,返回以下响应

GET host:port/_cluster/health

{
  "cluster_name" : "testcluster",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 1,
  "active_shards" : 1,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 1,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 50.0
}

字符串
为了使用Java执行http请求,可以像这样使用Apache HttpClient

GetMethod get = new GetMethod("url");
InputStream in = get.getResponseBodyAsStream();
// Process the response and parse the status.
get.releaseConnection();


您可以使用Java库解析JSON响应并访问字段status
要解析JSON,您可以使用以下库:
Jackson
Gson
org.json
也可以使用Elasticsearch Java API Client并直接获得结果,如下所示(这就是我要做的):

ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
ClusterHealthStatus status = response.getStatus();


查看官方文档here
希望这对你有帮助,祝你好运

相关问题