10

我正在玩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 客户端。我错过了什么?谢谢,米兰

4

2 回答 2

17

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

spring.data.elasticsearch.properties.http.enabled=true
于 2015-08-06T09:22:38.230 回答
7

您应该自己定义它,因为NodeClientFactoryBean有一个选项,http.enabledElasticSearchAutoConfiguration没有(还)设置它。

@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);
                }
            }
        }
    }
}
于 2014-08-27T15:34:38.330 回答