小编典典

如何启动我的Java应用程序中嵌入的elasticsearch 5.1?

elasticsearch

在Elasticsearch 2.x中,我使用以下代码来启动嵌入式Node进行测试:

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

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


阅读 449

收藏
2020-06-22

共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);
    }
}
2020-06-22