springcloud-health檢查超時引發的大坑

0. 前提約定

  • service:只一個微服務
  • server:只提供一個微服務的app,一般一個service有多個server。

1. 問題介紹

線上springcloud遇到這樣的問題:某些時候會移除某個service的所有server。

2. 原因分析

springcloud中默認使用springboot-actauctor的health-url作爲健康檢測,默認檢查的超時時間爲10s,如果生產環境遇到網絡、db、redis慢或者掛了等問題,會導致health檢查請求超時,springcloud註冊中心會認爲該server異常,從而將server狀態變更爲critial,服務調用方(feign)會將該異常server從負載中移除(HealthServiceServerListFilter)。 如果遇到某網段或更大規模的網絡、db等問題,會導致某個service所有server都被註冊中心移除,導致該service不可用。但是實際上該server只是存在部分問題例如:僅僅是db或redis慢,不算不可用,但還是被註冊中心強制摘除了。

3. 解決辦法

3.1 通用解決辦法

關閉health檢查,永遠返回up狀態,只要程序正常啓動就認爲可以提供正常服務。
如下是項目模板輸出默認的health檢查結果:

{
	"description": "",
	"status": "UP",
	"diskSpace": {
		"description": "",
		"status": "UP",
		"total": 50715856896,
		"free": 7065239552,
		"threshold": 10485760
	},
	"solr": {
		"description": "",
		"status": "UP",
		"solrStatus": "OK"
	},
	"redis": {
		"description": "",
		"status": "UP",
		"version": "2.8.21"
	},
	"db": {
		"description": "",
		"status": "UP",
		"authDataSource": {
			"description": "",
			"status": "UP",
			"database": "MySQL",
			"hello": "x"
		},
		"autodealerDataSource": {
			"description": "",
			"status": "UP",
			"database": "Microsoft SQL Server",
			"hello": "x"
		}
	}
}

關閉health檢查的方法:

# application*.yml中
management:
  health:
    defaults:
      enabled: false

關閉後health檢查結果:

{
	"description": "",
	"status": "UP",
	"application": {
		"description": "",
		"status": "UP"
	}
}

4. 如果有特定health檢查的需求

關閉health檢查後,如果需要某類health檢查需求,則需要單獨配置,配置方法如下:

management:
  health:
    defaults:
      enabled: false
    # 如下配置則打開db-health檢查
    db:
      enabled: true

health檢查結果如下:

{
	"description": "",
	"status": "UP",
	"db": {
		"description": "",
		"status": "UP",
		"authDataSource": {
			"description": "",
			"status": "UP",
			"database": "MySQL",
			"hello": "x"
		},
		"autodealerDataSource": {
			"description": "",
			"status": "UP",
			"database": "Microsoft SQL Server",
			"hello": "x"
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章