2014-08-27 32 views
8

我在玩spring-boot-sample-data-elastcisearch項目。 我已經改變了POM並補充說:如何使彈性搜索嵌入式可通過本地主機訪問:9200

SampleElasticsearchApplicationWebXml extends SpringBootServletInitializer 

在Tomcat中嵌入運行。 我application.properties有

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

我希望能夠連接到本地主機:9200,以使用elasticsearch頭或其它JS客戶端。我錯過了什麼? 謝謝, 米蘭

+0

你可以嘗試閱讀春天elasticsearch客戶端的文檔。看起來他們支持連接到elasticsearch的各種方式。我懷疑設置elasticsearch屬性會導致它神奇地做你想要的。如果你想開始嵌入elasticsearch,你可能需要自己做。爲此,請閱讀elasticsearch文檔。他們確實支持運行嵌入式客戶端節點,但我懷疑他們會爲此啓動http傳輸,因爲它不是必需的。你仍然需要一個適當的elasticsearch節點來連接它。 – 2014-08-27 13:29:06

回答

7

你應該定義自己這一點,因爲NodeClientFactoryBeanhttp.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); 
       } 
      } 
     } 
    } 
} 
+0

謝謝,我最終從spring引導重寫了ElasticSearchAutoConfiguration並且它工作正常。 – 2014-09-03 08:20:24

+0

不要忘記在您的starter-class(帶'main'-method的類)中禁用ElasticsearchAutoConfiguration'@EnableAutoConfiguration(exclude = {ElasticsearchAutoConfiguration.class})'。否則,啓動將啓動兩個節點! – einsA 2015-05-30 19:47:54

15

this ticket,你現在可以簡單地這一行添加到您的配置文件:

spring.data.elasticsearch.properties.http.enabled=true 
相關問題