在 spring-data-elasticsearch with High Level Rest Client 中,RestClient 何时关闭?
In spring-data-elasticsearch with High Level Rest Client, when does the RestClient closed?
我对 High Level REST Client
和 spring-data-elasticsearch
之间的关系有疑问。
在我的 spring-boot 项目中,我为连接创建了 RestClientConfig
,
// Config file
@Configuration
@EnableElasticsearchAuditing
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Value("${spring.data.elasticsearch.client.reactive.endpoints}")
private String endpoint;
@Value("${spring.data.elasticsearch.client.reactive.username}")
private String username;
@Value("${spring.data.elasticsearch.client.reactive.password}")
private String pwd;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(endpoint)
.withBasicAuth(username, pwd)
.build();
return RestClients.create(clientConfiguration).rest();
}
}
并使用 ElasticSearchRepository
向集群发送一些请求。
// repository
public interface TestRepository extends ElasticsearchRepository<Test, String> {
}
// I use the repository for whole request to the elasticsearch cluster
testRepo.save(/* object */)
当然效果很好,但是,我真的很好奇客户端何时关闭。 (据我所知,I should close the rest-client when I finished the job. 我必须自己关闭它吗?(如果是,什么时候?)还是 spring-data-elasticsearch
帮我关闭它?)
你能告诉我它是如何工作的吗?任何代码或文档的链接都将非常感谢,因为我找不到任何链接。
谢谢!
Spring 数据 Elasticsearch 永远不会关闭客户端,因为它不会创建它,您在配置中创建它。
并且像链接文档中指出的那样:
you should close the high-level client when you are well and truly done with it
Spring 数据 Elasticsearch 无法知道您的应用程序是否以及何时不再需要连接到 Elasticsearch。
因此,如果您认为确实不需要它,您可以保留对客户端的引用并关闭它。但是之后对 ElasticsearchOperations 或存储库的任何调用都会失败。
实际上,我从未听说过由于应用程序中此 class 的单个实例浪费资源而导致的问题,我想说不要费心关闭它。
我对 High Level REST Client
和 spring-data-elasticsearch
之间的关系有疑问。
在我的 spring-boot 项目中,我为连接创建了 RestClientConfig
,
// Config file
@Configuration
@EnableElasticsearchAuditing
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Value("${spring.data.elasticsearch.client.reactive.endpoints}")
private String endpoint;
@Value("${spring.data.elasticsearch.client.reactive.username}")
private String username;
@Value("${spring.data.elasticsearch.client.reactive.password}")
private String pwd;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(endpoint)
.withBasicAuth(username, pwd)
.build();
return RestClients.create(clientConfiguration).rest();
}
}
并使用 ElasticSearchRepository
向集群发送一些请求。
// repository
public interface TestRepository extends ElasticsearchRepository<Test, String> {
}
// I use the repository for whole request to the elasticsearch cluster
testRepo.save(/* object */)
当然效果很好,但是,我真的很好奇客户端何时关闭。 (据我所知,I should close the rest-client when I finished the job. 我必须自己关闭它吗?(如果是,什么时候?)还是 spring-data-elasticsearch
帮我关闭它?)
你能告诉我它是如何工作的吗?任何代码或文档的链接都将非常感谢,因为我找不到任何链接。
谢谢!
Spring 数据 Elasticsearch 永远不会关闭客户端,因为它不会创建它,您在配置中创建它。
并且像链接文档中指出的那样:
you should close the high-level client when you are well and truly done with it
Spring 数据 Elasticsearch 无法知道您的应用程序是否以及何时不再需要连接到 Elasticsearch。
因此,如果您认为确实不需要它,您可以保留对客户端的引用并关闭它。但是之后对 ElasticsearchOperations 或存储库的任何调用都会失败。
实际上,我从未听说过由于应用程序中此 class 的单个实例浪费资源而导致的问题,我想说不要费心关闭它。