java中RestHighLevelClient 获取端口配置等基本信息

示例代码:

@CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = {"/v1/es_info"}, method = {POST})
    public ResponseEntity<String> queryESInfo(@RequestBody QueryAlarmRequestInfo body)
            throws Exception {
        StringBuilder s = new StringBuilder();
        try {
            RestHighLevelClient client = ElasticSearchUtil.getElasticsearchRestClient().rest();

            MainResponse response = client.info(RequestOptions.DEFAULT);
            String esVersion = response.getVersion().getNumber();
            s.append("Elasticsearch Version: " + esVersion + "\n");
            // 打印 Elasticsearch 版本和其他信息
            String clusterName = response.getClusterName().toString();
            String nodeName = response.getNodeName();
            s.append("Cluster name: " + clusterName + "\n");
            s.append("Node name: " + nodeName + "\n");
            // 从RestHighLevelClient获取RestClient
            RestClient restClient = client.getLowLevelClient();
            // 获取已配置的HttpHost信息
            HttpHost[] hosts = restClient.getNodes().stream()
                    .map(node -> node.getHost())
                    .toArray(HttpHost[]::new);
            // 打印每个HttpHost的信息
            for (HttpHost host : hosts) {
                s.append("Host: " + host.getHostName() + "\n");
                s.append("Port: " + host.getPort() + "\n");
                s.append("Scheme: " + host.getSchemeName() + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return ResponseModel.success(s.toString());
    }

  

posted @ 2024-07-03 16:45  bonelee  阅读(14)  评论(0编辑  收藏  举报