Home > database >  Trying to run a test code using remote elasticsearch but only localhost connected
Trying to run a test code using remote elasticsearch but only localhost connected

Time:09-17

I'm trying to run a test code using remote elasticsearch cluster. The only configurations that I have are remote ES cluster addresses.

But suddenly my code looking for localhost:9200 ES cluster. Any of you knows What happened?

Logs in test results

1. Prove for remote ES cluster connection (maybe?)

2021-09-09 23:48:18.302 DEBUG 52659 --- [    Test worker] org.elasticsearch.client.RestClient      : request [GET http://{my-remote-elasticsearch-host}:{port}/] returned [HTTP/1.1 200 OK]
2021-09-09 23:48:18.336  INFO 52659 --- [    Test worker] o.s.d.elasticsearch.support.VersionInfo  : Version Spring Data Elasticsearch: 4.2.3
2021-09-09 23:48:18.337  INFO 52659 --- [    Test worker] o.s.d.elasticsearch.support.VersionInfo  : Version Elasticsearch Client in build: 7.12.1
2021-09-09 23:48:18.337  INFO 52659 --- [    Test worker] o.s.d.elasticsearch.support.VersionInfo  : Version Elasticsearch Client used: 7.12.1
2021-09-09 23:48:18.337  INFO 52659 --- [    Test worker] o.s.d.elasticsearch.support.VersionInfo  : Version Elasticsearch cluster: 7.10.1
2021-09-09 23:48:18.337  WARN 52659 --- [    Test worker] o.s.d.elasticsearch.support.VersionInfo  : Version mismatch in between Elasticsearch Client and Cluster: 7.12.1 - 7.10.1

2. Unexpected attempt to the localhost:9200

reactor.core.Exceptions$ErrorCallbackNotImplemented: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost:9200' not reachable. Cluster state is offline.
Caused by: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost:9200' not reachable. Cluster state is offline.
    at org.springframework.data.elasticsearch.client.reactive.SingleNodeHostProvider.lambda$lookupActiveHost$3(SingleNodeHostProvider.java:101)
    at org.springframework.data.elasticsearch.client.reactive.SingleNodeHostProvider$$Lambda$1993/0x0000000000000000.accept(Unknown Source)
    at reactor.core.publisher.FluxHandle$HandleSubscriber.onNext(FluxHandle.java:103)
    at reactor.core.publisher.FluxMap$MapConditionalSubscriber.onNext(FluxMap.java:220)

Codes

Bean

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        String hostAndPort = new HttpHost(getElasticsearchProperty().getConnectionProperty().getHosts(), Integer.parseInt(getElasticsearchProperty().getConnectionProperty().getPorts())).toHostString();
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
            .connectedTo(hostAndPort)
            .build();
        return RestClients.create(clientConfiguration).rest();
    }

Configuration yml

  • filename: elasticsearch-local.yml
elasticsearch:
  contents:
    connectionProperty:
      hosts: {remote-es-host}
      ports: {remote-port}
      connectionTimeout: 3000
      socketTimeout: 5000
      user: {user}
      password: {password}

Project structure

  • Based on Gradle
ㄴ project
  ㄴ project-api
  ㄴ project-elasticsearch

project-api

  • Dependent on project-elasticsearch
  • NOW I'm trying to run test codes in THIS project.

project-elasticsearch

  • All configurations or utilities related with Elasticsearch
  • I've tried to run test codes in this project, and successfully connected to the remote ES cluster.

CodePudding user response:

You didn't provide the configuration code of Elasticsearch, which according to your assumption should refer to the right host. Anyway, I believe that these links will explain everything you need to know how to configure it (I assume localhost is default since you don't configure it as needed).

  1. Rest Elasticsearch config
  2. High level Client config

CodePudding user response:

I solve my problem to add below in the project-api.

spring:
  autoconfigure:
    exclude: >
      org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticSearchReactiveHealthContributorAutoConfiguration,
      org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,
      org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration

The problem is that, Spring's autoconfiguration tried to use ReactiveElasticsearchRestClient but there were no configurations for the reactive one. So, automatically it tried to connect to the localhost.

  • Related