12
votes

I have an application built using the Jhipter generator, which is based on Spring Boot. The latest version of Jhipster allows you to include Elasticsearch as an option, so I have an application which runs an embedded instance of Elasticsearch in development mode and connects to a server instance in production mode.

When the application is running in development mode it connects perfectly fine to the embedded instance, but if I try to connect to an external instance I get the following error on console:

ERROR 7804 --- [ restartedMain] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]

My application is using Spring boot version 1.4.0.RELEASE and according to the elasticsearch.yml, the application has elasticsearch 2.3.5

My application-prod.yml settings:

spring:
    data:
        elasticsearch:
            cluster-name: 
            cluster-nodes: localhost:9300

The default ElasticSearchConfiguration was:

@Configuration
public class ElasticSearchConfiguration {

    @Bean
    public ElasticsearchTemplate elasticsearchTemplate(Client client, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
        return new ElasticsearchTemplate(client, new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build()));
    }            
}

Which I override with:

@Configuration
public class ElasticSearchConfiguration {
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
            String server = clusterNodes.split(":")[0];
            Integer port = Integer.parseInt(clusterNodes.split(":")[1]);
            Settings settings = Settings.settingsBuilder()
                .put("cluster.name", clusterName).build();
            client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port));
            return new ElasticsearchTemplate(client);
        }
    }

But I am still not able to connect elasticsearch using prod yml.

While debugging I got the following error while ElasticsearchTemplate bean creation:

Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.elasticsearch.common.inject.InjectorImpl.toString()

How can I resolve this issue?

1
is there any solution for this? i am trying to connect to the remote. Please update with the answerJinna Balu
Did you ran the elastic search on remote? When using remote elasticsearch you can keep using the default ElasticSearchConfiguration. Just run elasticsearch on remote.ajain
I am running my elasticsearch in aws instance and ports opened accordingly. unable to connect from another aws instance where my app is running. here is my issue github.com/jhipster/generator-jhipster/issues/…. Need help this.Jinna Balu
I have this issue today and when I downgrade ES from 2.4 to 1.7.3, the connection establishedHaifeng Zhang
I'm confused, is your production cluster's master node running on localhost? The No nodes available message is trying to connect locally.skylerl

1 Answers

2
votes

I've a working Jhipster project with Elasticsearch. If your Elastic instance is running locally on default ports, you can leave those properties empty. It's not needed to change the class ElasticSearchConfiguration

Using in Production

In production, JHipster expects an external Elasticsearch instance. By default, the application looks for an Elasticsearch instance running on localhost. This can be configured by using the standard Spring Boot properties, in the application-prod.yml file.