使用Docker Compose部署Spring Data OpenSearch时遇error while sniffing nodes错误
解决Spring Data OpenSearch + Docker Compose连接错误
核心问题分析
Connection refused:应用无法连接到OpenSearch节点,大概率是网络通信问题或服务未就绪BeanCreationException针对ReactiveRestClientConfiguration:通常是依赖版本不兼容,或配置缺失导致的客户端初始化失败
分步解决方案
1. 修正Docker Compose网络与OpenSearch配置
确保OpenSearch容器和Spring Boot应用在同一网络,且端口、服务名配置正确:
# docker-compose.yml示例 version: '3.8' services: opensearch: image: opensearchproject/opensearch:latest environment: - discovery.type=single-node - OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m - DISABLE_SECURITY_PLUGIN=true # 开发环境可禁用安全插件简化配置 ports: - "9200:9200" - "9300:9300" networks: - opensearch-net healthcheck: test: ["CMD", "curl", "-f", "http://localhost:9200/_cluster/health"] interval: 10s timeout: 10s retries: 5 spring-app: build: . depends_on: opensearch: condition: service_healthy environment: - SPRING_OPENSEARCH_REST_URIS=http://opensearch:9200 # 用Docker服务名而非localhost networks: - opensearch-net networks: opensearch-net: driver: bridge
注:通过
healthcheck和depends_on的service_healthy条件,确保OpenSearch完全就绪后再启动应用。
2. 匹配Spring Boot与OpenSearch版本兼容性
版本不匹配会直接导致客户端初始化失败,需严格对应:
| Spring Boot版本 | Spring Data OpenSearch版本 | OpenSearch版本 |
|---|---|---|
| 3.2.x | 5.2.x | 2.11.x |
| 3.1.x | 5.1.x | 2.9.x |
| 3.0.x | 5.0.x | 2.7.x |
Maven依赖配置示例:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-opensearch</artifactId> </dependency> <!-- 排除冲突的Elasticsearch依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-opensearch</artifactId> <exclusions> <exclusion> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> </exclusion> </exclusions> </dependency>
3. 调整OpenSearch客户端配置
开发环境单节点部署时,可直接禁用节点嗅探避免连接错误:
# application.yml spring: opensearch: rest: uris: http://opensearch:9200 client: sniffer: enabled: false # 禁用节点嗅探
如果需要启用嗅探,需确保OpenSearch节点的发布地址可被应用访问,在容器环境变量中添加:
opensearch: environment: - http.publish_host: opensearch # 与Docker服务名一致
内容的提问来源于stack exchange,提问作者shirin kulkarni
相关产品推荐
相关产品推荐

