我使用Elasticsearch版本6.8.5,其中9201是HTTP端口,而9301是群集节点的端口。
在我的项目中,我使用spring boot(spring-boot-starter-data-elasticsearch)。在application.properties文件中,我设置集群节点的端口: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();
}
编辑:
检查完整的配置from the docs,当使用这样的自定义配置时,建议不要让Spring Boot尝试配置Spring Data Elasticsearch。您可以通过将应用程序类上的注释更改为
@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)
关于spring-boot - Spring Data Elasticsearch 6.8.5如何配置HTTP端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60109201/