springboot2.1.8+elasticsearch6.8+x-pack密码验证整合(亲测可用版)

springboot2.1.8+elasticsearch6.8+x-pack密码验证整合

经测试,百度和官网的TransportClient,PreBuiltXPackTransportClient这种方式的密码验证,都会抛错。报错,None of the configured nodes are available,无法连接成功。最后使用RestHighLevelClient,测试通过,浪费了大半天时间。

过程内容如些:
pom文件:

    
        org.elasticsearch
        elasticsearch
        6.8.4
    
    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        6.8.4
    
    
        org.elasticsearch.client
        elasticsearch-rest-client
        6.8.4
    

config文件

@Bean
public RestHighLevelClient client(){
    /*用户认证对象*/
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    /*设置账号密码*/
    credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic", "654321"));
    /*创建rest client对象*/
    RestClientBuilder builder = RestClient.builder(new HttpHost("10.20.4.66",9200))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                    return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
            });
    RestHighLevelClient client = new RestHighLevelClient(builder);
    return client;
}

源码访问地址:https://github.com/viviandavid/springboot-gatherdemo

你可能感兴趣的:(elasticsearch,elasticsearch,springboot)