ElasticSearch Rest High Level Client gives UnknownHostException

3.1k views Asked by At

I was trying to connect to my elasticsearch cluster using RestHighLevelClient but it seems to be not working for me. Following is the code snippet I used.

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Base64;

@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;


    @Bean(destroyMethod = "close")
    public RestHighLevelClient esClient() {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username,     password));

        System.out.println("header " +  Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
        String encodedBytes = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, 9243,"https"));
    /*Header[] headers = {new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
            new BasicHeader("Authorization", "Basic " + encodedBytes)};
    */
        builder.setHttpClientConfigCallback(
            httpClientBuilder ->     httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    //builder.setDefaultHeaders(headers);
        System.out.println("password" +     credentialsProvider.getCredentials(AuthScope.ANY).getPassword());
    System.out.println("name" +     credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName());
    return new RestHighLevelClient(builder);

    }

}

I tried using the same set of hostname, port and credentials (Base 64 encoded string of "username:password") with cURL and it connects, but somehow programmatically it's giving my UnknownHostException, which is pretty weird, help is appreciated.

EDIT I'm using 9243 which is working with cURL and also I tried 9200, 9300 just in case but to no avail.

1

There are 1 answers

4
Wim Van den Brande On

One small but important correction. When using the default setup of Elastic Search, you should be connecting to port 9200.

Please read official documentation: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html