1.简介
1.1 概述
Spring Boot Admin is a community project to manage and monitor your Spring Boot ® applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul). The UI is just a Vue.js application on top of the Spring Boot Actuator endpoints.
Spring Boot Admin是一个社区项目,用于管理和监视Spring Boot® 应用程序。这些应用程序在我们的Spring Boot Admin Client中注册(通过HTTP),或者使用Spring Cloud®(例如,Eureka,Consul)发现。 UI只是Spring Boot Actuator端点之上的 Vue.js 应用程序。
1.2 特点
- Show health status:显示健康状况
- Show details, like:显示详细信息,例如
- JVM & memory metrics:JVM 和 内存指标
- micrometer.io metrics:micrometer io指标
- Datasource metrics:数据源指标
- Cache metrics:缓存指标
- Show build-info number:显示构建信息编号
- Follow and download logfile:追踪并下载日志文件
- View jvm system- & environment-properties:查看 jvm 系统属性和环境属性
- View Spring Boot Configuration Properties:查看 Spring Boot 配置属性
- Support for Spring Cloud’s postable /env- &/refresh-endpoint:支持Spring Cloud的可发布/ env-和// refresh-endpoint
- Easy loglevel management:轻松的日志级别管理
- Interact with JMX-beans:与JMX-beans交互
- View thread dump:查看线程转储
- View http-traces:查看http跟踪
- View auditevents:查看审计事件
- View http-endpoints:查看http端点
- View scheduled tasks:查看定时任务
- View and delete active sessions (using spring-session):查看和删除活动会话(使用spring-session)
- View Flyway / Liquibase database migrations:查看Flyway / Liquibase数据库迁移
- Download heapdump:下载 heapdump
- Notification on status change (via e-mail, Slack, Hipchat, …):状态更改通知(通过电子邮件,Slack,Hipchat等)
- Event journal of status changes (non persistent):状态更改的事件日志(非持久性)
2.演示环境
- JDK 1.8.0_201
- Spring Boot 2.2.0.RELEASE
- 构建工具(apache maven 3.6.3)
- 开发工具(IntelliJ IDEA )
3.演示代码
3.1 代码说明
使用 spring boot admin 需要两个项目:一个是 admin 项目,一个是 client 项目。admin 项目开启 SpringBoot Admin 功能,client 项目注册到 admin 项目上。
3.2 代码结构
3.3 maven 依赖
spring-boot-admin --> pom.xml
<modules>
<module>spring-boot-admin-server</module>
<module>spring-boot-admin-client</module>
</modules>
spring-boot-admin-server --> pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
</dependencies>
spring-boot-admin-client --> pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
</dependencies>
3.4 配置文件
spring-boot-admin-server --> application.properties
spring.application.name=spring-boot-admin-server
server.port=8080
spring-boot-admin-client --> application.properties
spring.application.name=spring-boot-admin-client
server.port=9090
# 指定admin-server地址
spring.boot.admin.client.url=http://localhost:8080
# 监控所有的endpoint
management.endpoints.web.exposure.include=*
3.5 java代码
SpringBootAdminServerApplication.java
@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServerApplication.class, args);
}
}
SpringBootAdminClientApplication.java
@SpringBootApplication
public class SpringBootAdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminClientApplication.class, args);
}
}
3.6 git 地址
spring-boot/spring-boot-05-basis/spring-boot-admin
4.效果展示
先启动 SpringBootAdminServerApplication.main 方法,再启动 SpringBootAdminClientApplication.main 方法。
访问 http://localhost:8080/,会自动跳转到 Spring Boot Admin 界面,可以看到 spring-boot-admin-client 已经注册上来了
点击 spring-boot-admin-client,可以查看各项监控指标
5.源码分析
5.1 admin server 的请求原理
Spring Boot Admin 组件中,通过 @EnableAdminServer
标识一个 admin server 组件,并启动。EnableAdminServer 的内容如下.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AdminServerMarkerConfiguration.class)
public @interface EnableAdminServer {
}
EnableAdminServer 通过 @Import 导入一个 AdminServerMarkerConfiguration 配置类。
@Configuration(proxyBeanMethods = false)
public class AdminServerMarkerConfiguration {
@Bean
public Marker adminServerMarker() {
return new Marker();
}
public static class Marker {
}
}
AdminServerMarkerConfiguration 中定义了一个静态内部类 Marker,并将它声明为 Bean。那么这个 admin server 又是如何生效的呢?实际上,admin server 有一个自动装配类 AdminServerAutoConfiguration,这个类的内容如下。
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(AdminServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties(AdminServerProperties.class)
@ImportAutoConfiguration({
AdminServerInstanceWebClientConfiguration.class, AdminServerWebConfiguration.class })
@AutoConfigureAfter({
WebClientAutoConfiguration.class })
public class AdminServerAutoConfiguration {
private final AdminServerProperties adminServerProperties;
public AdminServerAutoConfiguration(AdminServerProperties adminServerProperties) {
this.adminServerProperties = adminServerProperties;
}
@Bean
@ConditionalOnMissingBean
public InstanceRegistry instanceRegistry(InstanceRepository instanceRepository,
InstanceIdGenerator instanceIdGenerator) {
return new InstanceRegistry(instanceRepository, instanceIdGenerator);
}
@Bean
@ConditionalOnMissingBean
public ApplicationRegistry applicationRegistry(InstanceRegistry instanceRegistry,
InstanceEventPublisher instanceEventPublisher) {
return new ApplicationRegistry(instanceRegistry, instanceEventPublisher);
}
@Bean
@ConditionalOnMissingBean
public InstanceIdGenerator instanceIdGenerator() {
return new HashingInstanceUrlIdGenerator();
}
@Bean
@ConditionalOnMissingBean
public StatusUpdater statusUpdater(InstanceRepository instanceRepository,
InstanceWebClient.Builder instanceWebClientBulder) {
return new StatusUpdater(instanceRepository, instanceWebClientBulder.build());
}
@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;
}
@Bean
@ConditionalOnMissingBean
public EndpointDetector endpointDetector(InstanceRepository instanceRepository,
InstanceWebClient.Builder instanceWebClientBuilder) {
InstanceWebClient instanceWebClient = instanceWebClientBuilder.bu