Spring Boot 3.2对接Elasticsearch 7.10.2遇版本兼容问题求助
Spring Boot 3.2 对接 Elasticsearch 7.10.2 兼容问题
背景
Elasticsearch 服务器版本为 7.10.2 且无法升级,原有应用基于 Spring Boot 3.1 与 spring-data-elasticsearch 4.4 构建,计划升级至 Spring Boot 3.2 时遇到以下问题:
现有 Elasticsearch 配置
class ElasticSearchConfig( private val elasticSearchProperties: ElasticSearchProperties, ): ReactiveElasticsearchConfiguration() { override fun clientConfiguration(): ClientConfiguration { return ClientConfiguration.builder() .connectedTo(elasticSearchProperties.hostAndPort) .usingSsl() .withConnectTimeout(Duration.ofSeconds(elasticSearchProperties.connectionTimeout)) .withSocketTimeout(Duration.ofSeconds(elasticSearchProperties.socketTimeout)) .withBasicAuth(elasticSearchProperties.username, elasticSearchProperties.password) .withClientConfigurer(ElasticsearchClients.ElasticsearchClientConfigurationCallback.from { builder -> builder .setDefaultHeaders( listOf(BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON))) .addInterceptorLast(HttpResponseInterceptor{ response, _ -> response.addHeader("X-Elastic-Product", "Elasticsearch") }) }) .build() } }
遇到的问题
问题1:保留 spring-data-elasticsearch 4.4 时的方法不存在错误
Spring Boot 3.2 的自动配置不再兼容 ElasticsearchTransportConfiguration,报错信息如下:
An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations$ElasticsearchTransportConfiguration.restClientTransport(ElasticsearchClientConfigurations.java:94) The following method did not exist: 'void co.elastic.clients.transport.rest_client.RestClientTransport.<init>(org.elasticsearch.client.RestClient, co.elastic.clients.json.JsonpMapper, co.elastic.clients.transport.rest_client.RestClientOptions)' The calling method's class, org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations$ElasticsearchTransportConfiguration, was loaded from the following location: jar:file:/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/3.2.2/5c407409f8d260a4bc6e173d16fc3b36e6adec21/spring-boot-autoconfigure-3.2.2.jar!/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientConfigurations$ElasticsearchTransportConfiguration.class The called method's class, co.elastic.clients.transport.rest_client.RestClientTransport, is available from the following locations: jar:file:/.gradle/caches/modules-2/files-2.1/co.elastic.clients/elasticsearch-java/7.17.9/65d35187ab13f6427d5694a84bd963aa8d0ce964/elasticsearch-java-7.17.9.jar!/co/elastic/clients/transport/rest_client/RestClientTransport.class The called method's class hierarchy was loaded from the following locations: co.elastic.clients.transport.rest_client.RestClientTransport: file:/.gradle/caches/modules-2/files-2.1/co.elastic.clients/elasticsearch-java/7.17.9/65d35187ab13f6427d5694a84bd963aa8d0ce964/elasticsearch-java-7.17.9.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations$ElasticsearchTransportConfiguration and co.elastic.clients.transport.rest_client.RestClientTransport
问题2:升级至 Spring Boot 兼容的 elasticsearch-java 8.X 时的请求头错误
访问 ES 时出现运行时错误:
org.springframework.data.elasticsearch.UncategorizedElasticsearchException: [es/search] failed: [content_type_header_exception] java.lang.IllegalArgumentException: only one Content-Type header should be provided at org.springframework.data.elasticsearch.client.elc.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:105) ~[spring-data-elasticsearch-5.2.2.jar:5.2.2]
移除配置中的默认 Content-Type 头配置后:
.setDefaultHeaders( listOf(BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)))
出现 406 错误:
org.springframework.dao.DataAccessResourceFailureException: method [POST], host [https://pcp-alpha-ltcp-api.line-ves-dev.com:12000], URI [/settlement-detail/_search?typed_keys=true&search_type=query_then_fetch], status line [HTTP/1.1 406 Not Acceptable] {"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported","status":406} at org.springframework.data.elasticsearch.client.elc.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:110) ~[spring-data-elasticsearch-5.2.2.jar:5.2.2]
诉求
已查阅官方迁移文档,了解到 RestHighLevelClient 在 spring-data-elasticsearch 5.2 中已移除,希望通过修改请求头或其他方式实现 8.X 客户端对接 7.X ES 服务,同时保留 spring-data-elasticsearch 4.4 的方案也无法正常工作,寻求可行解决方案。
内容的提问来源于stack exchange,提问作者Rex Lin
相关产品推荐
相关产品推荐

