2017-04-10 281 views
1

我嘗試使用elasticsearch容器編寫測試。 我用https://www.testcontainers.org/庫運行它。 這是我的配置:用於測試的Elasticsearch和Testcontainers

@ClassRule 
public static GenericContainer elasticContainer = 
     new GenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0") 
       .withExposedPorts(9300, 9200) 
       .withEnv("xpack.security.enabled", "false") 
       .withEnv("transport.host", "127.0.0.1") 
       .withEnv("http.host", "0.0.0.0"); 

而且我得到了一個例外:

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{9fuJUZYWS6O6IgLGOgJDaA}{localhost}{127.0.0.1:32792}] 

我重新配置我的端口,用於測試和9200端口是可用的(即由testcontainers映射端口) - 我被檢查了它捲曲。但9300不是。

劑量任何人都知道如何解決運輸主機問題?

回答

3

問題出在彈性搜索容器 - 而不是testcontainers lib。

我發現這裏的解決方案 https://github.com/olivere/elastic/issues/57#issuecomment-88697714

傳輸客戶端無法解析ElasticSearch節點容器。

最後的代碼是:

@ClassRule 
public static GenericContainer elasticContainer = 
     new FixedHostPortGenericContainer("docker.elastic.co/elasticsearch/elasticsearch:5.3.0") 
       .withFixedExposedPort(9200, 9200) 
       .withFixedExposedPort(9300, 9300) 
       .waitingFor(Wait.forHttp("/")) // Wait until elastic start 
       .withEnv("xpack.security.enabled", "false") 
       .withEnv("network.host", "_site_") 
       .withEnv("network.publish_host", "_local_"); 

另外如果你想剛開始ElasticSearch在泊塢窗,並使用9300(傳輸端口)運行以下命令:

docker run -p 9300:9300 -p 9200:9200 -e "xpack.security.enabled=false" -e "network.host=_site_" -e "network.publish_host=_local_" docker.elastic.co/elasticsearch/elasticsearch:5.3.0