關于Spring Cloud健康檢查的陷阱
基于Spring Boot Actuator的健康檢查是Spring Cloud微服務的必備組件,用來確保我們的服務是否可用。
引入 Spring Boot Actuator后,通過http://ip:port/health ,可以看到 HealthEndPoint 給我們提供默認的監控結果,包含磁盤檢測和數據庫檢測。如下
{ 'status': 'UP', 'diskSpace': {'status': 'UP','total': 398458875904,'free': 315106918400,'threshold': 10485760 }, 'db': {'status': 'UP','database': 'MySQL','hello': 1 }}排除不必要的健康檢查項
有一天調用方突然反饋調不通我們的服務。查看Eureka控制臺,發現服務狀態是UP。查看服務進程一切正常。束手無策之際,忽然想到會不會是健康檢查在作怪,因為Eureka Client判斷服務可用與否的依據就是健康檢查。而Spring Boot Actuator所有的監控項中的任何一個健康狀態是DOWN,那個整體應用的健康狀態也是DOWN,這時候調用方就把服務當作不可用。
再次查看http://ip:port/health,果然發現有一項郵件健康檢查掛了。
最近項目引入了spring-boot-starter-mail,實現發送郵件的功能。
郵箱服務器掛了,造成整個服務的監控檢查狀態是DOWN。
{ 'status': 'DOWN', 'mail': { 'status': 'DOWN', 'location': 'email-smtp.test.com:-1', 'error': 'javax.mail.AuthenticationFailedException: 535 Authentication Credentials Invalidn' }, 'diskSpace': { 'status': 'UP', 'total': 266299998208, 'free': 146394308608, 'threshold': 10485760 }, 'hystrix': { 'status': 'UP' }}
由于郵件發送不是核心功能,可以把非核心組件從健康檢查中排除,避免造成整個服務不可用。
通過如下配置關閉郵箱健康檢查。
management.health.mail.enabled=falsespringcloud-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' } }}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. vue-drag-chart 拖動/縮放圖表組件的實例代碼2. vue使用moment如何將時間戳轉為標準日期時間格式3. Android studio 解決logcat無過濾工具欄的操作4. 什么是Python變量作用域5. js select支持手動輸入功能實現代碼6. PHP正則表達式函數preg_replace用法實例分析7. Android Studio3.6.+ 插件搜索不到終極解決方案(圖文詳解)8. bootstrap select2 動態從后臺Ajax動態獲取數據的代碼9. Android 實現徹底退出自己APP 并殺掉所有相關的進程10. 一個 2 年 Android 開發者的 18 條忠告
