简体   繁体   English

Spring 启动和 Elasticsearch: NoReachableHostException: Host 'localhost/<unresolved> :9200' 无法访问</unresolved>

[英]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.我有一个非常简单的用例,我只想使用 Spring Boot (2.5.2) 将文档放入 elasticsearch 索引中。 I keep seeing this error when the application starts up:应用程序启动时,我一直看到此错误:

Caused by: org.springframework.data.elasticsearch.client.NoReachableHostException: Host 'localhost/:9200' not reachable.原因:org.springframework.data.elasticsearch.client.NoReachableHostException:主机'localhost/:9200'不可访问。 Cluster state is offline.集群 state 处于离线状态。

It seems to be just loading the default host and port for elasticsearch, but I'm not sure why.它似乎只是加载 elasticsearch 的默认主机和端口,但我不知道为什么。 I've tried moving the properties around.我试过移动这些属性。 I've tried not using the Repository, but instead just using the RestHighLevelClient.我试过不使用存储库,而是只使用 RestHighLevelClient。 No matter what, I keep getting this error on startup.无论如何,我在启动时不断收到此错误。

I'm using the spring-boot-starter-data-elasticsearch我正在使用 spring-boot-starter-data-elasticsearch

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

Here's my application.yml这是我的 application.yml

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

The Document object I'm trying to send我正在尝试发送的文档 object

@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.我认为您关于 elasticsearch 实例的问题。 Are you sure elasticsearch instance working on 9200 port?您确定 elasticsearch 实例在 9200 端口上工作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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