Springboot elastic search health management : ConnectException: Connection refused Springboot elastic search health management : ConnectException: Connection refused elasticsearch elasticsearch

Springboot elastic search health management : ConnectException: Connection refused


Spring boot elasticsearch needs to know to which port (and host) to connect for the health check. Add:

spring:  elasticsearch:    rest:      uris: "myelasticserver:9200"      #username: ""      #password: ""

This is in addition to the config you already have. This is separate because many people, like me, use the (non-http) port 9300 of elasticsearch for the actual searches instead of your part of the config. I neither have management.health.elasticsearch. My total config for elasticsearch is:

spring:  elasticsearch:    rest:      uris: "myelasticserver:9200"  data:    elasticsearch:      cluster-nodes: "myelasticserver:9300"      cluster-name: "my-cluster-name"


The problem is indeed, as mst mentioned, that the actuator uses the RestClient. If you have configured the RestHighLevelClient, the configuration is not applied to the RestClient.

If you already have the RestHighLevelClient available, you can easily make a configured RestClient available as follows:

    @Bean(destroyMethod = "close")    public RestClient restClient() {        return restHighLevelClient().getLowLevelClient();    }


I had a same issue and I figured out what was a problem.For the health check java spring boot uses RestClient where I was using HighLevelRestClient for ES indexing/searching/deleting.

So, it seems you have also same case, whether you are using high or low (or tcp) level client for ES query but health check requires RestClient. So, the solution is to override RestClient bean with your env params. Add following to your configuration file (replace environment values with yours):

@Bean(destroyMethod = "close")public RestClient restClient() {    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();    credentialsProvider.setCredentials(AuthScope.ANY,            new UsernamePasswordCredentials(                    environment.getProperty("elasticsearch.username"),                    environment.getProperty("elasticsearch.password")));    List<HttpHost> hosts = new ArrayList<>();    hosts.add(new HttpHost(            environment.getProperty("elasticsearch.hosts.host.name", String.class),            environment.getProperty("elasticsearch.hosts.host.port", Integer.class),            "http"));    LOGGER.info("Elasticsearch connection establishing..." + hosts.toArray().toString());    return RestClient.builder(Iterables.toArray(hosts, HttpHost.class))            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));}

Let me see (plz post) your configuration if this does not work.Good luck!

References