1
votes

I am using low level rest API to access Elastic Search in AWS (AWS Service). AWS provides the HTTPS url. The following code is inside SpringBoot configuration class named com.example.configurations.EsConfig.

@Bean
public RestClient restClient() throws Exception {
    RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    HttpAsyncClientBuilder httpAsyncClientBuilder = null;
                    try {
                        httpAsyncClientBuilder = httpClientBuilder.setSSLContext(SSLContext.getDefault());
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                    return httpAsyncClientBuilder;
                }
            })
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000)
                    .setSocketTimeout(60000))
            .setMaxRetryTimeoutMillis(60000)
            .build();
    return restClient;
}

For esHost I am providing the AWS Elasticsearch endpoint URL only like localhost.

For esPort its 443 in case of HTTPS.

Following are the error messages:

2017-07-31 09:22:43.001  INFO 6239 --- [/O dispatcher 1] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.018  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.037  INFO 6239 --- [/O dispatcher 3] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.043  INFO 6239 --- [/O dispatcher 4] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 6] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 5] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 7] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.077  INFO 6239 --- [/O dispatcher 8] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.099  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer

I am not able to get RestClient over SSL connection. Please suggest me what is needed to get a RestClient over SSL connection. Thanks

1

1 Answers

3
votes

I solved it passing "https" while creating HttpPost object as third argument.

RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort, "https"))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setSSLContext(sslcontext);
                return httpAsyncClientBuilder;
            })
            .build();

Now its working fine.