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