1
votes

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
try to use Http connection instead of https and see that's the issue? - dassum
@dassum it's not working still - Deepesh
On what port is ES running? Why is the port 443? Share the sample elasticsearch.host value that you have specified in properties file. Better print and verify if you are getting right value - sidgate
the port should be 9300 and then use an HTTP connection. Please mention the elastic search connection properties in the question. - dassum
@dassum my port is 9243. - Deepesh

1 Answers

0
votes

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