Hi,
i am trying to connect to Elasticsearch hosted in our private cloud environment.
Support team has provided me host and credentials for the same.
I am able to connect to elasticsearch when i run it in my local but when i connect to the host via hostname i get UnknownHostException.
When i try using the Ip address instead then i get timeout exception, i am not sure what is wrong in my code.
Below is my elasticsearch configuration
Configuration
@ConditionalOnMissingBean(name = "elasticsearchOperations")
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String elasticsearchHost;
@Value("${elasticsearch.username}")
private String elasticsearchUsername;
@Value("${elasticsearch.password}")
private String elasticsearchPassword;
@Value("${elasticsearch.connectTimeout}")
private int elasticsearchConnectionTimeout;
@Value("${elasticsearch.socketTimeout}")
private int elasticsearchSocketTimeout;
@Bean
public ElasticsearchRestTemplate elasticsearchOperations() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
@Override
@Bean(destroyMethod = "close", name = "esHighLevelRestClient")
public RestHighLevelClient elasticsearchClient() {
return new RestHighLevelClient(restClientBuilder());
}
@Bean(destroyMethod = "close")
public RestClient restClient() {
return restClientBuilder().build();
}
private RestClientBuilder restClientBuilder() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(
elasticsearchUsername,
elasticsearchPassword));
return RestClient.builder(new HttpHost(elasticsearchHost,9200, "http"))
.setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder
.setConnectTimeout(elasticsearchConnectionTimeout)
.setSocketTimeout(elasticsearchConnectionTimeout))
.setHttpClientConfigCallback(
httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
);
}
@Bean
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(new StringToLocalDateTimeConverter()));
}
@ReadingConverter
public class StringToLocalDateTimeConverter implements Converter<Long, LocalDateTime> {
@Override
public LocalDateTime convert(Long source) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(source), ZoneId.systemDefault());
}
}
i am getting host, username and password from my properties file.
Exception when using ip address is
java.net.ConnectException: Timeout connecting to [/XXX.XXX.X.X:9200]
at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:169) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:632) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:898) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:198) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:213) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:158) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:351) ~[httpcore-nio-4.4.14.jar:4.4.14]
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221) ~[httpasyncclient-4.1.4.jar:4.1.4]
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64) ~[httpasyncclient-4.1.4.jar:4.1.4]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_271]
when I use hostname instead then it throws UnknownHostException