我有一台 ELK 服务器,需要关闭并更换为新服务器。我有一个连接到原始 ELK 服务器的 Java Spring Boot 项目。搬到新服务器后,所有索引都被重置,我丢失了所有以前的数据。
现在,我在尝试将 Spring Boot 应用程序连接到新的 ELK 服务器时遇到问题。我已验证 ELK 服务器正在运行并且可以访问。但是,当我启动 Spring Boot 应用程序时,我收到以下错误:
2024-10-31T00:42:37.816+03:00 WARN 9020 --- [data-leak-service] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller' defined in file [Controller.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'Service' defined in file [Service.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'DataRepository' defined in DataRepository defined in @EnableElasticsearchRepositories declared on ElasticSearchConfiguration: Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations' ...
我怀疑这个问题可能与ELK配置有关。这是我当前的配置:
@Configuration
public class ElasticSearchConfiguration {
@Value("${ElasticUrl}")
private String elkUrl;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
@Bean
public ElasticsearchClient elasticsearchClient() {
// Set up credentials provider for basic authentication
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(username, password)
);
// Configure RestClient with authentication
RestClient restClient = RestClient.builder(HttpHost.create(elkUrl))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.build();
// Set up transport with Jackson JSON mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// Return the authenticated Elasticsearch client
return new ElasticsearchClient(transport);
}
}
另外,我在我的
application.properties
中添加了自动创建索引的属性:
spring.elasticsearch.index.auto-create=true
我正在使用以下依赖项:
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
我使用的是 Elasticsearch 客户端版本 8.15.1。
之前,我有一个无需身份验证即可工作的不同配置:
@Configuration
public class ElasticsearchConfig {
@Bean
public ElasticsearchClient elasticsearchClient() {
// Create the low-level client (without authentication)
RestClient restClient = RestClient.builder(HttpHost.create("http://elkip:9200")).build();
// Create the transport with a Jackson JSON mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// Create the Elasticsearch client
return new ElasticsearchClient(transport);
}
}
什么可能导致此错误,如何解决?此外,我需要采取哪些步骤才能启用自动索引创建?
我通过阅读Elasticsearch Guide [8.15]的文档解决了这个问题。如果您遇到问题,也可以按照文档进行操作。我也在这里分享我的代码:
@Bean
public ElasticsearchClient elasticsearchClient() {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(HttpHost.create(elkUrl))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestClient restClient = builder.build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
}