【问题标题】:How to make elasticsearch embedded accessible via localhost:9200如何通过 localhost:9200 使嵌入式弹性搜索可访问
【发布时间】:2014-10-20 21:04:32
【问题描述】:

我正在玩spring-boot-sample-data-elastcisearch 项目。 我已经更改了 pom 并添加了:

SampleElasticsearchApplicationWebXml extends SpringBootServletInitializer 

在嵌入 Tomcat 的情况下运行。 我的 application.properties 有

spring.data.elasticsearch.http-enabled=true
spring.data.elasticsearch.local=true

我希望能够连接到 localhost:9200 以便使用 elasticsearch-head 或其他 JS 客户端。我错过了什么? 谢谢, 米兰

【问题讨论】:

  • 您可以尝试阅读 spring elasticsearch 客户端的文档。看起来他们支持连接到弹性搜索的各种方式。我怀疑设置 elasticsearch 属性会导致它神奇地做你想做的事。如果你想开始嵌入式弹性搜索,你可能需要自己做。为此,请阅读 elasticsearch 文档。他们确实支持运行嵌入式客户端节点,但我怀疑他们是否会为此启动 http 传输,因为它不是必需的。你仍然需要一个合适的 elasticsearch 节点才能连接到它。

标签: elasticsearch spring-data-elasticsearch


【解决方案1】:

根据this ticket,您现在可以简单地将这一行添加到您的配置文件中:

spring.data.elasticsearch.properties.http.enabled=true

【讨论】:

    【解决方案2】:

    你应该自己定义它,因为NodeClientFactoryBean 有一个http.enabled 的选项,但ElasticSearchAutoConfiguration 没有(还)设置它。

    @Configuration
    @EnableConfigurationProperties(ElasticsearchProperties.class)
    public class ElasticsearchConfiguration implements DisposableBean {
    
        private static Log logger = LogFactory.getLog(ElasticsearchConfiguration.class);
    
        @Autowired
        private ElasticsearchProperties properties;
    
        private NodeClient client;
    
        @Bean
        public ElasticsearchTemplate elasticsearchTemplate() {
            return new ElasticsearchTemplate(esClient());
        }
    
        @Bean
        public Client esClient() {
            try {
                if (logger.isInfoEnabled()) {
                    logger.info("Starting Elasticsearch client");
                }
                NodeBuilder nodeBuilder = new NodeBuilder();
                nodeBuilder
                        .clusterName(this.properties.getClusterName())
                        .local(false)
                ;
                nodeBuilder.settings()
                        .put("http.enabled", true)
                ;
                this.client = (NodeClient)nodeBuilder.node().client();
                return this.client;
            }
            catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }
    
        @Override
        public void destroy() throws Exception {
            if (this.client != null) {
                try {
                    if (logger.isInfoEnabled()) {
                        logger.info("Closing Elasticsearch client");
                    }
                    if (this.client != null) {
                        this.client.close();
                    }
                }
                catch (final Exception ex) {
                    if (logger.isErrorEnabled()) {
                        logger.error("Error closing Elasticsearch client: ", ex);
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢,我最终从 spring boot 重写了 ElasticSearchAutoConfiguration 并且它可以工作。
    • 不要忘记在您的入门级(使用 main 方法的类)中禁用 ElasticsearchAutoConfiguration @EnableAutoConfiguration(exclude= { ElasticsearchAutoConfiguration.class })。否则 boot 会启动两个节点!
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多