0


Spring Boot Admin2 AdminServerAutoConfiguration详解

其他相关文章:

  1. Spring Boot Admin 参考指南
  2. SpringBoot Admin服务离线、不显示健康信息的问题
  3. Spring Boot Admin2 @EnableAdminServer的加载

前面已经分析了@EnableAdminServer的作用,唯一的功能是将

AdminServerMarkerConfiguration.Marker

类加载到Spring中,并且知道了

AdminServerAutoConfiguration

类的加载依赖于

AdminServerMarkerConfiguration.Marker

类。
这篇文章主要来分析AdminServerAutoConfiguration的作用,加载了哪些东西。

首先AdminServerAutoConfiguration类头部有如下几个注解:

@Configuration(proxyBeanMethods =false)@Conditional(SpringBootAdminServerEnabledCondition.class)@ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)@EnableConfigurationProperties(AdminServerProperties.class)@ImportAutoConfiguration({AdminServerInstanceWebClientConfiguration.class,AdminServerWebConfiguration.class})@AutoConfigureAfter({WebClientAutoConfiguration.class})@Lazy(false)
  • @Configuration(proxyBeanMethods = false) 表示是一个配置类,并且不被Spring 代理,如同普通类一样
  • @Conditional(SpringBootAdminServerEnabledCondition.class) 根据SpringBootAdminServerEnabledCondition做为条件判断是否加载该类,SpringBootAdminServerEnabledCondition中根据spring.boot.admin.server.enabled是否为true来处理,优先级最高
  • @ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class) 判断Spring容器中是否存在AdminServerMarkerConfiguration.Marker 的Bean
  • @EnableConfigurationProperties(AdminServerProperties.class) 启用AdminServerProperties配置文件,AdminServerProperties能够做为Bean 注入该类,其是我们常用的spring.boot.admin 相关配置
  • @ImportAutoConfiguration({ AdminServerInstanceWebClientConfiguration.class, AdminServerWebConfiguration.class }) 加载AdminServerInstanceWebClientConfiguration、AdminServerWebConfiguration配置类。AdminServerInstanceWebClientConfiguration 包括了一系列服务实例Http调用相关的Bean配置。AdminServerWebConfiguration定义了SBA2监控相关的接口Controller,以及ReactiveRestApiConfiguration和ServletRestApiConfirguation相关的配置
  • @AutoConfigureAfter({ WebClientAutoConfiguration.class }) 顾名思义,表示该类将在WebClientAutoConfiguration载入后,再加载
  • @Lazy(false) 表示不延迟加载,覆盖默认配置

再往下看,是构造器部分

privatefinalAdminServerProperties adminServerProperties;publicAdminServerAutoConfiguration(AdminServerProperties adminServerProperties){this.adminServerProperties = adminServerProperties;}

这里将AdminServerProperties配置以构造器的方式注入该类

继续看其他加载的配置

@Bean@ConditionalOnMissingBeanpublicInstanceRegistryinstanceRegistry(InstanceRepository instanceRepository,InstanceIdGenerator instanceIdGenerator){returnnewInstanceRegistry(instanceRepository, instanceIdGenerator);}

该部分创建了一个用于管理实例注册的Bean,InstanceRepository 用于提供实例的持久化,InstanceIdGenerator 用于生成实例id

@Bean@ConditionalOnMissingBeanpublicApplicationRegistryapplicationRegistry(InstanceRegistry instanceRegistry,InstanceEventPublisher instanceEventPublisher){returnnewApplicationRegistry(instanceRegistry, instanceEventPublisher);}

该部分创建一个用于管理所有已注册的应用

@Bean@ConditionalOnMissingBeanpublicInstanceIdGeneratorinstanceIdGenerator(){returnnewHashingInstanceUrlIdGenerator();}

该部分创建了一个根据实例健康URL地址来生成实例ID的Bean

@Bean@ConditionalOnMissingBeanpublicStatusUpdaterstatusUpdater(InstanceRepository instanceRepository,InstanceWebClient.Builder instanceWebClientBulder){returnnewStatusUpdater(instanceRepository, instanceWebClientBulder.build());}

该部分创建了一个用于查询更新实例状态的Bean

@Bean(initMethod ="start", destroyMethod ="stop")@ConditionalOnMissingBeanpublicStatusUpdateTriggerstatusUpdateTrigger(StatusUpdater statusUpdater,Publisher<InstanceEvent> events){StatusUpdateTrigger trigger =newStatusUpdateTrigger(statusUpdater, events);
        trigger.setInterval(this.adminServerProperties.getMonitor().getStatusInterval());
        trigger.setLifetime(this.adminServerProperties.getMonitor().getStatusLifetime());return trigger;}

StatusUpdateTrigger 会在加载的时候开启一个定时任务,用来检查所有实例的状态,检测周期和状态生存时间可以通过

spring.boot.admin.monitor.status-interval

spring.boot.admin.monitor.status-lifetime

配置。除此之外,其监听了实例变更事件,会在实例注册和实例更新的时候,触发去更新实例状态。

@Bean@ConditionalOnMissingBeanpublicEndpointDetectorendpointDetector(InstanceRepository instanceRepository,InstanceWebClient.Builder instanceWebClientBuilder){InstanceWebClient instanceWebClient = instanceWebClientBuilder.build();ChainingStrategy strategy =newChainingStrategy(newQueryIndexEndpointStrategy(instanceWebClient),newProbeEndpointsStrategy(instanceWebClient,this.adminServerProperties.getProbedEndpoints()));returnnewEndpointDetector(instanceRepository, strategy);}

EndpointDetector 用于检测实例暴露的端点

@Bean(initMethod ="start", destroyMethod ="stop")@ConditionalOnMissingBeanpublicEndpointDetectionTriggerendpointDetectionTrigger(EndpointDetector endpointDetector,Publisher<InstanceEvent> events){returnnewEndpointDetectionTrigger(endpointDetector, events);}

EndpointDetectionTrigger 实例端点检测触发器,在实例状态变更和实例注册更新的时候,触发检测

@Bean@ConditionalOnMissingBeanpublicInfoUpdaterinfoUpdater(InstanceRepository instanceRepository,InstanceWebClient.Builder instanceWebClientBuilder){returnnewInfoUpdater(instanceRepository, instanceWebClientBuilder.build());}

InfoUpdater 调用

info

接口,更新实例信息

@Bean(initMethod ="start", destroyMethod ="stop")@ConditionalOnMissingBeanpublicInfoUpdateTriggerinfoUpdateTrigger(InfoUpdater infoUpdater,Publisher<InstanceEvent> events){InfoUpdateTrigger trigger =newInfoUpdateTrigger(infoUpdater, events);
        trigger.setInterval(this.adminServerProperties.getMonitor().getInfoInterval());
        trigger.setLifetime(this.adminServerProperties.getMonitor().getInfoLifetime());return trigger;}

InfoUpdateTrigger 实例信息检查触发器,道理同

StatusUpdateTrigger 
@Bean@ConditionalOnMissingBean(InstanceEventStore.class)publicInMemoryEventStoreeventStore(){returnnewInMemoryEventStore();}

InMemoryEventStore 实例存储类,基于ConcurrentMap

@Bean(initMethod ="start", destroyMethod ="stop")@ConditionalOnMissingBean(InstanceRepository.class)publicSnapshottingInstanceRepositoryinstanceRepository(InstanceEventStore eventStore){returnnewSnapshottingInstanceRepository(eventStore);}

SnapshottingInstanceRepository 实例持久化类,用于操作InstanceEventStore

到此AdminServerAutoConfiguration自动配置类结束

标签: spring boot spring java

本文转载自: https://blog.csdn.net/weixin_40972073/article/details/127990150
版权归原作者 阿提说说 所有, 如有侵权,请联系我们删除。

“Spring Boot Admin2 AdminServerAutoConfiguration详解”的评论:

还没有评论