ES 7.16 Java API Clien 新特性(ES开启x-pack安全认证,RestClientBuilder设置账号密码连接集群)

ES 7.16 Java API Clien 新特性(ES开启x-pack安全认证,RestClientBuilder设置账号密码连接集群),第1张

ES 7.16 Java API Clien 新特性(ES开启x-pack安全认证,RestClientBuilder设置账号密码连接集群

截至7.15前版本的Java api官网已废弃。

1、添加依赖
    
      co.elastic.clients
      elasticsearch-java
      7.16.2
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.12.3
    
2、账号密码连接集群
        
        String nodes = "192.168.1.96:9200,192.168.1.97:9200,192.168.1.98:9200";//集群节点

        HttpHost[] httpHosts = Arrays.stream(nodes.split(",")).map(x -> {
                      String[] hostInfo = x.split(":");
                       return new HttpHost(hostInfo[0], Integer.parseInt(hostInfo[1]));
                   }).toArray(HttpHost[]::new);

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));//账号密码

        RestClientBuilder builder = RestClient.builder(httpHosts)
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

        RestClient restClient = builder.build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5709611.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存