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?
From your IDE, right-click on the “Application” class at the root of your Java package hierarchy, and run it directly. You should also be able to debug it from your IDE. The application will be available on http://localhost:8080.
JHipster gives you the tools to update, manage and package the resulting application. Run mvn package -Pprod to trigger a Maven build that uses the Spring Boot Maven plugin to create a single executable . war file, and Grunt or Gulp. js tasks to test, minify and optimize JavaScript, HTML and CSS code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With