Spring Boot应用无法连接ElasticSearch:连接被拒绝错误排查
连接ElasticSearch失败问题排查与解决
问题场景
使用Spring Boot 3.0.6与ElasticSearch 8.8.1开发在线商城应用时,运行持续出现ElasticSearch连接拒绝错误,核心报错栈如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticProductController' defined in URL [jar:file:/OnlineStore-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/kazantseva/project/OnlineStore/controller/rest/ElasticProductController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'elasticProductServiceImpl' defined in URL [jar:file:/OnlineStore-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/kazantseva/project/OnlineStore/service/impl/ElasticProductServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'elasticProductRepository' defined in kazantseva.project.OnlineStore.repository.elasticSearch.ElasticProductRepository defined in @EnableElasticsearchRepositories declared on OnlineStoreApplication: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticProductServiceImpl' defined in URL [jar:file:/OnlineStore-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/kazantseva/project/OnlineStore/service/impl/ElasticProductServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'elasticProductRepository' defined in kazantseva.project.OnlineStore.repository.elasticSearch.ElasticProductRepository defined in @EnableElasticsearchRepositories declared on OnlineStoreApplication: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticProductRepository' defined in kazantseva.project.OnlineStore.repository.elasticSearch.ElasticProductRepository defined in @EnableElasticsearchRepositories declared on OnlineStoreApplication: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception | Caused by: java.net.ConnectException: Connection refused
相关配置代码
ElasticProductRepository.java
public interface ElasticProductRepository extends ElasticsearchRepository<ElasticProduct, String> { }
ElasticProductServiceImpl.java
@Service @AllArgsConstructor public class ElasticProductServiceImpl implements ElasticProductService { private ElasticProductRepository elasticProductRepository; @Override public List<ElasticProduct> getAllProducts() { return (List<ElasticProduct>) elasticProductRepository.findAll(); } @Override public ElasticProduct insertProduct(ElasticProduct product) { return elasticProductRepository.save(product); } }
ElasticSearchConfig.java
@Configuration @EnableElasticsearchRepositories public class ElasticSearchConfig extends ElasticsearchConfiguration { @Override public @NotNull ClientConfiguration clientConfiguration() { return ClientConfiguration.builder() .connectedTo("localhost:9200") .build(); } }
pom.xml依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
application.properties配置
spring.data.elasticsearch.repositories.enabled=true spring.elasticsearch.uris=https://localhost:9200
docker-compose.yml容器编排
version: '3.7' services: elastic-service: image: elasticsearch:8.8.1 container_name: elasticsearch environment: - xpack.security.enabled=false - discovery.type=single-node - "ES_JAVA_OPTS=-Xms2g -Xmx2g" ports: - "9200:9200" - "9300:9300" restart: always networks: - "my-net" mysql-service: image: mysql:latest container_name: mysql-db env_file: - api.env ports: - "3306:3306" restart: always networks: - "my-net" mongo-service: image: mongo:latest container_name: mongo-db env_file: - api.env ports: - "27017:27017" restart: always networks: - "my-net" onlinestore: image: online-store container_name: online-store-app build: context: . dockerfile: Dockerfile ports: - "8080:8080" restart: always volumes: - /var/www/images/customers:/tmp/images/customers - /var/www/images/products:/tmp/images/products depends_on: - mysql-service - mongo-service - elastic-service env_file: - api.env - prod.env user: "1000:1000" networks: - "my-net" networks: my-net: driver: bridge
排查原因与解决方案
1. 容器间网络访问地址错误
Docker容器内的localhost指向容器自身,而非宿主机或其他容器。应用容器和ElasticSearch容器同属my-net网络,需使用ElasticSearch的服务名elastic-service作为连接地址,而非localhost。
修改点:
- 调整
ElasticSearchConfig.java中的连接地址:@Override public @NotNull ClientConfiguration clientConfiguration() { return ClientConfiguration.builder() .connectedTo("elastic-service:9200") .build(); } - 修正
application.properties中的地址协议和主机:
(注:已关闭spring.elasticsearch.uris=http://elastic-service:9200xpack.security,ElasticSearch默认使用HTTP协议,无需HTTPS)
2. 配置优先级冲突
同时存在Java配置类ElasticSearchConfig和application.properties的ElasticSearch配置,可能导致配置加载冲突。建议统一配置方式:
- 方案一:保留
application.properties配置,删除自定义的ElasticSearchConfig类,Spring Boot会自动读取配置创建客户端。 - 方案二:在
ElasticSearchConfig中读取配置文件参数,避免硬编码:@Configuration @EnableElasticsearchRepositories public class ElasticSearchConfig extends ElasticsearchConfiguration { @Value("${spring.elasticsearch.uris}") private String elasticsearchUris; @Override public @NotNull ClientConfiguration clientConfiguration() { return ClientConfiguration.builder() .connectedTo(elasticsearchUris.replace("http://", "")) .build(); } }
3. 服务启动顺序与就绪检查
depends_on仅保证容器启动顺序,不确保ElasticSearch服务完全就绪(ES启动需要约10-30秒),应用启动时ES可能未完成初始化导致连接失败。
修改点:
- 在
docker-compose.yml的elastic-service中添加健康检查:elastic-service: image: elasticsearch:8.8.1 container_name: elasticsearch environment: - xpack.security.enabled=false - discovery.type=single-node - "ES_JAVA_OPTS=-Xms2g -Xmx2g" ports: - "9200:9200" - "9300:9300" restart: always networks: - "my-net" healthcheck: test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"] interval: 10s timeout: 10s retries: 5 - 修改
onlinestore服务的depends_on,等待ES健康检查通过后再启动:onlinestore: # ... 其他配置 depends_on: elastic-service: condition: service_healthy mysql-service: condition: service_started mongo-service: condition: service_started
4. 验证ElasticSearch服务状态
完成上述修改后,可进入应用容器测试连接:
docker exec -it online-store-app curl http://elastic-service:9200
若返回ElasticSearch的节点信息,说明网络连接正常。
内容的提问来源于stack exchange,提问作者Sonnka
相关产品推荐
相关产品推荐

