简体   繁体   English

使用prod yml在JHipster项目中配置elasticsearch

[英]Configuring elasticsearch in JHipster project using prod yml

I have an application built using the Jhipter generator, which is based on Spring Boot. 我有一个使用Jhipter生成器构建的应用程序,它基于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. 最新版本的Jhipster允许您将Elasticsearch作为选项包含在内,因此我有一个应用程序在开发模式下运行Elasticsearch的嵌入式实例,并以生产模式连接到服务器实例。

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] .dersAbstractElasticsearchRepository : 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}] 错误7804 --- [restartedMain] .dersAbstractElasticsearchRepository:无法加载elasticsearch节点:org.elasticsearch.client.transport.NoNodeAvailableException:没有配置的节点可用:[{#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 我的应用程序使用的是Spring启动版本1.4.0.RELEASE ,根据elasticsearch.yml ,该应用程序有弹性搜索2.3.5

My application-prod.yml settings: 我的application-prod.yml设置:

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

The default ElasticSearchConfiguration was: 默认的ElasticSearchConfiguration是:

@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. 但我仍然无法使用prod yml连接elasticsearch。

While debugging I got the following error while ElasticsearchTemplate bean creation: 在调试时,我在创建ElasticsearchTemplate bean时遇到以下错误:

Method threw 'java.lang.StackOverflowError' exception. 方法抛出'java.lang.StackOverflowError'异常。 Cannot evaluate org.elasticsearch.common.inject.InjectorImpl.toString() 无法评估org.elasticsearch.common.inject.InjectorImpl.toString()

How can I resolve this issue? 我该如何解决这个问题?

I've a working Jhipster project with Elasticsearch. 我和Elasticsearch一起工作的Jhipster项目。 If your Elastic instance is running locally on default ports, you can leave those properties empty. 如果您的Elastic实例在默认端口上本地运行,则可以将这些属性保留为空。 It's not needed to change the class ElasticSearchConfiguration 不需要更改类ElasticSearchConfiguration

Using in Production 在生产中使用

In production, JHipster expects an external Elasticsearch instance. 在生产中,JHipster期望一个外部的Elasticsearch实例。 By default, the application looks for an Elasticsearch instance running on localhost. 默认情况下,应用程序查找在localhost上运行的Elasticsearch实例。 This can be configured by using the standard Spring Boot properties, in the application-prod.yml file. 这可以使用application-prod.yml文件中的标准Spring Boot属性进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM