[英]Spring Boot and Elasticsearch: NoReachableHostException: Host 'localhost/<unresolved>:9200' not reachable
我有一個非常簡單的用例,我只想使用 Spring Boot (2.5.2) 將文檔放入 elasticsearch 索引中。 應用程序啟動時,我一直看到此錯誤:
原因:org.springframework.data.elasticsearch.client.NoReachableHostException:主機'localhost/:9200'不可訪問。 集群 state 處於離線狀態。
它似乎只是加載 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>
我正在嘗試發送的文檔 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;
}
}
客戶端配置
@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());
}
}
對於存儲庫,我只是創建了它,沒有添加任何其他方法,因為我只需要保存
public interface SomeObjectRepository extends ElasticsearchRepository<SomeObjectDocument, String> {
}
我認為您關於 elasticsearch 實例的問題。 您確定 elasticsearch 實例在 9200 端口上工作嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.