1

is there any way to configure my rest high client to connect with es using proxy. My configuration is

    @Override
@Bean
public RestHighLevelClient elasticsearchClient() {
    return new RestHighLevelClient(RestClient.builder(HttpHost.create(elasticSearchUrl)));}

My elastic search url is: aaa.bbbb.ccc.company.com/api/elastic-search-proxy In that case I get No such host is known (aaa.bbbb.ccc.company.com/api/elastic-search-proxy) what is clear for me but is there any option to configure it ?

A.Sidor
  • 257
  • 4
  • 12

2 Answers2

3

Its mentioned in the Elasticsearch documentation of JHLRC initialization , use below code:

RestClientBuilder builder = RestClient.builder(
    new HttpHost("localhost", 9200, "http"));
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
        @Override
        public HttpAsyncClientBuilder customizeHttpClient(
                HttpAsyncClientBuilder httpClientBuilder) {
            return httpClientBuilder.setProxy(
                new HttpHost("proxy", 9000, "http"));  
        }
    });

Set a callback that allows to modify the http client configuration (e.g. encrypted communication over ssl, or anything that the org.apache.http.impl.nio.client.HttpAsyncClientBuilder allows to set)

So in your case, you need to give your original host in below code

new HttpHost("localhost", 9200, "http"));

And then you need to define a callback to your proxy server in setHttpClientConfigCallback call back.

new HttpHost("proxy", 9000, "http"));  
Amit
  • 30,756
  • 6
  • 57
  • 88
0

If someone using the latest Elastic Java client 8.x, you can use this way of configuring the proxy to your rest client. (note the proxy should already set in system properties). It might help someone.

val restClientBuilder = RestClient.builder(
            HttpHost(randomHost, 443, https)
        )*.setHttpClientConfigCallback {
            HttpAsyncClientBuilder.create().useSystemProperties()
        }*
Mohan
  • 457
  • 6
  • 15