我的SpringBoot应用程序似乎无法连接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
型
Sping Boot 3.0.6和ElasticSearch 8.8.1
1条答案
按热度按时间uqjltbpv1#
您运行的容器默认开启了安全,因此需要HTTPS连接,并会生成一些密码凭据。
要在http上启动它并且不使用creds -使用xpack.security.enabled=falseflag
字符串
附言
你不需要公共类ElasticSearchConfig,因为你的application.properties已经有了你需要的一切。