>我使用 Elasticsearch 版本 6.8.5,9201 是 HTTP 端口,9301 是集群节点的端口。 在我的项目中,我使用Spring boot(spring-boot-starter-data-elasticsearch(。在应用程序属性文件上,我设置了群集节点的端口:
spring.data.elasticsearch.cluster-nodes=localhost:9301
但是我不知道如何设置HTTP端口。因此,当我开始我的项目时,我收到一个错误:
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{de81Kcj-QUqTRdA9HskFWg}{localhost}{localhost:9301}]];
我尝试使用高级 REST 客户端设置 (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest(,但它仍然不起作用:
@Configuration
public class ElasticsearchConfig {
@Bean(destroyMethod = "close")
public RestHighLevelClient restClient1() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9201));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
如何配置 HTTP 端口(非默认端口(?
应使用以下代码:
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9201")
// if you need basic authentication:
.withBasicAuth("user", "password")
.build();
return RestClients.create(clientConfiguration).rest();
}
编辑:
检查文档中的完整配置,当使用这样的自定义配置时,我建议不要让 Spring Boot 尝试配置 Spring Data Elasticsearch。为此,可以将应用程序类上的注释更改为
@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)