日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud实战小贴士:健康检查

發(fā)布時間:2024/7/5 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud实战小贴士:健康检查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天在博客的交流區(qū)收到一條不錯的問題,拿出來給大家分享一下。具體問題如下:

因為項目里面用到了redis集群,但并不是用spring boot的配置方式,啟動后項目健康檢查老是檢查redis的時候狀態(tài)為down,導致注冊到eureka后項目狀態(tài)也是down。
問下能不能設置spring boot不檢查 redis的健康狀態(tài)

"redis": {
"status": "DOWN",
"error": "org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool"
}

問題原帖可點擊此處跳轉!

原因分析

如提問者所述,由于在Spring Boot項目中引用了Redis模塊,所以Spring Boot Actuator會對其進行健康檢查,正常情況下不會出現問題,但是由于采用了其他配置方式,導致redis的連接檢查沒有通過。這樣就會導致了Consul或Eureka的HealthCheck認為該服務是DOWN狀態(tài)。

那么redis的健康檢查是如何實現的呢?我們不妨來看看健康檢查的自動化配置中針對redis的配置源碼:

@Configuration
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
public static class RedisHealthIndicatorConfiguration extends
CompositeHealthIndicatorConfiguration<RedisHealthIndicator, RedisConnectionFactory> {

@Autowired
private Map<String, RedisConnectionFactory> redisConnectionFactories;

@Bean
@ConditionalOnMissingBean(name = "redisHealthIndicator")
public HealthIndicator redisHealthIndicator() {
return createHealthIndicator(this.redisConnectionFactories);
}

}

以上內容取自:org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration類。從自動化配置中我們可以看到,會自動加載一個針對redis的HealthIndicator實現,并且該Bean命名為redisHealthIndicator。

解決方法

通過上面的分析,我們已經知道了是哪個Bean導致了服務實例的健康檢查不通過,那么如何解決該問題的方法也就馬上能想到了:我們只需要再實現一個redis的HealthIndicator實現來替代原先默認的檢查邏輯。比如:

@Component
public class RedisHealthIndicator implements HealthIndicator {

@Override
public Health health() {
return Health.up().build();
}

}

上面通過實現HealthIndicator接口中的health方法,直接返回了up狀態(tài)。通過@Component注解,讓Spring Boot掃描到該類就能自動的進行加載,并覆蓋原來的redis健康檢查實現。當然,這里的實現并不好,因為它只是為了讓健康檢查可以通過,但是并沒有做真正的健康檢查。如提問者所說,采用了其他配置訪問,那么正確的做法就是在health方法中實現針對其他配置的內容進行健康檢查。

注意:這里隱含了一個實現命名的問題,由于默認的bean名稱會使用redisHealthIndicator,所以這里的定義可以替換默認的實現,因為它的名字與@ConditionalOnMissingBean(name = "redisHealthIndicator")中的命名一致。但是如果您自定義的實現類并非叫RedisHealthIndicator,它的默認名稱與自動化配置的名稱是不匹配的,那么就不會替換,這個時候需要在@Component注解中指定該Bean的名稱為redisHealthIndicator


總結

以上是生活随笔為你收集整理的Spring Cloud实战小贴士:健康检查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。