2017-04-10 78 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