成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

關(guān)于Spring Cloud健康檢查的陷阱

瀏覽:5日期:2023-06-30 08:52:21
SpringCloud健康檢查的陷阱健康檢查

基于Spring Boot Actuator的健康檢查是Spring Cloud微服務(wù)的必備組件,用來確保我們的服務(wù)是否可用。

引入 Spring Boot Actuator后,通過http://ip:port/health ,可以看到 HealthEndPoint 給我們提供默認(rèn)的監(jiān)控結(jié)果,包含磁盤檢測和數(shù)據(jù)庫檢測。如下

{ 'status': 'UP', 'diskSpace': {'status': 'UP','total': 398458875904,'free': 315106918400,'threshold': 10485760 }, 'db': {'status': 'UP','database': 'MySQL','hello': 1 }}排除不必要的健康檢查項(xiàng)

有一天調(diào)用方突然反饋調(diào)不通我們的服務(wù)。查看Eureka控制臺(tái),發(fā)現(xiàn)服務(wù)狀態(tài)是UP。查看服務(wù)進(jìn)程一切正常。束手無策之際,忽然想到會(huì)不會(huì)是健康檢查在作怪,因?yàn)镋ureka Client判斷服務(wù)可用與否的依據(jù)就是健康檢查。而Spring Boot Actuator所有的監(jiān)控項(xiàng)中的任何一個(gè)健康狀態(tài)是DOWN,那個(gè)整體應(yīng)用的健康狀態(tài)也是DOWN,這時(shí)候調(diào)用方就把服務(wù)當(dāng)作不可用。

再次查看http://ip:port/health,果然發(fā)現(xiàn)有一項(xiàng)郵件健康檢查掛了。

最近項(xiàng)目引入了spring-boot-starter-mail,實(shí)現(xiàn)發(fā)送郵件的功能。

郵箱服務(wù)器掛了,造成整個(gè)服務(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ā)送不是核心功能,可以把非核心組件從健康檢查中排除,避免造成整個(gè)服務(wù)不可用。

通過如下配置關(guān)閉郵箱健康檢查。

management.health.mail.enabled=falsespringcloud-health檢查超時(shí)引發(fā)的大坑0. 前提約定

service:只一個(gè)微服務(wù)

server:只提供一個(gè)微服務(wù)的app,一般一個(gè)service有多個(gè)server。

1. 問題介紹

線上springcloud遇到這樣的問題:某些時(shí)候會(huì)移除某個(gè)service的所有server。

2. 原因分析

springcloud中默認(rèn)使用springboot-actauctor的health-url作為健康檢測,默認(rèn)檢查的超時(shí)時(shí)間為10s,如果生產(chǎn)環(huán)境遇到網(wǎng)絡(luò)、db、redis慢或者掛了等問題,會(huì)導(dǎo)致health檢查請求超時(shí),springcloud注冊中心會(huì)認(rèn)為該server異常,從而將server狀態(tài)變更為critial,服務(wù)調(diào)用方(feign)會(huì)將該異常server從負(fù)載中移除(HealthServiceServerListFilter)。

如果遇到某網(wǎng)段或更大規(guī)模的網(wǎng)絡(luò)、db等問題,會(huì)導(dǎo)致某個(gè)service所有server都被注冊中心移除,導(dǎo)致該service不可用。

但是實(shí)際上該server只是存在部分問題例如:僅僅是db或redis慢,不算不可用,但還是被注冊中心強(qiáng)制摘除了。

3. 解決辦法

3.1 通用解決辦法

關(guān)閉health檢查,永遠(yuǎn)返回up狀態(tài),只要程序正常啟動(dòng)就認(rèn)為可以提供正常服務(wù)。

如下是項(xiàng)目模板輸出默認(rèn)的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檢查需求,則需要單獨(dú)配置,配置方法如下:

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' } }}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章: