【问题标题】:Spring Boot and Elasticsearch: NoReachableHostException: Host 'localhost/<unresolved>:9200' not reachableSpring Boot 和 Elasticsearch:NoReachableHostException:主机 'localhost/<unresolved>:9200' 不可访问
【发布时间】:2022-01-12 22:19:10
【问题描述】:

我有一个非常简单的用例,我只想使用 Spring Boot (2.5.2) 将文档放入 elasticsearch 索引。应用程序启动时我一直看到此错误:

原因:org.springframework.data.elasticsearch.client.NoReachableHostException:无法访问主机'localhost/:9200'。集群状态为离线。

似乎只是为 elasticsearch 加载默认主机和端口,但我不知道为什么。我试过移动这些属性。我试过不使用存储库,而是只使用 RestHighLevelClient。无论如何,我总是在启动时收到此错误。

我正在使用 spring-boot-starter-data-elasticsearch

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

这是我的 application.yml

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

我要发送的 Document 对象

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

客户端配置

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

而对于Repository,我只是创建了它,并没有添加任何其他方法,因为我只需要保存

public interface SomeObjectRepository extends ElasticsearchRepository<SomeObjectDocument, String> {

}

【问题讨论】:

  • 你在&lt;my host&gt; 有什么?如果是host/,则需要删除/
  • 在我的 application.yml 的 spring.elasticsearch.rest.uris 属性中,我有类似 myhost.company.com:443 的东西,所以它没有 /。看起来我实际看到的错误消息被截断了一点。在错误消息中,这是它所说的无法访问的主机:“localhost/:9200”。我认为 来自我设置 indexName 的@Document,它无法解决它?我只是不明白为什么它使用 localhost。
  • 嗨@H.Miller,你有没有解决过这个问题?我也遇到了同样的问题

标签: spring-boot elasticsearch


【解决方案1】:

我认为您对弹性搜索实例的问题。你确定 elasticsearch 实例在 9200 端口上工作吗?

【讨论】:

  • 不,elasticsearch 在完全不同的主机上运行。我在 application.yml 的 spring.elasticsearch.rest.uris 属性中配置了它,例如“myhost.company.com:443”。当我使用“LOG.info(uri);”将它登录到 ElasticClientConfig 中时它确实正确打印了“myhost.company.com:443”。我不知道 localhost:9200 来自哪里,除了它似乎是 elasticsearch 的默认值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 2020-07-28
  • 2014-03-15
  • 2019-07-13
  • 2021-04-16
  • 1970-01-01
  • 2019-07-04
相关资源
最近更新 更多