日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

詳解SpringBoot健康檢查的實現原理

瀏覽:82日期:2023-03-21 13:15:37

SpringBoot自動裝配的套路,直接看 spring.factories 文件,當我們使用的時候只需要引入如下依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

然后在 org.springframework.boot.spring-boot-actuator-autoconfigure 包下去就可以找到這個文件

自動裝配

查看這個文件發現引入了很多的配置類,這里先關注一下 XXXHealthIndicatorAutoConfiguration 系列的類,這里咱們拿第一個 RabbitHealthIndicatorAutoConfiguration 為例來解析一下。看名字就知道這個是RabbitMQ的健康檢查的自動配置類

@Configuration@ConditionalOnClass(RabbitTemplate.class)@ConditionalOnBean(RabbitTemplate.class)@ConditionalOnEnabledHealthIndicator('rabbit')@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)@AutoConfigureAfter(RabbitAutoConfiguration.class)public class RabbitHealthIndicatorAutoConfiguration extends CompositeHealthIndicatorConfiguration<RabbitHealthIndicator, RabbitTemplate> { private final Map<String, RabbitTemplate> rabbitTemplates; public RabbitHealthIndicatorAutoConfiguration( Map<String, RabbitTemplate> rabbitTemplates) { this.rabbitTemplates = rabbitTemplates; } @Bean @ConditionalOnMissingBean(name = 'rabbitHealthIndicator') public HealthIndicator rabbitHealthIndicator() { return createHealthIndicator(this.rabbitTemplates); }}

按照以往的慣例,先解析注解

@ConditionalOnXXX 系列又出現了,前兩個就是說如果當前存在 RabbitTemplate 這個bean也就是說我們的項目中使用到了RabbitMQ才能進行下去 @ConditionalOnEnabledHealthIndicator 這個注解很明顯是SpringBoot actuator自定義的注解,看一下吧

@Conditional(OnEnabledHealthIndicatorCondition.class)public @interface ConditionalOnEnabledHealthIndicator { String value();}class OnEnabledHealthIndicatorCondition extends OnEndpointElementCondition { OnEnabledHealthIndicatorCondition() { super('management.health.', ConditionalOnEnabledHealthIndicator.class); }}public abstract class OnEndpointElementCondition extends SpringBootCondition { private final String prefix; private final Class<? extends Annotation> annotationType; protected OnEndpointElementCondition(String prefix, Class<? extends Annotation> annotationType) { this.prefix = prefix; this.annotationType = annotationType; } @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { AnnotationAttributes annotationAttributes = AnnotationAttributes .fromMap(metadata.getAnnotationAttributes(this.annotationType.getName())); String endpointName = annotationAttributes.getString('value'); ConditionOutcome outcome = getEndpointOutcome(context, endpointName); if (outcome != null) { return outcome; } return getDefaultEndpointsOutcome(context); } protected ConditionOutcome getEndpointOutcome(ConditionContext context, String endpointName) { Environment environment = context.getEnvironment(); String enabledProperty = this.prefix + endpointName + '.enabled'; if (environment.containsProperty(enabledProperty)) { boolean match = environment.getProperty(enabledProperty, Boolean.class, true); return new ConditionOutcome(match, ConditionMessage.forCondition(this.annotationType).because( this.prefix + endpointName + '.enabled is ' + match)); } return null; } protected ConditionOutcome getDefaultEndpointsOutcome(ConditionContext context) { boolean match = Boolean.valueOf(context.getEnvironment() .getProperty(this.prefix + 'defaults.enabled', 'true')); return new ConditionOutcome(match, ConditionMessage.forCondition(this.annotationType).because( this.prefix + 'defaults.enabled is considered ' + match)); }}public abstract class SpringBootCondition implements Condition { private final Log logger = LogFactory.getLog(getClass()); @Override public final boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { String classOrMethodName = getClassOrMethodName(metadata); try { ConditionOutcome outcome = getMatchOutcome(context, metadata); logOutcome(classOrMethodName, outcome); recordEvaluation(context, classOrMethodName, outcome); return outcome.isMatch(); } catch (NoClassDefFoundError ex) { throw new IllegalStateException( 'Could not evaluate condition on ' + classOrMethodName + ' due to ' + ex.getMessage() + ' not ' + 'found. Make sure your own configuration does not rely on ' + 'that class. This can also happen if you are ' + '@ComponentScanning a springframework package (e.g. if you ' + 'put a @ComponentScan in the default package by mistake)', ex); } catch (RuntimeException ex) { throw new IllegalStateException( 'Error processing condition on ' + getName(metadata), ex); } } private void recordEvaluation(ConditionContext context, String classOrMethodName, ConditionOutcome outcome) { if (context.getBeanFactory() != null) { ConditionEvaluationReport.get(context.getBeanFactory()) .recordConditionEvaluation(classOrMethodName, this, outcome); } }}

上方的入口方法是 SpringBootCondition 類的 matches 方法, getMatchOutcome 這個方法則是子類 OnEndpointElementCondition 的,這個方法首先會去環境變量中查找是否存在 management.health.rabbit.enabled 屬性,如果沒有的話則去查找 management.health.defaults.enabled 屬性,如果這個屬性還沒有的話則設置默認值為true

當這里返回true時整個 RabbitHealthIndicatorAutoConfiguration 類的自動配置才能繼續下去

@AutoConfigureBefore 既然這樣那就先看看類 HealthIndicatorAutoConfiguration 都是干了啥再回來吧

@Configuration@EnableConfigurationProperties({ HealthIndicatorProperties.class })public class HealthIndicatorAutoConfiguration { private final HealthIndicatorProperties properties; public HealthIndicatorAutoConfiguration(HealthIndicatorProperties properties) { this.properties = properties; } @Bean @ConditionalOnMissingBean({ HealthIndicator.class, ReactiveHealthIndicator.class }) public ApplicationHealthIndicator applicationHealthIndicator() { return new ApplicationHealthIndicator(); } @Bean @ConditionalOnMissingBean(HealthAggregator.class) public OrderedHealthAggregator healthAggregator() { OrderedHealthAggregator healthAggregator = new OrderedHealthAggregator(); if (this.properties.getOrder() != null) { healthAggregator.setStatusOrder(this.properties.getOrder()); } return healthAggregator; }}

首先這個類引入了配置文件 HealthIndicatorProperties 這個配置類是系統狀態相關的配置

@ConfigurationProperties(prefix = 'management.health.status')public class HealthIndicatorProperties { private List<String> order = null; private final Map<String, Integer> httpMapping = new HashMap<>();}

接著就是注冊了2個bean ApplicationHealthIndicator 和 OrderedHealthAggregator 這兩個bean的作用稍后再說,現在回到 RabbitHealthIndicatorAutoConfiguration 類

@AutoConfigureAfterHealthIndicatorpublic abstract class CompositeHealthIndicatorConfiguration<H extends HealthIndicator, S> { @Autowired private HealthAggregator healthAggregator; protected HealthIndicator createHealthIndicator(Map<String, S> beans) { if (beans.size() == 1) { return createHealthIndicator(beans.values().iterator().next()); } CompositeHealthIndicator composite = new CompositeHealthIndicator( this.healthAggregator); for (Map.Entry<String, S> entry : beans.entrySet()) { composite.addHealthIndicator(entry.getKey(), createHealthIndicator(entry.getValue())); } return composite; } @SuppressWarnings('unchecked') protected H createHealthIndicator(S source) { Class<?>[] generics = ResolvableType .forClass(CompositeHealthIndicatorConfiguration.class, getClass()) .resolveGenerics(); Class<H> indicatorClass = (Class<H>) generics[0]; Class<S> sourceClass = (Class<S>) generics[1]; try { return indicatorClass.getConstructor(sourceClass).newInstance(source); } catch (Exception ex) { throw new IllegalStateException('Unable to create indicator ' + indicatorClass + ' for source ' + sourceClass, ex); } }} 首先這里注入了一個對象 HealthAggregator ,這個對象就是剛才注冊的 OrderedHealthAggregator 第一個 createHealthIndicator 方法執行邏輯為:如果傳入的beans的size 為1,則調用 createHealthIndicator 創建 HealthIndicator 否則創建 CompositeHealthIndicator ,遍歷傳入的beans,依次創建 HealthIndicator ,加入到 CompositeHealthIndicator 中 第二個 createHealthIndicator 的執行邏輯為:獲得 CompositeHealthIndicatorConfiguration 中的泛型參數根據泛型參數H對應的class和S對應的class,在H對應的class中找到聲明了參數為S類型的構造器進行實例化 最后這里創建出來的bean為 RabbitHealthIndicator 回憶起之前學習健康檢查的使用時,如果我們需要自定義健康檢查項時一般的操作都是實現 HealthIndicator 接口,由此可以猜測 RabbitHealthIndicator 應該也是這樣做的。觀察這個類的繼承關系可以發現這個類繼承了一個實現實現此接口的類 AbstractHealthIndicator ,而RabbitMQ的監控檢查流程則如下代碼所示

//這個方法是AbstractHealthIndicator的public final Health health() { Health.Builder builder = new Health.Builder(); try { doHealthCheck(builder); } catch (Exception ex) { if (this.logger.isWarnEnabled()) { String message = this.healthCheckFailedMessage.apply(ex); this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex); } builder.down(ex); } return builder.build(); }//下方兩個方法是由類RabbitHealthIndicator實現的protected void doHealthCheck(Health.Builder builder) throws Exception { builder.up().withDetail('version', getVersion()); } private String getVersion() { return this.rabbitTemplate.execute((channel) -> channel.getConnection() .getServerProperties().get('version').toString()); }健康檢查

上方一系列的操作之后,其實就是搞出了一個RabbitMQ的 HealthIndicator 實現類,而負責檢查RabbitMQ健康不健康也是這個類來負責的。由此我們可以想象到如果當前環境存在MySQL、Redis、ES等情況應該也是這么個操作

那么接下來無非就是當有調用方訪問如下地址時,分別調用整個系統的所有的 HealthIndicator 的實現類的 health 方法即可了

http://ip:port/actuator/health

HealthEndpointAutoConfiguration

上邊說的這個操作過程就在類 HealthEndpointAutoConfiguration 中,這個配置類同樣也是在 spring.factories 文件中引入的

@Configuration@EnableConfigurationProperties({HealthEndpointProperties.class, HealthIndicatorProperties.class})@AutoConfigureAfter({HealthIndicatorAutoConfiguration.class})@Import({HealthEndpointConfiguration.class, HealthEndpointWebExtensionConfiguration.class})public class HealthEndpointAutoConfiguration { public HealthEndpointAutoConfiguration() { }}

這里重點的地方在于引入的 HealthEndpointConfiguration 這個類

@Configurationclass HealthEndpointConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint public HealthEndpoint healthEndpoint(ApplicationContext applicationContext) { return new HealthEndpoint(HealthIndicatorBeansComposite.get(applicationContext)); }}

這個類只是構建了一個類 HealthEndpoint ,這個類我們可以理解為一個SpringMVC的Controller,也就是處理如下請求的

http://ip:port/actuator/health

那么首先看一下它的構造方法傳入的是個啥對象吧

public static HealthIndicator get(ApplicationContext applicationContext) { HealthAggregator healthAggregator = getHealthAggregator(applicationContext); Map<String, HealthIndicator> indicators = new LinkedHashMap<>(); indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class)); if (ClassUtils.isPresent('reactor.core.publisher.Flux', null)) { new ReactiveHealthIndicators().get(applicationContext) .forEach(indicators::putIfAbsent); } CompositeHealthIndicatorFactory factory = new CompositeHealthIndicatorFactory(); return factory.createHealthIndicator(healthAggregator, indicators); }

跟我們想象中的一樣,就是通過Spring容器獲取所有的 HealthIndicator 接口的實現類,我這里只有幾個默認的和RabbitMQ

詳解SpringBoot健康檢查的實現原理

然后都放入了其中一個聚合的實現類 CompositeHealthIndicator 中

既然 HealthEndpoint構建好了,那么只剩下最后一步處理請求了

@Endpoint(id = 'health')public class HealthEndpoint { private final HealthIndicator healthIndicator; @ReadOperation public Health health() { return this.healthIndicator.health(); }}

剛剛我們知道,這個類是通過 CompositeHealthIndicator 構建的,所以 health 方法的實現就在這個類中

public Health health() { Map<String, Health> healths = new LinkedHashMap<>(); for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) { //循環調用 healths.put(entry.getKey(), entry.getValue().health()); } //對結果集排序 return this.healthAggregator.aggregate(healths); }

至此SpringBoot的健康檢查實現原理全部解析完成

以上就是詳解SpringBoot健康檢查的實現原理的詳細內容,更多關于SpringBoot健康檢查實現原理的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日本精品另类| 久久影院资源站| 色偷偷偷在线视频播放| 老司机精品在线| 精品视频在线一区二区在线| 欧美日韩1区2区3区| 88久久精品| 国产精品伦一区二区| 少妇精品久久久一区二区| 蜜桃av一区二区三区电影| 久久最新视频| 国产精品一区2区3区| 91亚洲国产| 日韩视频精品在线观看| 亚洲精品欧美| 久久wwww| 欧美日韩国产欧| 人人爱人人干婷婷丁香亚洲| 久久精品国产久精国产| 香蕉视频亚洲一级| 色综合视频一区二区三区日韩 | 青青青免费在线视频| 久久中文亚洲字幕| 日韩一区二区三区高清在线观看| 国产精品欧美一区二区三区不卡 | 夜夜嗨av一区二区三区网站四季av| 免费的成人av| 国内揄拍国内精品久久| 欧美二区视频| 久久99精品久久久野外观看| 欧美va天堂在线| 国产经典一区| 免费日本视频一区| 韩日一区二区| 国产亚洲久久| 欧美日韩视频一区二区三区| 国产精品中文| 久久福利精品| 亚洲视频综合| 精品国产午夜| 亚洲图片久久| 亚洲成人三区| 免费福利视频一区二区三区| 日本成人一区二区| 香蕉久久久久久久av网站| 四虎国产精品免费观看| 国产亚洲第一伦理第一区| 日韩中文字幕一区二区三区| 国产+成+人+亚洲欧洲在线| 日韩精品第一| 日韩精品成人| 日韩欧美中文在线观看| 免费日韩一区二区| 尤物精品在线| 国产精品婷婷| 在线亚洲成人| 99国产精品99久久久久久粉嫩| 最新中文字幕在线播放| 中文字幕在线免费观看视频| 国产成人久久| 新版的欧美在线视频| av在线资源| 伊人久久高清| 岛国av在线播放| 中文av在线全新| 一区二区小说| 亚洲永久字幕| 亚洲免费福利一区| 久久国产乱子精品免费女| 欧美影院精品| 国产+成+人+亚洲欧洲在线| 热三久草你在线| 免费黄色成人| 综合视频一区| 国产精品宾馆| 日韩精品一区二区三区免费观影 | 中文欧美日韩| 中文字幕av一区二区三区人 | 国产91精品对白在线播放| 激情久久久久久| 美女久久网站| 国产精品对白久久久久粗| 日本精品不卡| 亚洲资源网站| 捆绑调教日本一区二区三区| 高清av不卡| 亚洲精品黄色| 97国产成人高清在线观看| 国产视频久久| 国产欧美一区二区三区国产幕精品 | 另类欧美日韩国产在线| 精品国产午夜肉伦伦影院| 亚洲欧美一区在线| 欧美啪啪一区| 亚洲激情偷拍| 精品视频在线观看网站| 日韩亚洲国产欧美| 精品深夜福利视频| 日韩精品三级| 午夜欧美精品| 国模精品一区| 天堂va欧美ⅴa亚洲va一国产| 9999国产精品| 国产精品天堂蜜av在线播放| 日韩专区一卡二卡| 亚洲欧美日韩精品一区二区| 国产亚洲精品久久久久婷婷瑜伽| 欧美影院视频| 久久婷婷久久| 综合欧美精品| 日韩欧美字幕| 丝袜美腿一区二区三区| 欧美亚洲在线日韩| 欧美日一区二区| 精品精品99| 综合激情婷婷| 中文精品视频| 国产综合精品一区| 91av亚洲| 久久久精品国产**网站| 视频国产精品| 亚洲激情国产| 精品日韩毛片| 日韩精品福利一区二区三区| 国产精品欧美三级在线观看| 亚洲综合二区| 99国产精品久久久久久久成人热 | 国产一区2区| 国产福利91精品一区二区| 国产精品第十页| 国产精成人品2018| 你懂的亚洲视频| 国产不卡精品在线| 中文字幕色婷婷在线视频| 日韩88av| 鲁大师精品99久久久| 精品国产成人| 国产精品av一区二区| 午夜在线视频观看日韩17c| 自拍自偷一区二区三区| 日本午夜精品久久久久| 国产伦精品一区二区三区千人斩| 国产极品模特精品一二| 日韩欧美中文| 中文视频一区| 美女精品网站| 欧美经典一区| 悠悠资源网久久精品| 日本vs亚洲vs韩国一区三区二区| 国产精品视频一区二区三区综合| 成人亚洲一区二区| 亚洲欧洲午夜| 精品久久在线| 老牛影视一区二区三区| 蜜桃久久久久| 欧美日韩国产免费观看| 国产精品欧美日韩一区| 婷婷综合在线| 精品视频网站| 免费人成黄页网站在线一区二区| 久久这里只有精品一区二区| 欧美日中文字幕| 国产精品一站二站| 亚洲一区二区三区久久久| 国产在线观看www| 偷拍亚洲精品| 黄色亚洲大片免费在线观看| 精品国产亚洲一区二区三区大结局 | 日韩超碰人人爽人人做人人添| 神马午夜在线视频| 青草久久视频| 午夜在线观看免费一区| 日韩欧美午夜| 精品久久不卡| 国产精品二区影院| 91精品国产自产精品男人的天堂| 美女网站视频一区| 国产精品对白| 亚洲三级国产| 亚洲一区欧美激情| 午夜欧美精品| 激情综合亚洲| 一区二区三区视频免费观看| 国产成人精品福利| 国产精品一区二区三区www| 日韩午夜电影| 999国产精品视频| а√天堂8资源中文在线| 精品理论电影在线| 久久久久观看| 精品视频网站| 亚洲黄色免费av| 久久精品国产99国产| 国产精品宾馆| 成人一二三区| 美女亚洲一区| 久久av一区| 日本久久一区| 国产欧美日韩影院| 久久国产精品美女|