SpringBoot集成OpenSearch启动异常及版本兼容性咨询
问题描述
启动OpenSearch索引操作Demo时出现以下异常:
024-03-16T12:39:25.867+05:30 ERROR 9085 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataConfiguration$ReactiveRestClientConfiguration': Lookup method resolution failed
POM依赖配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.elasticsearch</groupId> <artifactId>elasticsearch-poc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>elasticsearch-poc</name> <description>Demo project for Spring Boot ElasticSearch</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test-autoconfigure</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.opensearch.client</groupId> <artifactId>spring-data-opensearch-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.opensearch.client</groupId> <artifactId>spring-data-opensearch-test-autoconfigure</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.opensearch.client</groupId> <artifactId>opensearch-rest-high-level-client</artifactId> <version>2.12.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建索引代码
package com.example.elasticsearch.elasticsearchpoc; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.opensearch.client.RequestOptions; import org.opensearch.client.RestClient; import org.opensearch.client.RestClientBuilder; import org.opensearch.client.RestHighLevelClient; import org.opensearch.client.indices.CreateIndexRequest; import org.opensearch.client.indices.CreateIndexResponse; import org.opensearch.common.settings.Settings; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration; import java.io.IOException; import java.util.HashMap; @SpringBootApplication(exclude = {ElasticsearchRepositoriesAutoConfiguration.class}) public class ElasticsearchPocApplication { public static void main(String[] args) throws IOException { SpringApplication.run(ElasticsearchPocApplication.class, args); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin")); //Create a client. RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); RestHighLevelClient client = new RestHighLevelClient(builder); //Create a non-default index with custom settings and mappings. CreateIndexRequest createIndexRequest = new CreateIndexRequest("custom-index"); createIndexRequest.settings(Settings.builder() //Specify in the settings how many shards you want in the index. .put("index.number_of_shards", 4) .put("index.number_of_replicas", 3) ); //Create a set of maps for the index's mappings. HashMap<String, String> typeMapping = new HashMap<String,String>(); typeMapping.put("type", "integer"); HashMap<String, Object> ageMapping = new HashMap<String, Object>(); ageMapping.put("age", typeMapping); HashMap<String, Object> mapping = new HashMap<String, Object>(); mapping.put("properties", ageMapping); createIndexRequest.mapping(mapping); CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); } }
咨询问题
- 该启动异常的原因是什么?具体含义是什么?
- 本地Docker部署的OpenSearch 1.2服务,是否与OpenSearch Rest High Level Client 2.12.0、spring-data-opensearch 1.3、SpringBoot 3兼容?(无法升级服务端版本)
问题解答
1. 启动异常原因及含义
这个异常的核心是依赖版本不兼容导致的方法查找失败:
- Spring Boot 3.2.3依赖的Spring Data Elasticsearch版本是5.2.x,而你引入的
spring-data-opensearch-starter 1.3.0基于Spring Data Elasticsearch 4.4.x开发,两者在Reactive Rest Client配置的内部方法签名、类结构上存在差异。 - Spring Boot自动配置试图创建
ReactiveRestClientConfigurationBean时,找不到对应的查找方法(比如方法参数、返回值类型不匹配,或类路径缺失相关依赖类),最终触发Bean创建失败。 - 同时引入的
opensearch-rest-high-level-client 2.12.0与spring-data-opensearch-starter 1.3.0的OpenSearch客户端版本不一致,进一步加剧了类冲突和方法不匹配问题。
2. 版本兼容性判断
完全不兼容,冲突点如下:
- OpenSearch服务端1.2 vs Rest High Level Client 2.12.0:OpenSearch遵循大版本一致兼容原则,2.x客户端与1.x服务端在API、协议上存在差异,比如索引创建语法、请求头格式、安全验证逻辑都有变化,直接使用会出现请求失败、序列化错误等问题。
- spring-data-opensearch 1.3 vs Spring Boot 3:spring-data-opensearch 1.3.x基于Spring Boot 2.7.x开发,完全不兼容Spring Boot 3.x的自动配置体系、Spring Data 5.x的API规范,会导致大量Bean创建失败、类加载错误。
- 即使手动创建RestHighLevelClient,跨大版本的客户端与服务端也会出现各种无法预知的兼容性问题。
可行解决方案(基于无法升级服务端的前提)
- 降级Spring Boot到2.7.x版本,与spring-data-opensearch 1.3.0匹配。
- 将
opensearch-rest-high-level-client版本降级到1.2.x,与服务端版本一致。 - 移除
spring-data-opensearch-starter和相关测试依赖,直接使用原生1.2.x版本Rest High Level Client进行操作,避免Spring Data自动配置的冲突。
内容的提问来源于stack exchange,提问作者Arunabha Ghosh
相关产品推荐
相关产品推荐

