關(guān)于Spring Cloud健康檢查的陷阱
基于Spring Boot Actuator的健康檢查是Spring Cloud微服務(wù)的必備組件,用來確保我們的服務(wù)是否可用。
引入 Spring Boot Actuator后,通過http://ip:port/health ,可以看到 HealthEndPoint 給我們提供默認的監(jiān)控結(jié)果,包含磁盤檢測和數(shù)據(jù)庫檢測。如下
{ 'status': 'UP', 'diskSpace': {'status': 'UP','total': 398458875904,'free': 315106918400,'threshold': 10485760 }, 'db': {'status': 'UP','database': 'MySQL','hello': 1 }}排除不必要的健康檢查項
有一天調(diào)用方突然反饋調(diào)不通我們的服務(wù)。查看Eureka控制臺,發(fā)現(xiàn)服務(wù)狀態(tài)是UP。查看服務(wù)進程一切正常。束手無策之際,忽然想到會不會是健康檢查在作怪,因為Eureka Client判斷服務(wù)可用與否的依據(jù)就是健康檢查。而Spring Boot Actuator所有的監(jiān)控項中的任何一個健康狀態(tài)是DOWN,那個整體應(yīng)用的健康狀態(tài)也是DOWN,這時候調(diào)用方就把服務(wù)當(dāng)作不可用。
再次查看http://ip:port/health,果然發(fā)現(xiàn)有一項郵件健康檢查掛了。
最近項目引入了spring-boot-starter-mail,實現(xiàn)發(fā)送郵件的功能。
郵箱服務(wù)器掛了,造成整個服務(wù)的監(jiān)控檢查狀態(tài)是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' }}
由于郵件發(fā)送不是核心功能,可以把非核心組件從健康檢查中排除,避免造成整個服務(wù)不可用。
通過如下配置關(guān)閉郵箱健康檢查。
management.health.mail.enabled=falsespringcloud-health檢查超時引發(fā)的大坑0. 前提約定
service:只一個微服務(wù)
server:只提供一個微服務(wù)的app,一般一個service有多個server。
1. 問題介紹線上springcloud遇到這樣的問題:某些時候會移除某個service的所有server。
2. 原因分析springcloud中默認使用springboot-actauctor的health-url作為健康檢測,默認檢查的超時時間為10s,如果生產(chǎn)環(huán)境遇到網(wǎng)絡(luò)、db、redis慢或者掛了等問題,會導(dǎo)致health檢查請求超時,springcloud注冊中心會認為該server異常,從而將server狀態(tài)變更為critial,服務(wù)調(diào)用方(feign)會將該異常server從負載中移除(HealthServiceServerListFilter)。
如果遇到某網(wǎng)段或更大規(guī)模的網(wǎng)絡(luò)、db等問題,會導(dǎo)致某個service所有server都被注冊中心移除,導(dǎo)致該service不可用。
但是實際上該server只是存在部分問題例如:僅僅是db或redis慢,不算不可用,但還是被注冊中心強制摘除了。
3. 解決辦法3.1 通用解決辦法
關(guān)閉health檢查,永遠返回up狀態(tài),只要程序正常啟動就認為可以提供正常服務(wù)。
如下是項目模板輸出默認的health檢查結(jié)果:
{ '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' } }}
關(guān)閉health檢查的方法:
# application*.yml中management: health: defaults: enabled: false
關(guān)閉后health檢查結(jié)果:
{ 'description': '', 'status': 'UP', 'application': { 'description': '', 'status': 'UP' }}4. 如果有特定health檢查的需求
關(guān)閉health檢查后,如果需要某類health檢查需求,則需要單獨配置,配置方法如下:
management: health: defaults: enabled: false # 如下配置則打開db-health檢查 db: enabled: true
health檢查結(jié)果如下:
{ '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' } }}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python+unittest+requests 接口自動化測試框架搭建教程2. Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法3. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)4. 存儲于xml中需要的HTML轉(zhuǎn)義代碼5. Springboot 全局日期格式化處理的實現(xiàn)6. 利用CSS制作3D動畫7. 一款功能強大的markdown編輯器tui.editor使用示例詳解8. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程9. SpringBoot+TestNG單元測試的實現(xiàn)10. PHP利用COM對象訪問SQLServer、Access
