Bas*_*igt 10 java elasticsearch
使用elasticsearch 2.x,我使用以下代码启动嵌入式节点进行测试:
@Bean
public Node elasticSearchTestNode() {
return NodeBuilder.nodeBuilder()
.settings(Settings.settingsBuilder()
.put("http.enabled", "true")
.put("path.home", "elasticsearch-data")
.build())
.node();
}
Run Code Online (Sandbox Code Playgroud)
这不再编译.如何在5.x中启动嵌入式节点?
Bas*_*igt 15
嵌入弹性搜索不再是官方支持,它比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>
Run Code Online (Sandbox Code Playgroud)
然后启动这样的节点:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
使ES5工作的最简单方法是使用Allegro embedded-elasticsearch库(此处有更多信息).经过一天努力将ES5嵌入jar级别后,我发现它非常简单.包括在你的pom中:
<dependency>
<groupId>pl.allegro.tech</groupId>
<artifactId>embedded-elasticsearch</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并在单元测试中使用以下代码
EmbeddedElastic embeddedElastic = EmbeddedElastic.builder()
.withElasticVersion("5.5.2")
.withSetting(PopularProperties.HTTP_PORT, 21121)
.build();
embeddedElastic.start();
Run Code Online (Sandbox Code Playgroud)
测试将自动下载Elastic并在操作系统进程级别的测试驱动的隔离环境中运行它.http://localhost:21121证明它有效.
我们的应用程序必须与不同版本的ES进行交互.所以这就是为什么这个解决方案对我们的案例也很有用的另一个原因,因为我们不能通过在classpath中添加多个elasticsearch.jars来测试多个版本的ES.
注意:如果这种方法类似于"穷人的Docker",您还可以查看TestContainers项目.我自己没有尝试,虽然我认为这是可能的,假设您的测试基础设施使用Docker.
不支持。
您应该阅读此博客文章。
编辑: