其他相关文章:
- Spring Boot Admin 参考指南
- SpringBoot Admin服务离线、不显示健康信息的问题
- Spring Boot Admin2 @EnableAdminServer的加载
前面已经分析了@EnableAdminServer的作用,唯一的功能是将AdminServerMarkerConfiguration.Marker
类加载到Spring中,并且知道了AdminServerAutoConfiguration
类的加载依赖于AdminServerMarkerConfiguration.Marker
类。
这篇文章主要来分析AdminServerAutoConfiguration的作用,加载了哪些东西。
首先AdminServerAutoConfiguration类头部有如下几个注解:
代码语言:javascript复制@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) 表示不延迟加载,覆盖默认配置
再往下看,是构造器部分
代码语言:javascript复制 private final AdminServerProperties adminServerProperties;
public AdminServerAutoConfiguration(AdminServerProperties adminServerProperties) {
this.adminServerProperties = adminServerProperties;
}
这里将AdminServerProperties配置以构造器的方式注入该类
继续看其他加载的配置
代码语言:javascript复制 @Bean
@ConditionalOnMissingBean
public InstanceRegistry instanceRegistry(InstanceRepository instanceRepository,
InstanceIdGenerator instanceIdGenerator) {
return new InstanceRegistry(instanceRepository, instanceIdGenerator);
}
该部分创建了一个用于管理实例注册的Bean,InstanceRepository 用于提供实例的持久化,InstanceIdGenerator 用于生成实例id
代码语言:javascript复制 @Bean
@ConditionalOnMissingBean
public ApplicationRegistry applicationRegistry(InstanceRegistry instanceRegistry,
InstanceEventPublisher instanceEventPublisher) {
return new ApplicationRegistry(instanceRegistry, instanceEventPublisher);
}
该部分创建一个用于管理所有已注册的应用
代码语言:javascript复制 @Bean
@ConditionalOnMissingBean
public InstanceIdGenerator instanceIdGenerator() {
return new HashingInstanceUrlIdGenerator();
}
该部分创建了一个根据实例健康URL地址来生成实例ID的Bean
代码语言:javascript复制 @Bean
@ConditionalOnMissingBean
public StatusUpdater statusUpdater(InstanceRepository instanceRepository,
InstanceWebClient.Builder instanceWebClientBulder) {
return new StatusUpdater(instanceRepository, instanceWebClientBulder.build());
}
该部分创建了一个用于查询更新实例状态的Bean
代码语言:javascript复制 @Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean
public StatusUpdateTrigger statusUpdateTrigger(StatusUpdater statusUpdater, Publisher<InstanceEvent> events) {
StatusUpdateTrigger trigger = new StatusUpdateTrigger(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
@ConditionalOnMissingBean
public EndpointDetector endpointDetector(InstanceRepository instanceRepository,
InstanceWebClient.Builder instanceWebClientBuilder) {
InstanceWebClient instanceWebClient = instanceWebClientBuilder.build();
ChainingStrategy strategy = new ChainingStrategy(new QueryIndexEndpointStrategy(instanceWebClient),
new ProbeEndpointsStrategy(instanceWebClient, this.adminServerProperties.getProbedEndpoints()));
return new EndpointDetector(instanceRepository, strategy);
}
EndpointDetector 用于检测实例暴露的端点
代码语言:javascript复制 @Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean
public EndpointDetectionTrigger endpointDetectionTrigger(EndpointDetector endpointDetector,
Publisher<InstanceEvent> events) {
return new EndpointDetectionTrigger(endpointDetector, events);
}
EndpointDetectionTrigger 实例端点检测触发器,在实例状态变更和实例注册更新的时候,触发检测
代码语言:javascript复制 @Bean
@ConditionalOnMissingBean
public InfoUpdater infoUpdater(InstanceRepository instanceRepository,
InstanceWebClient.Builder instanceWebClientBuilder) {
return new InfoUpdater(instanceRepository, instanceWebClientBuilder.build());
}
InfoUpdater 调用info
接口,更新实例信息
@Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean
public InfoUpdateTrigger infoUpdateTrigger(InfoUpdater infoUpdater, Publisher<InstanceEvent> events) {
InfoUpdateTrigger trigger = new InfoUpdateTrigger(infoUpdater, events);
trigger.setInterval(this.adminServerProperties.getMonitor().getInfoInterval());
trigger.setLifetime(this.adminServerProperties.getMonitor().getInfoLifetime());
return trigger;
}
InfoUpdateTrigger 实例信息检查触发器,道理同StatusUpdateTrigger
@Bean
@ConditionalOnMissingBean(InstanceEventStore.class)
public InMemoryEventStore eventStore() {
return new InMemoryEventStore();
}
InMemoryEventStore 实例存储类,基于ConcurrentMap
代码语言:javascript复制 @Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean(InstanceRepository.class)
public SnapshottingInstanceRepository instanceRepository(InstanceEventStore eventStore) {
return new SnapshottingInstanceRepository(eventStore);
}
SnapshottingInstanceRepository 实例持久化类,用于操作InstanceEventStore
到此AdminServerAutoConfiguration自动配置类结束