【问题标题】:How to start elasticsearch 5.1 embedded in my java application?如何启动嵌入到我的 java 应用程序中的 elasticsearch 5.1?
【发布时间】:2017-05-08 23:36:30
【问题描述】:

在 elasticsearch 2.x 中,我使用以下代码启动嵌入式节点进行测试:

@Bean
public Node elasticSearchTestNode() {
    return NodeBuilder.nodeBuilder()
            .settings(Settings.settingsBuilder()
                    .put("http.enabled", "true")
                    .put("path.home", "elasticsearch-data")
                    .build())
            .node();
}

这不再编译。如何在 5.x 中启动嵌入式节点?

【问题讨论】:

  • "这不再编译" 你得到什么编译错误?

标签: java elasticsearch


【解决方案1】:

官方不再支持嵌入 elasticsearch,而且比 2.x 中的要复杂一些,但是可以。

你需要添加一些依赖:

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency><!-- required by elasticsearch -->
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>transport-netty4-client</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency><!-- required by elasticsearch -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>

然后像这样启动一个节点:

@Bean
public Node elasticSearchTestNode() throws NodeValidationException {
    Node node = new MyNode(
            Settings.builder()
                    .put("transport.type", "netty4")
                    .put("http.type", "netty4")
                    .put("http.enabled", "true")
                    .put("path.home", "elasticsearch-data")
                    .build(),
            asList(Netty4Plugin.class));
    node.start();
    return node;
}

private static class MyNode extends Node {
    public MyNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins) {
        super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), classpathPlugins);
    }
}

【讨论】:

  • 这有点帮助。现在我得到java.lang.IllegalStateException: failed to obtain node locks, tried [[target/elasticsearch]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])
  • 你可以尝试删除你的数据文件夹,如果它以某种方式被搞砸了
  • 你知道我得到了什么吗,Caused by: java.lang.IllegalStateException: Error finding the build shortHash. Stopping Elasticsearch now so it doesn't run in subtly broken ways. This is likely a build bug.?。我正在使用版本5.2.2 btw。
  • @AhmetDAL 我还没有在 5.2.2 上尝试过,因为我们的主要搜索项目目前仍在 ES 2.4 上运行。如果你让它工作,请编辑我的答案:)
  • 依赖找不到!!
【解决方案2】:

使 ES5 工作的最简单方法是使用 Allegro embedded-elasticsearch library(更多信息请参阅文章 here)。经过一天在 jar 级别上嵌入 ES5 的苦苦挣扎后,我发现这很容易。包含在你的 pom 中:

<dependency>
    <groupId>pl.allegro.tech</groupId>
    <artifactId>embedded-elasticsearch</artifactId>
    <version>2.5.0</version>
    <scope>test</scope>
</dependency>

并在您的单元测试中使用以下代码

EmbeddedElastic embeddedElastic = EmbeddedElastic.builder()
        .withElasticVersion("5.5.2")
        .withSetting(PopularProperties.HTTP_PORT, 21121)
        .build();
embeddedElastic.start();

测试将自动下载 Elastic 并在操作系统进程级别的测试驱动的隔离环境中运行它。 http://localhost:21121 证明它有效。

我们的应用程序必须与不同版本的 ES 交互。所以这是这个解决方案对我们的案例也有用的另一个原因,因为我们无法通过将多个 elasticsearch.jars 添加到类路径中来测试多个版本的 ES。

注意:如果这种方法对你来说类似于“穷人的 Docker”,你也可以看看 TestContainers 项目。我自己没有尝试过,但我认为这是可能的,假设您的测试基础设施使用了 Docker。

【讨论】:

  • 不错的方法。对我来说开箱即用。唯一的缺点是它启动了第二个 JVM,它比在进程中消耗更多的内存。
  • @Tomas 这正是我要搜索的内容,我可以启动 elasticsearch 实例,请指导我如何加载文件/数据以及如何根据 Key 搜索该数据?我是 ES 新手。
  • @Prajitha ES 具有强大的 REST API,请参阅手册 elastic.co/guide/en/elasticsearch/reference/current/index.html 。就像我上面写的那样,你可以在 localhost url 上与生成的 ES 通信。
  • @TomášZáluský 感谢您的回答。你拯救了我的一天!
  • 同意 TestContainers。我前段时间推过:testcontainers.org/modules/elasticsearch。也就是说,我更喜欢使用 docker fabric8 maven 插件。例如看这个项目:github.com/dadoonet/elasticsearch-beyonder/blob/master/pom.xml
【解决方案3】:

不支持。

您应该阅读this blog post

编辑:

这就是我解决integration tests with maven 问题的方法。

【讨论】:

  • 好的,在生产环境中嵌入elasticsearch肯定不是个好主意,但我仍然需要它来测试。
  • 我用david.pilato.fr/blog/2016/10/18/…更新了我的答案
  • 您可以考虑从您在答案中提供的链接中嵌入相关部分。链接来来去去,没有什么比找到一个问题的答案更令人沮丧的了
  • 如果您使用 Maven 并且不想使用 Ant 脚本,您也可以使用 Elasticsearch Maven 插件在构建的集成测试阶段运行 Elasticsearch 版本 5+ 的实例:github.com/alexcojocaru/elasticsearch-maven-plugin
【解决方案4】:

不再支持嵌入式弹性搜索

你可以使用这个maven依赖,它会为你启动elasticsearch 6集群

<dependency>
     <groupId>org.elasticsearch-6</groupId>
     <artifactId>elasticsearch-embedded-cluster</artifactId>
     <version>1.0-SNAPSHOT</version>
</dependency>

您可以阅读更多详细信息 https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster

【讨论】:

    猜你喜欢
    • 2017-06-03
    • 2013-07-31
    • 2012-11-10
    • 2017-07-13
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多