SpringBoot Admin 可视化监控平台
Spring Boot Admin,这是一个开源社区项目,用于管理和监控SpringBoot应用程序。这个项目中包含有客户端和服务端两部分,而监控平台指的就是服务端。我们做的程序如果需要被监控,将我们做的程序制作成客户端,然后配置服务端地址后,服务端就可以通过HTTP请求的方式从客户端获取对应的信息,并通过UI界面展示对应信息。
服务端开发
步骤①:导入springboot admin对应的starter,版本与当前使用的springboot版本保持一致,并将其配置成web工程
admin依赖的版本,尽量和springboot的version一致。以免出现不兼容的情况。
<!--我的SpringBoot主版本使用的是2.3.3.RELEASE,所以我使用2.3.x-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
步骤②:在引导类上添加注解@EnableAdminServer,声明当前应用启动后作为SpringBootAdmin的服务器使用
@SpringBootApplication
@EnableAdminServer
public class Springboot25AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot25AdminServerApplication.class, args);
}
}
做到这里,这个服务器就开发好了,启动后就可以访问当前程序了,直接输入 localhost:8080 ,界面如下 :
由于目前没有启动任何被监控的程序,所以里面什么信息都没有。下面制作一个被监控的客户端程序。
客户端开发
步骤①:导入springboot admin对应的starter,版本与当前使用SpringBoot版本保持一致,并将其配置成web工程
<!--我的SpringBoot主版本使用的是2.3.3.RELEASE,所以我使用2.3.x-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
步骤②:设置当前客户端将信息上传到哪个服务器上,通过yml文件配置
server:
port: 8088
spring:
boot:
admin:
client:
url: http://localhost:8080
做到这里,这个客户端就可以启动了。启动后再次访问服务端程序,界面如下:
可以看到,当前监控了1个程序,点击进去查看详细信息。
由于当前没有设置开放哪些信息给监控服务器,所以目前看不到什么有效的信息。下面需要做两组配置就可以看到信息了:
- 开放指定信息给服务器看
- 允许服务器以HTTP请求的方式获取对应的信息
配置如下:
server:
port: 8088
spring:
boot:
admin:
client:
url: http://localhost:8080
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
配置说明
spring boot admin的客户端默认开放了13组信息给服务器,但是这些信息除了health之外,其他的信息都不让通过HTTP请求查看。
但是即便如此,我们看到健康信息中也没什么内容,原因在于健康信息中有一些信息描述了你当前应用使用了什么技术等信息,如果无脑的对外暴露功能会有安全隐患。通过配置就可以开放所有的健康信息明细查看了。
management:
endpoint:
health:
show-details: always
健康明细信息如下:
目前除了健康信息,其他信息都查阅不了。原因在于其他12种信息是默认不提供给服务器通过HTTP请求查阅的,所以需要开启查阅的内容项,使用*表示查阅全部。记得带引号。
endpoints:
web:
exposure:
include: "*"
配置后再刷新服务器页面,就可以看到所有的信息了。
以上界面中展示的信息量就非常大了,包含了13组信息,有性能指标监控,加载的bean列表,加载的系统属性,日志的显示控制等等。
- 类加载面板中可以查阅到开发者自定义的类
- 映射中可以查阅到当前应用配置的所有请求
- 性能指标中可以查阅当前应用独有的请求路径统计数据
监控原理
通过查阅监控中的映射指标,可以看到当前系统中可以运行的所有请求路径,其中大部分路径以/actuator开头
首先这些请求路径不是开发者自己编写的,其次这个路径代表什么含义呢?既然这个路径可以访问,就可以通过浏览器发送该请求看看究竟可以得到什么信息。
通过发送请求http:localhost:8080/actuator,可以得到一组json信息,如下:
{
"_links": {
"self": {
"href": "http://localhost:81/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:81/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:81/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:81/actuator/caches",
"templated": false
},
"health": {
"href": "http://localhost:81/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:81/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:81/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:81/actuator/conditions",
"templated": false
},
"shutdown": {
"href": "http://localhost:81/actuator/shutdown",
"templated": false
},
"configprops": {
"href": "http://localhost:81/actuator/configprops",
"templated": false
},
"configprops-prefix": {
"href": "http://localhost:81/actuator/configprops/{prefix}",
"templated": true
},
"env": {
"href": "http://localhost:81/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:81/actuator/env/{toMatch}",
"templated": true
},
"loggers": {
"href": "http://localhost:81/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:81/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:81/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:81/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:81/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:81/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://localhost:81/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:81/actuator/mappings",
"templated": false
}
}
}
其中每一组数据都有一个请求路径,而在这里请求路径中有之前看到过的health,发送此请求又得到了一组信息
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 297042808832,
"free": 72284409856,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
当前信息与监控面板中的数据存在着对应关系
原来监控中显示的信息实际上是通过发送请求后得到json数据,然后展示出来。按照上述操作,可以发送更多的以/actuator开头的链接地址,获取更多的数据,这些数据汇总到一起组成了监控平台显示的所有数据。
Actuator,可以称为端点,描述了一组监控信息,SpringBootAdmin提供了多个内置端点,通过访问端点就可以获取对应的监控信息,也可以根据需要自定义端点信息。通过发送请求路劲/actuator可以访问应用所有端点信息,如果端点中还有明细信息可以发送请求/actuator/端点名称来获取详细信息。以下列出了所有端点信息说明:
management:
endpoint:
health: # 端点名称
show-details: always
info: # 端点名称
enabled: true # 是否开放
为了方便开发者快速配置端点,springboot admin设置了13个较为常用的端点作为默认开放的端点,如果需要控制默认开放的端点的开放状态,可以通过配置设置,如下:
management:
endpoints:
enabled-by-default: true # 是否开启默认端点,默认值true
上述端点开启后,就可以通过端点对应的路径查看对应的信息了。但是此时还不能通过HTTP请求查询此信息,还需要开启通过HTTP请求查询的端点名称,使用“*”可以简化配置成开放所有端点的WEB端HTTP请求权限。
management:
endpoints:
web:
exposure:
include: "*"
整体上来说,对于端点的配置有两组信息,一组是endpoints开头的,对所有端点进行配置,一组是endpoint开头的,对具体端点进行配置。
management:
endpoint: # 具体端点的配置
health:
show-details: always
info:
enabled: true
endpoints: # 全部端点的配置
web:
exposure:
include: "*"
enabled-by-default: true
自定义监控指标
INFO端点
info端点描述了当前应用的基本信息,可以通过两种形式快速配置info端点的信息
- 配置形式
在yml文件中通过设置info节点的信息就可以快速配置端点信息
info:
appName: "@project.artifactId@"
version: "@project.version@"
company: 测试项目
author: linhongwei
注意,以上要动态获取参数的值,必须在pom.xml中添加:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>@</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<!--必须添加,否则无法动态获取值-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
配置完毕后,对应信息显示在监控平台上
- 编程形式
通过配置的形式只能添加固定的数据,如果需要动态数据还可以通过配置bean的方式为info端点添加信息,此信息与配置信息共存。
@Component
public class InfoConfig implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("runTime",System.currentTimeMillis()); //添加单个信息
Map infoMap = new HashMap();
infoMap.put("buildTime","2006");
builder.withDetails(infoMap); //添加一组信息
}
}
Health端点
health端点描述当前应用的运行健康指标,即应用的运行是否成功。通过编程的形式可以扩展指标信息。
@Component
public class HealthConfig extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
boolean condition = true;
if(condition) {
builder.status(Status.UP); //设置运行状态为启动状态
builder.withDetail("runTime", System.currentTimeMillis());
Map infoMap = new HashMap();
infoMap.put("buildTime", "2006");
builder.withDetails(infoMap);
}else{
builder.status(Status.OUT_OF_SERVICE); //设置运行状态为不在服务状态
builder.withDetail("上线了吗?","你做梦");
}
}
}
Metrics端点
举例:
@Service
public class BookServiceImpl3 implements BookService3 {
@Autowired
private BookDao bookDao;
//性能指标metrics监控
private Counter counter;
public BookServiceImpl3(MeterRegistry meterRegistry){
counter = meterRegistry.counter("用户付费操作次数");
}
//增
@Override
public Boolean save(Book book) {
//每次增加都受到性能指标的监控
//假装是付费业务
counter.increment();
return bookDao.insert(book) > 0;
}
}
监控性能指标这里自动刷新
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!