0
votes

I use Elasticsearch version 6.8.5 with 9201 is HTTP port and 9301 is port of cluster node. On my project, I use spring boot (spring-boot-starter-data-elasticsearch). On application.properties file, I set port of cluster node:

spring.data.elasticsearch.cluster-nodes=localhost:9301

But I don't know how to set HTTP port. So when I start my project, I get an error:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{de81Kcj-QUqTRdA9HskFWg}{localhost}{localhost:9301}]];

I tried to use High Level REST Client setting (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest), but it still not work:

@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;
    }
}

How can I config HTTP port (not default port)?

1
which version of Spring Data Elasticsearch are you using?P.J.Meisch
I use 3.2.3 version and my spring boot version is 2.2.2Bằng Ngô Duy

1 Answers

0
votes

You should use the following code:

@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();
}

Edit:

Check the complete configuration from the docs, when using a custom configuration like this, I'd recommend to not let Spring Boot try to configure Spring Data Elasticsearch. You can do this by changing the annotation on your application class to

@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)