如何启动嵌入在我的 Java 应用程序中的 elasticsearch 5.1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41298467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start elasticsearch 5.1 embedded in my java application?
提问by Bastian Voigt
With elasticsearch 2.x I used the following code to launch an embedded Node for testing:
在 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();
}
This does not compile any more. How can I start an embedded node in 5.x ?
这不再编译。如何在 5.x 中启动嵌入式节点?
回答by Bastian Voigt
Embedding elasticsearch is no longer officially supported, and it's a bit more complicated than in 2.x, but it works.
嵌入elasticsearch 不再受到官方支持,它比2.x 中的要复杂一些,但它可以工作。
You need to add some dependencies:
您需要添加一些依赖项:
<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>
And then launch a node like this:
然后像这样启动一个节点:
@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);
}
}
回答by Tomá? Zálusky
The simplest way to make ES5 working is to use Allegro embedded-elasticsearch
library(more info in article here). After day of struggling with embedding ES5 on jar level I found it pretty easy. Include into your pom:
使 ES5 正常工作的最简单方法是使用Allegroembedded-elasticsearch
库(更多信息参见此处的文章)。经过一天在 jar 级别嵌入 ES5 的苦苦挣扎后,我发现这很容易。包括到你的 pom 中:
<dependency>
<groupId>pl.allegro.tech</groupId>
<artifactId>embedded-elasticsearch</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
and use following code in your unit test
并在您的单元测试中使用以下代码
EmbeddedElastic embeddedElastic = EmbeddedElastic.builder()
.withElasticVersion("5.5.2")
.withSetting(PopularProperties.HTTP_PORT, 21121)
.build();
embeddedElastic.start();
Test will automatically download Elastic and run it in isolated environment driven from your test at OS process level. http://localhost:21121
proves it works.
测试将自动下载 Elastic 并在由您在操作系统进程级别的测试驱动的隔离环境中运行它。http://localhost:21121
证明它有效。
Our application has to interact with different versions of ESs. So this was another reason why this solution was also useful for our case, since we wouldn't otherwise be able to test multiple versions of ESs by adding multiple elasticsearch.jars into classpath.
我们的应用程序必须与不同版本的 ES 进行交互。所以这是这个解决方案对我们的案例也有用的另一个原因,因为否则我们将无法通过将多个 elasticsearch.jars 添加到类路径来测试多个版本的 ES。
Note: if this approach resembles "poor-man's Docker" to you, you can also have a look at TestContainersproject. I didn't try it myself though I suppose it would be possible, assuming your testing infrastructure have the use of Docker.
注意:如果这种方法对您来说类似于“穷人的 Docker”,您还可以查看TestContainers项目。我自己没有尝试过,但我认为这是可能的,假设您的测试基础设施使用了 Docker。
回答by dadoonet
It's not supported.
不支持。
You should read this blog post.
您应该阅读这篇博文。
EDIT:
编辑:
This is how I solved the integration tests with mavenproblem.
回答by Nitish Goyal
Embedded elasticsearch is not supported anymore
不再支持嵌入式弹性搜索
You can use this maven dependency, it will start elasticsearch 6 cluster for you
你可以使用这个 maven 依赖,它会为你启动 elasticsearch 6 集群
<dependency>
<groupId>org.elasticsearch-6</groupId>
<artifactId>elasticsearch-embedded-cluster</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
You can read more details on https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster
您可以在https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster上阅读更多详细信息