简体   繁体   中英

Spring Boot and Elasticsearch: NoReachableHostException: Host 'localhost/<unresolved>:9200' not reachable

I have a very simple use case, I just want to use Spring Boot (2.5.2) to put documents into an elasticsearch index. I keep seeing this error when the application starts up:

Caused by: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost/:9200' not reachable. Cluster state is offline.

It seems to be just loading the default host and port for elasticsearch, but I'm not sure why. I've tried moving the properties around. I've tried not using the Repository, but instead just using the RestHighLevelClient. No matter what, I keep getting this error on startup.

I'm using the spring-boot-starter-data-elasticsearch

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch'

Here's my application.yml

spring:
  elasticsearch:
    rest:
      uris: <my host>:<my port>
      username: <username>
      password: <password>

The Document object I'm trying to send

@Document(indexName = "#{@environment.getProperty('indices.someobject')}", createIndex = false)
public class SomeObjectDocument{
    @Id
    private String id;

    private SomeObject object;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public SomeObject getSomeObject () {
        return someObject;
    }

    public void setSomeObject (SomeObject someObject ) {
        this.someObject = someObject;
    }
}

The Client Configuration

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.some.company.project.elastic.repository")
public class ElasticClientConfig {
    @Value("${spring.elasticsearch.rest.username}")
    private String elasticUsername;

    @Value("${spring.elasticsearch.rest.password}")
    private String elasticPassword;

    @Value("${spring.elasticsearch.rest.uris}")
    private String uri;

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        LOG.info(uri);
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(uri)
                .usingSsl()
                .withBasicAuth(elasticUsername, elasticPassword)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    public ElasticsearchOperations elasticsearchOperations() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}

And for the Repository, I just created it and didn't add any other methods because I just need to save

public interface SomeObjectRepository extends ElasticsearchRepository<SomeObjectDocument, String> {

}

I think your problem about elasticsearch instance. Are you sure elasticsearch instance working on 9200 port?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM