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

标签 java elasticsearch spring-boot jhipster

我有一个使用基于 Spring Boot 的 Jhipter 生成器构建的应用程序。最新版本的 Jhipster 允许您将 Elasticsearch 作为一个选项包含在内,因此我有一个应用程序在开发模式下运行 Elasticsearch 的嵌入式实例并在生产模式下连接到服务器实例。

当应用程序在开发模式下运行时,它可以很好地连接到嵌入式实例,但是如果我尝试连接到外部实例,我会在控制台上收到以下错误:

ERROR 7804 --- [restartedMain] .d.e.r.s.AbstractElasticsearchRepository:加载 Elasticsearch 节点失败:org.elasticsearch.client.transport.NoNodeAvailableException:配置的节点均不可用:[{#transport#-1} {127.0.0.1}{127.0.0.1:9300}]

我的应用程序正在使用 Spring boot 版本 1.4.0.RELEASE 并且根据 elasticsearch.yml,该应用程序具有 elasticsearch 2.3.5

我的 application-prod.yml 设置:

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

默认的 ElasticSearchConfiguration 是:

@Configuration
public class ElasticSearchConfiguration {

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

我用它覆盖:

@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);
        }
    }

但我仍然无法使用 prod yml 连接 elasticsearch。

调试时,我在创建 ElasticsearchTemplate bean 时遇到以下错误:

方法抛出“java.lang.StackOverflowError”异常。无法评估 org.elasticsearch.common.inject.InjectorImpl.toString()

我该如何解决这个问题?

最佳答案

我有一个使用 Elasticsearch 的 Jhipster 项目。如果您的 Elastic 实例在本地默认端口上运行,您可以将这些属性留空。不需要更改类 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.

关于java - 使用 prod yml 在 JHipster 项目中配置 elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41331863/

相关文章:

elasticsearch - Kibana 4和相对时间过滤器/JSON输入

java - 如何在java中使用AWS Textract检索pdf中存在的表

java - 为什么我的应用程序显示两个 requestPermission Rationales?

java - 是否可以更改 log4j 中包的日志级别?

ElasticSearch 无法解析映射

spring-boot - 如何在 Spring WebClient 中一次设置多个 header ?

java - 找不到 SpringApplication 类

java - 如何读取序列化文件?

java - Jaxb2Marshaller 和原始类型

multithreading - 乐观并发控制澄清