解决 Elasticsearch 的Timeout connecting to [localhost/127.0.0.1:9200]

问题描述

使用 spring data elasticsearch 来连接使用 elasticsearch, 配置如下:

# elasticsearch
spring.data.elasticsearch.cluster-nodes=xxxxx:9993
spring.data.elasticsearch.cluster-name=xxxxx

之前都运行好好的,但今天在项目中加入 Actuator 来监控系统运行情况时,报了如下错误:

2020-06-24 15:24:04.602 |-WARN  [http-nio-8730-exec-5] org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator [87] -|   Elasticsearch health check failed
java.net.ConnectException: Timeout connecting to [localhost/127.0.0.1:9200]
	at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:959)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:233)
	at org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator.doHealthCheck(ElasticsearchRestHealthIndicator.java:60)
	at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82)
	at org.springframework.boot.actuate.health.HealthIndicator.getHealth(HealthIndicator.java:37)
	at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:95)
	at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:43)
	at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:108)
	at org.springframework.boot.actuate.health.HealthEndpointSupport.getAggregateHealth(HealthEndpointSupport.java:119)
	at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:105)
	at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:83)
	at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:70)
	at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:81)
	at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:70)
	at sun.reflect.GeneratedMethodAccessor564.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282)

 

解决问题

查看错误地方 ElasticsearchRestHealthIndicator 的源码:

 

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
            builder.down();
            builder.withDetail("statusCode", statusLine.getStatusCode());
            builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
            return;
        }
        try (InputStream inputStream = response.getEntity().getContent()) {
            doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
        }
    }

可以看到使用GET请求了/_cluster/health/,但是最后报错信息是http://localhost:9200,于是在查看 elasticsearch 的自动配置类
在 RestClientProperties 中:

 

@ConfigurationProperties(prefix = "spring.elasticsearch.rest")
public class RestClientProperties {

    /**
     * Comma-separated list of the Elasticsearch instances to use.
     */
    private List uris = new ArrayList<>(Collections.singletonList("http://localhost:9200"));
}

这个 uris 应该就是导致错误的原因,默认是 http://localhost:9200,解决办法有两个,一个就是改变默认的配置,一个就是取消spring boot对Elasticsearch的健康检查

 

第一种: 
 修改默认配置

spring.elasticsearch.rest.uris=["xxxx:9200"]

 

第二种: 

取消对elastsearch的健康检查

management.health.elasticsearch.enabled=false

 

 

你可能感兴趣的:(springboot)