spring boot + hibernate search + elastic search embedded 启动失败
spring boot + hibernate search + elastic search embedded fails to start
我正在努力在 spring 引导设置中使用弹性搜索后端设置休眠搜索。
我有的是 spring 引导和以下依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.4.0.M3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-backend-elasticsearch</artifactId>
<version>5.6.0.Alpha3</version>
</dependency>
发生的情况是,hibernate search 在 elastic search 完成启动之前初始化。
使用以下 属性 也公开了其余接口
spring:
data:
elasticsearch:
properties:
http:
enabled: true
导致异常
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
现在,如何在这里定义依赖关系?
我尝试使用自定义 BeanFactoryPostProcessor 来注入对弹性搜索的依赖,但在自动配置场景中似乎被忽略了。
有什么方法可以引入等到弹性搜索启动吗?
设置有效,当我将休眠 index_management_strategy
设置为 NONE
时,但随后未配置索引并且所有自定义分析器注释都被忽略,默认为弹性搜索中的默认映射,自动配置场景下无法配置
理想情况下,弹性搜索应该托管在 jvm 外部,但在测试场景中很方便。
我检查了 sprint-data 文档,看起来你误解了这篇文章(实际上它很令人困惑,伙计们不理解背后的技术?)
By default the instance will attempt to connect to a local in-memory server (a NodeClient in Elasticsearch terms), but you can switch to a remote server (i.e. a TransportClient) by setting spring.data.elasticsearch.cluster-nodes to a comma-separated ‘host:port’ list.
NodeClient 不是 "local server",它是 ES 客户端 的特殊类型。这个本地客户端可以连接到包含数据的ES集群节点,正如我在评论中所说,你没有任何ES数据节点运行。
阅读本文以更好地理解 https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
我了解到这是您在集成测试期间遇到的问题。
您可以看看我们如何在 Hibernate Search 本身的集成测试期间启动 ES,使用 Maven 插件确保服务器在测试之前启动:
- https://github.com/hibernate/hibernate-search/blob/5.6.0.Beta1/elasticsearch/pom.xml#L341-L368
N.B。这使用自定义 ES 配置,调整为快速启动,即使它只是一个单节点集群:
- https://raw.githubusercontent.com/hibernate/hibernate-search/5.6.0.Beta1/elasticsearch/elasticsearchconfiguration/elasticsearch.yml
Hibernate Search使用Jest client连接ES,所以需要开启ES的HTTP connector:不要把它和NodeClient搞混了,后者是不同的运行模式。
如果您的问题与自动化测试无关,而是与生产集群相关,那么我建议您使用像 Kubernetes 这样的服务编排器。
感谢 spring 引导团队的一些帮助,我得以解决问题 - 解决方案 here。
The problem is that there's no dependency between the EntityManagerFactory bean and the Elasticsearch Client bean so there's no guarantee that Elasticsearch will start before Hibernate. As it happens, Hibernate starts first and then fails to connect to Elasticsearch.
这可以通过在两个 bean 之间设置依赖关系来解决。一种简单的方法是使用 EntityManagerFactoryDependsOnPostProcessor
:
的子类
@Configuration
static class ElasticsearchJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor {
public ElasticsearchJpaDependencyConfiguration() {
super("elasticsearchClient");
}
}
现在只需要将副本数设置为0
即可修复单节点部署集群的健康状态。这可以通过在 application.properties
文件
中指定额外的 属性 来完成
spring.data.elasticsearch.properties.index.number_of_replicas= 0
我正在努力在 spring 引导设置中使用弹性搜索后端设置休眠搜索。
我有的是 spring 引导和以下依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.4.0.M3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-backend-elasticsearch</artifactId>
<version>5.6.0.Alpha3</version>
</dependency>
发生的情况是,hibernate search 在 elastic search 完成启动之前初始化。
使用以下 属性 也公开了其余接口
spring:
data:
elasticsearch:
properties:
http:
enabled: true
导致异常
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:9200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
现在,如何在这里定义依赖关系?
我尝试使用自定义 BeanFactoryPostProcessor 来注入对弹性搜索的依赖,但在自动配置场景中似乎被忽略了。
有什么方法可以引入等到弹性搜索启动吗?
设置有效,当我将休眠 index_management_strategy
设置为 NONE
时,但随后未配置索引并且所有自定义分析器注释都被忽略,默认为弹性搜索中的默认映射,自动配置场景下无法配置
理想情况下,弹性搜索应该托管在 jvm 外部,但在测试场景中很方便。
我检查了 sprint-data 文档,看起来你误解了这篇文章(实际上它很令人困惑,伙计们不理解背后的技术?)
By default the instance will attempt to connect to a local in-memory server (a NodeClient in Elasticsearch terms), but you can switch to a remote server (i.e. a TransportClient) by setting spring.data.elasticsearch.cluster-nodes to a comma-separated ‘host:port’ list.
NodeClient 不是 "local server",它是 ES 客户端 的特殊类型。这个本地客户端可以连接到包含数据的ES集群节点,正如我在评论中所说,你没有任何ES数据节点运行。 阅读本文以更好地理解 https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
我了解到这是您在集成测试期间遇到的问题。
您可以看看我们如何在 Hibernate Search 本身的集成测试期间启动 ES,使用 Maven 插件确保服务器在测试之前启动: - https://github.com/hibernate/hibernate-search/blob/5.6.0.Beta1/elasticsearch/pom.xml#L341-L368
N.B。这使用自定义 ES 配置,调整为快速启动,即使它只是一个单节点集群: - https://raw.githubusercontent.com/hibernate/hibernate-search/5.6.0.Beta1/elasticsearch/elasticsearchconfiguration/elasticsearch.yml
Hibernate Search使用Jest client连接ES,所以需要开启ES的HTTP connector:不要把它和NodeClient搞混了,后者是不同的运行模式。
如果您的问题与自动化测试无关,而是与生产集群相关,那么我建议您使用像 Kubernetes 这样的服务编排器。
感谢 spring 引导团队的一些帮助,我得以解决问题 - 解决方案 here。
The problem is that there's no dependency between the EntityManagerFactory bean and the Elasticsearch Client bean so there's no guarantee that Elasticsearch will start before Hibernate. As it happens, Hibernate starts first and then fails to connect to Elasticsearch.
这可以通过在两个 bean 之间设置依赖关系来解决。一种简单的方法是使用 EntityManagerFactoryDependsOnPostProcessor
:
@Configuration
static class ElasticsearchJpaDependencyConfiguration extends EntityManagerFactoryDependsOnPostProcessor {
public ElasticsearchJpaDependencyConfiguration() {
super("elasticsearchClient");
}
}
现在只需要将副本数设置为0
即可修复单节点部署集群的健康状态。这可以通过在 application.properties
文件
spring.data.elasticsearch.properties.index.number_of_replicas= 0