【问题标题】:Elasticsearch and Testcontainers for testing "None of the configured nodes are available"用于测试“配置的节点均不可用”的 Elasticsearch 和 Testcontainers
【发布时间】:2017-09-05 11:50:13
【问题描述】:

我尝试使用 elasticsearch 容器编写测试。 我用https://www.testcontainers.org/ 库运行它。 这是我的配置:

@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 映射的端口上) - 我通过 curl 检查了它。但9300不是。

有人知道如何解决传输主机问题吗?

【问题讨论】:

    标签: elasticsearch containers testcontainers


    【解决方案1】:

    问题在于弹性搜索容器 - 而不是 testcontainers 库。

    我在这里找到了解决方案 https://github.com/olivere/elastic/issues/57#issuecomment-88697714

    Transport 客户端无法解析容器中的 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_");
    

    另外,如果您只想在 docker 中启动 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
    

    【讨论】:

    • 来自 Testcontainers 维护者的简短评论:我们通常不鼓励使用 FixedHostPortGenericContainer,因为存在端口冲突的风险。它适用于确实没有其他选择的情况,但请注意风险!
    • 你有 spring-boot-elasticsearch 的工作示例吗?
    【解决方案2】:

    对于现在提出这个问题的任何人,请注意,从 Testcontainers 1.10.1 开始,我们将 Elasticsearch 作为 Testcontainers 库中的官方模块。希望您会发现这比使用GenericContainer“自己动手”要容易得多!

    使用记录在here

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多