如何阻止hibernate search嗅探不存在的本地ElasticSearch服务器的节点?

myzjeezk  于 2023-04-11  发布在  ElasticSearch
关注(0)|答案(2)|浏览(194)

我在AWS上创建了一个开放的搜索服务域,并将属性hibernate.search.backend.uris设置为该域的地址。一切正常,我能够索引我的实体表,并可以对开放的搜索服务域运行搜索查询。
然而,出于某种原因,hibernate搜索仍然会定期尝试连接到localhost:9200,以执行节点嗅探例程。
我如何阻止hibernate search执行这种无用的操作?它每隔几分钟左右就会尝试嗅探节点。我已经尝试设置字段hibernate.search.backend.hostshibernate.search.backend.protocol而不是.uris。但即使我设置了这些属性,hibernate search仍然试图与localhost上不存在的ElasticSearch服务交互。我正在运行hibernate search 6.1.5.Final。ElasticSearch版本设置为7.16.3。以下是我以编程方式设置的所有相关属性。

jpaProperties.put("hibernate.search.backend.aws.credentials.type", "static");
    jpaProperties.put("hibernate.search.backend.aws.credentials.access_key_id", awsId);
    jpaProperties.put("hibernate.search.backend.aws.credentials.secret_access_key", awsKey);
    jpaProperties.put("hibernate.search.backend.aws.region", openSearchAwsInstanceRegion);
    jpaProperties.put("hibernate.search.backend.aws.signing.enabled", true);
    //--------------------------------------------------------------------------------------------
    jpaProperties.put("hibernate.search.automatic_indexing.synchronization.strategy", indexSynchronizationStrategy);
    jpaProperties.put("hibernate.search.backend.request_timeout", requestTimeout);
    jpaProperties.put("hibernate.search.backend.connection_timeout", elasticSearchConnectionTimeout);
    jpaProperties.put("hibernate.search.backend.read_timeout", readTimeout);
    jpaProperties.put("hibernate.search.backend.max_connections", maximumElasticSearchConnections);
    jpaProperties.put("hibernate.search.backend.max_connections_per_route", maximumElasticSearchConnectionsPerRout);
    jpaProperties.put("hibernate.search.schema_management.strategy", schemaManagementStrategy);
    jpaProperties.put("hibernate.search.backend.analysis.configurer", "class:config.EnhancedLuceneAnalysisConfig");
    jpaProperties.put("hibernate.search.backend.uris", elasticSearchHostAddress);
    jpaProperties.put("hibernate.search.backend.directory.type", "local-filesystem");
    jpaProperties.put("hibernate.search.backend.type", "elasticsearch");
    jpaProperties.put("hibernate.search.backend.directory.root", luceneAbsoluteFilePath);
    jpaProperties.put("hibernate.search.backend.lucene_version", "LUCENE_CURRENT");
    jpaProperties.put("hibernate.search.backend.io.writer.infostream", true);

编辑:
这些是我的应用程序使用的所有与ElasticSearch相关的依赖项。

<dependency>
    <groupId>org.hibernate.search</groupId>
    <artifactId>hibernate-search-mapper-orm</artifactId>
    <version>6.1.5.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate.search</groupId>
    <artifactId>hibernate-search-backend-elasticsearch-aws</artifactId>
    <version>6.1.5.Final</version>
</dependency>

这是堆栈跟踪,

[ERROR] 2022-07-21 14:33:58.402 [es_rest_client_sniffer[T#1]] Sniffer - error while sniffing nodes
java.net.ConnectException: Connection refused: no further information
    at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:918) ~[elasticsearch-rest-client-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:299) ~[elasticsearch-rest-client-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287) ~[elasticsearch-rest-client-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.sniff.ElasticsearchNodesSniffer.sniff(ElasticsearchNodesSniffer.java:106) ~[elasticsearch-rest-client-sniffer-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.sniff.Sniffer.sniff(Sniffer.java:209) ~[elasticsearch-rest-client-sniffer-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.sniff.Sniffer$Task.run(Sniffer.java:140) ~[elasticsearch-rest-client-sniffer-7.17.3.jar:7.17.3]
    at org.elasticsearch.client.sniff.Sniffer$1.run(Sniffer.java:81) ~[elasticsearch-rest-client-sniffer-7.17.3.jar:7.17.3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
    at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.Net.pollConnect(Native Method) ~[?:?]
    at sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[?:?]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946) ~[?:?]
    at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvent(DefaultConnectingIOReactor.java:174) ~[httpcore-nio-4.4.15.jar:4.4.15]
    at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:148) ~[httpcore-nio-4.4.15.jar:4.4.15]
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:351) ~[httpcore-nio-4.4.15.jar:4.4.15]
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221) ~[httpasyncclient-4.1.5.jar:4.1.5]
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64) ~[httpasyncclient-4.1.5.jar:4.1.5]
    ... 1 more

这个堆栈跟踪是在我删除<elasticsearch.version>7.16.3</elasticsearch.version>之后复制的,所以看看这是否能解决问题。如果版本丢失,hibernate搜索似乎默认为版本7.17.3

goucqfw6

goucqfw61#

花了一段时间,终于找到了问题的根源。
显然,spring boot有一个内置的Elastic search自动配置类,名为ElasticsearchRestClientAutoConfiguration。这个类中的代码默认运行,它初始化一个默认启用了node snifferorg.elasticsearch.client.RestClient。如果本地主机上没有运行ElasticSearch服务器,这个RestClient将继续抛出异常,因为没有什么可连接的。
因为这个类不是hibernate search java库的一部分,所以像hibernate.search.backend.discovery.enabled = false这样的设置不会影响这个RestClient或它的Sniffer。你可以通过告诉Spring不要运行ElasticsearchRestClientAutoConfiguration来阻止Spring创建这个RestClient。这可以通过两种方式来完成。
首先,您可以将以下属性添加到application.properties

spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration

其次,您还可以通过将其作为值添加到@SpringBootApplication注解的exclude参数来排除此autoconfiguration类。例如:

@SpringBootApplication(exclude ={ElasticsearchRestClientAutoConfiguration.class}) 
public class MyConfiguration {
7d7tgy0s

7d7tgy0s2#

如果配置属性hibernate.search.backend.discovery.enabled设置为true,Hibernate Search将仅启用节点发现(创建嗅探器),默认情况下它是false
如果你列出的属性是你设置的唯一属性,那么我不认为Hibernate Search正在创建这个嗅探器。嗅探器也不使用你传递给Hibernate Search的URI,所以也倾向于证明嗅探器不是由Hibernate Search创建的。
如果你不相信我的话,你可以在调试模式下启动你的应用程序,并在org.hibernate.search.backend.elasticsearch.client.impl.ElasticsearchClientFactoryImpl#createSniffer中设置一个断点。
我想你可能在你的应用程序中有其他的东西,创建了一个Elasticsearch客户端和一个嗅探器,还有一些东西没有完全配置好。试着在调试模式下启动你的应用程序,并在org.elasticsearch.client.sniff.Sniffer的构造函数中放置断点?

相关问题