SpringBoot Actuator健康檢查Elasticsearch異常Timeout connecting to [localhost/127.0.0....

版本環(huán)境

  • spring boot: 2.2.4.RELEASE
  • spring-data-elasticsearch: 3.2.4.RELEASE
  • Elasticsearch: 6.3.0

問題描述

使用 spring data elasticsearch 來連接使用 elasticsearch, 配置如下:

spring:
  data:
    elasticsearch:
      cluster-name: aliyun-es
      cluster-nodes: 114.xx.xx.xx:9300

之前都運行好好的介返,已經(jīng)確認 elasticsearch 的 9300 和 9200 端口無任何問題。但今天在項目中加入 Actuator 來監(jiān)控系統(tǒng)運行情況時,報了如下錯誤:

2020-05-21 19:03:47,183 WARN  [http-nio-8080-exec-3] org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator [AbstractHealthIndicator.java: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)

問題解決

查看錯誤地方 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));
        }
    }

可以看到方法第一行檢測 Elasticsearch 是否健康是使用 GET 請求訪問了 /_cluster/health 路徑惕鼓,但為什么訪問的地址是 localhost:9200 呢谴返?猜測應(yīng)該是 Spring Boot 默認的配置,于是在查看 elasticsearch 的自動配置類 org.springframework.boot.autoconfigure.elasticsearch.
在 RestClientProperties 中:

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

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

這個 uris 應(yīng)該就是導(dǎo)致錯誤的原因蜡饵,默認是 http://localhost:9200管怠,所以配置下:

spring:
  data:
    elasticsearch:
      cluster-name: aliyun-es
      cluster-nodes: 114.xx.xx.xx:9300
  elasticsearch:
    rest:
      uris: ["114.xx.xx.xx:9200"]
      connection-timeout: 10s

重新運行后再次出錯:

2020-05-21 19:28:51,726 WARN  [http-nio-8080-exec-8] org.springframework.boot.actuate.elasticsearch.ElasticsearchHealthIndicator [AbstractHealthIndicator.java:87] Elasticsearch health check failed
org.elasticsearch.ElasticsearchTimeoutException: java.util.concurrent.TimeoutException: Timeout waiting for task.
    at org.elasticsearch.common.util.concurrent.FutureUtils.get(FutureUtils.java:79)
    at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:54)
    at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:44)
    at org.springframework.boot.actuate.elasticsearch.ElasticsearchHealthIndicator.doHealthCheck(ElasticsearchHealthIndicator.java:79)
    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)

錯誤說是連接超時,debug 發(fā)現(xiàn)發(fā)送請求檢測的超時時間只有 100 毫秒


debug

這個時間太快了亏掀,我的網(wǎng)絡(luò)環(huán)境不支持,需要增加點超時時間泛释,由于這個健康檢測是由 Actuator 執(zhí)行的滤愕,于是去查看 Actuator 中 Elasticsearch 的自動配置類,在 ElasticsearchHealthIndicatorProperties 中找到:

@ConfigurationProperties(
    prefix = "management.health.elasticsearch",
    ignoreUnknownFields = false
)
@Deprecated
public class ElasticsearchHealthIndicatorProperties {
    private List<String> indices = new ArrayList();
    private Duration responseTimeout = Duration.ofMillis(100L);
}

可以看到 responseTimeout 為 100 毫秒怜校,和上面 debug 的時間一致间影,應(yīng)該就是這一項了,修改 yml :

# actuator
management:
  endpoints:
    web:
      exposure:
        include: ['*']
  health:
    elasticsearch:
      response-timeout: 3s

再次運行茄茁,無誤魂贬。

還有一種方式也可以解決,但是并不是一種好的解決方式,那就是關(guān)閉 actuator 對 elasticsearch 的健康檢查:

management:
      health:
        elasticsearch:
          enabled: false
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市裙顽,隨后出現(xiàn)的幾起案子付燥,更是在濱河造成了極大的恐慌,老刑警劉巖愈犹,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件键科,死亡現(xiàn)場離奇詭異,居然都是意外死亡漩怎,警方通過查閱死者的電腦和手機勋颖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勋锤,“玉大人饭玲,你說我怎么就攤上這事∪矗” “怎么了茄厘?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長徒恋。 經(jīng)常有香客問我蚕断,道長,這世上最難降的妖魔是什么入挣? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任亿乳,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘葛假。我一直安慰自己障陶,他們只是感情好,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布聊训。 她就那樣靜靜地躺著抱究,像睡著了一般。 火紅的嫁衣襯著肌膚如雪带斑。 梳的紋絲不亂的頭發(fā)上鼓寺,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機與錄音勋磕,去河邊找鬼妈候。 笑死,一個胖子當著我的面吹牛挂滓,可吹牛的內(nèi)容都是我干的苦银。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赶站,長吁一口氣:“原來是場噩夢啊……” “哼幔虏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起贝椿,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤想括,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后烙博,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體主胧,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年习勤,在試婚紗的時候發(fā)現(xiàn)自己被綠了踪栋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡图毕,死狀恐怖夷都,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情予颤,我是刑警寧澤囤官,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站蛤虐,受9級特大地震影響党饮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驳庭,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一刑顺、第九天 我趴在偏房一處隱蔽的房頂上張望氯窍。 院中可真熱鬧,春花似錦蹲堂、人聲如沸狼讨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽政供。三九已至,卻和暖如春朽基,著一層夾襖步出監(jiān)牢的瞬間布隔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工稼虎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留执泰,地道東北人。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓渡蜻,卻偏偏與公主長得像,于是被迫代替她去往敵國和親计济。 傳聞我的和親對象是個殘疾皇子茸苇,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354