使用 Spring Boot Admin 监控应用状态
程序员优雅哥 SpringBoot 2.7 实战基础 - 11 - 使用 Spring Boot Admin 监控应用状态
1 Spring Boot Actuator
Spring Boot Actuator 是 Spring Boot 提供的对应用的自省和监控功能,如健康检查,审计,指标收集,HTTP 跟踪等,可以帮助开发和运维人员监控和管理 Spring Boot 应用。该模块采集应用的内部信息,并暴露给外部的模块,支持 HTTP 和 JMX,并可以与一些第三方监控系统(如 Prometheus)整合。
1.1 Actuator endpoint
端点 Endpoint 是 Actuator 的核心组成部分,用来监视应用程序的各种状态。 Spring Boot Actuator 内置很多 Endpoint,总体上看分成三类:
- 应用配置类:主要包括配置信息、Spring Bean 的信息、配置文件信息、环境信息等;
- 度量指标类:应用在运行期间的信息,包括堆栈、健康状态、线程池信息、HTTP请求统计等;
- 操作控制类:如 shutdown,提供了对应用的关闭等操作类功能。
1.2 添加依赖
在 pom.xml 中添加 Actuator 的 starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.3 访问端点
添加依赖后,启动服务,可通过如下请求查看暴露的端点:
http://localhost:9099/actuator
该请求返回:
{
"_links": {
"self": {
"href": "http://localhost:9099/actuator",
"templated": false
},
"health-path": {
"href": "http://localhost:9099/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:9099/actuator/health",
"templated": false
}
}
}
从返回的结果可以看出默认只开放了 /actuator/health
端点。访问该端点 http://localhost:9099/actuator/health
:
{"status":"UP"}
其他未开放的端点可以独立配置开启或禁用。
在 application.yml 中添加如下配置,开放所有的端点,并显示详细的 health:
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
重启服务,再次查看暴露的端点,可以看出有如下端点:
2 Spring Boot Admin
Spring Boot Actuator 提供了各种端点,Spring Boot Admin 能够将 Actuator 中的信息进行界面化的展示,并提供实时报警功能。
在微服务环境中,使用 Spring Boot Admin,通常包括服务端和客户端,服务端只运行 Spring Boot Admin Server,收集各个客户端的数据,并以可视化界面显示出来。客户端运行 Spring Boot Admin Client,或者通过服务发现与注册获取应用的信息。
这里的 demo 我就不在 Spring Boot Admin Server了,将当前 hero-springboot-demo 既作为 server、也作为 client 使用。在后面的实战篇章中会独立 Admin Server,同时客户端也不使用 client,而是通过服务注册与发现。
2.1 添加依赖
在 pom.xml 中添加 Spring Boot Admin Server 的依赖:
<!-- 实战中该依赖只在独立的 Admin Server 中使用,此处仅为测试 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.4</version>
</dependency>
<!-- 实战中客户端也不需要添加该依赖,而是通过服务发现与注册,此处仅为测试 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.4</version>
</dependency>
需要注意版本号,由于 Spring Boot 版本使用的是 2.7.x
,Spring Boot Admin Server 的版本也要用 2.7.x
,千万别乱搞!
2.2 开启 Admin Server
在启动类 DemoApplication 上添加注解 @EnableAdminServer
开启 Spring Boot Admin Server。
@EnableAdminServer
@EnableAsync
@MapperScan("com.yygnb.demo.mapper")
@SpringBootApplication
public class DemoApplication {
...
}
2.3 配置客户端
在 application.yml 中添加如下配置:
- 配置 Admin Server 的 context-path;
- 为客户端配置 Admin Server 的地址。
spring:
application:
name: hero-springboot-demo
boot:
admin:
client:
url: 'http://localhost:9099/monitor'
context-path: '/monitor'
2.4 访问 Admin Server
重启服务,在浏览器中访问 Spring Boot Admin Server:
http://localhost:9099/monitor
可以看到当前应用的作为客户端注册到 Admin Server 上:
再次强调,上述操作仅仅针对demo学习,非真实的企业级开发!
3 自定义告警
当应用状态异常时,Spring Boot Admin 会自动实时告警,而告警的方式可以由我们自定义。这里模拟日志的方式。
在 config 包下创建类 DemoNotifier
,该类继承自 AbstractEventNotifier
:
@Slf4j
@Component
public class DemoNotifier extends AbstractEventNotifier {
protected DemoNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
return Mono.fromRunnable(() -> log.error("Instance info: {}, {}, {}",
instance.getRegistration().getName(), event.getInstance(),
event.getType()));
}
}
此时,注册到这个Admin Server的其他客户端启动、停止等,当前应用都会监听到事件,输出日志。实战中可以在这里面发送邮件、消息等。
4 登录访问
上面配置的 Admin Server 无需登录就可以访问,在真实开发中需要登录后才能访问。admin server 也提供了登录页面。
4.1 添加依赖
在 pom.xml 添加 Spring Security 的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
4.2 配置用户名密码
在 application.yml 中配置登录的用户名和密码:
spring:
application:
name: hero-springboot-demo
boot:
admin:
client:
url: 'http://localhost:9099/monitor'
context-path: '/monitor'
security:
user:
name: admin
password: 111111
上面的配置在之前的基础上增加了:spring.security.user
的配置。
4.3 添加配置类
在 config 包下添加 Spring Security 的配置类 SecurityConfig
:
@Configuration
public class SecurityConfig {
private final String adminContextPath;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler =
new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
return http.authorizeHttpRequests(auth -> auth.antMatchers(
adminContextPath + "/assets/**",
adminContextPath + "/login",
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
).permitAll()
.antMatchers(adminContextPath + "/**").authenticated()
.anyRequest().permitAll()
).formLogin(form -> form.loginPage(adminContextPath + "/login")
.successHandler(successHandler)
).logout(logout -> logout.logoutUrl(adminContextPath + "/logout"))
.csrf(AbstractHttpConfigurer::disable)
.build();
}
}
上面配置文件中的 adminContextPath 就是前面配置的 spring.boot.admin.context-path
,即 /monitor
。
上面配置包括几个部分:
- 仅对路径 /monitor/** 请求权限控制;
- 登录页面和登录成功后的默认地址;
- 表单登录配置;
- 禁用 CSRF。
4.4 测试运行
重启服务,访问之前开发的 computer 等接口,可以正常访问;如果访问 /monitor 等路径,就会跳转 Spring Boot Admin 提供的登录页:
使用配置的用户名密码(admin/111111)登录,登录成功后进入 Admin Server 页面。
感谢你阅读本文,如果本文给了你一点点帮助或者启发,还请三连支持一下,点赞、关注、收藏,作者会持续与大家分享更多干货
使用 Spring Boot Admin 监控应用状态的更多相关文章
- Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用
Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...
- Springboot监控之二:Spring Boot Admin对Springboot服务进行监控
概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...
- Eureka心跳健康检查机制和Spring boot admin 节点状态一直为DOWN的排查(忽略某一个节点的健康检查)
https://www.jdon.com/springcloud/eureka-health-monitoring.html 运行阶段执行健康检查的目的是为了从Eureka服务器注册表中识别并删除不可 ...
- spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin
参考:Spring Boot Admin 2.0 上手 Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提 ...
- Spring Cloud第十三篇 | Spring Boot Admin服务监控
本文是Spring Cloud专栏的第十三篇文章,了解前十二篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- 转载-Spring Boot应用监控实战
概述 之前讲过Docker容器的可视化监控,即监控容器的运行情况,包括 CPU使用率.内存占用.网络状况以及磁盘空间等等一系列信息.同样利用SpringBoot作为微服务单元的实例化技术选型时,我们不 ...
- Spring Boot Admin,贼好使!
Spring Boot Admin(SBA)是一个开源的社区项目,用于管理和监控 Spring Boot 应用程序.应用程序可以通过 http 的方式,或 Spring Cloud 服务发现机制注册到 ...
- Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...
随机推荐
- 基于thinkphp6 layui的优秀极速后台开发框架推荐
很多时候我们在做项目开发的时候,苦于没有好一点的轮子,自己动手开发的话,太耗费时间了,如果采用VUE的话,学习成本跟调试也比较麻烦, 而且有时候选用的东西甲方也不太容易接受,现在给大家介绍一款优秀的极 ...
- Sentiment analysis in nlp
Sentiment analysis in nlp The goal of the program is to analysis the article title is Sarcasm or not ...
- Tapdata Cloud 2.1.4 来啦:数据连接又上新,PolarDB MySQL、轻流开始接入,可自动标记不支持的字段类型
需求持续更新,优化一刻不停--Tapdata Cloud 2.1.4 来啦! 最新发布的版本中,在新增数据连接之余,默认标记不支持同步的字段类型,避免因此影响任务的正常运行. 更新速览 ① 数 ...
- SELECT 的6大子句
SELECT 6大子句的顺序: SELECT selection_list /*要查询的列名称*/, 结果的字段列表 FROM table_list /*要查询的表名称*/, 后面跟表,视图,多行多列 ...
- Python迷宫生成器
作为一项古老的智力游戏,千百年来迷宫都散发着迷人的魅力.但是,手工设计迷宫费时又耗(脑)力,于是,我们有必要制作一个程序:迷宫生成器-- 好吧,我编不下去了.但是,从上面的文字中,我们可以看出,我们此 ...
- 数据质量管理工具预研——Griffin VS Deequ VS Great expectations VS Qualitis
开源数据质量管理工具预研--Griffin VS Deequ VS Great expectations VS Qualitis. 概述 数据质量监控(DQC)是最近很火的一个话题,也是数据治理中 ...
- Solution -「Luogu 3959」 宝藏
果真是宝藏题目. 0x01 前置芝士 这道题我是真没往状压dp上去想.题目来源. 大概看了一下结构.盲猜直接模拟退火!\xyx 所需知识点:模拟退火,贪心. 0x02 分析 题目大意:给你一个图,可能 ...
- 开发中常用的两个JSON方法
参考文章:https://juejin.cn/post/6844903711127404557 在前端开发过程中,有两个非常有用的方法来处理 JSON 格式的内容: JSON.parse(string ...
- python opencv图像识别(相同大小图片)
简介 由于项目需要对比两张相同图片的相似度,因此采用opencv将图片转为灰阶数组,然后对比相应的数组来取相似度,此方法只适用于大小相同的图片,较为局限 # -*- coding: utf-8 -*- ...
- Linux学习系列--用户(组)新增、查看和删除
在实际的工作中,在接触Linux的用户组管理的时候,一般来说都是在系统开建设的时候设置好,root权限由特定的负责人保管用户密码,避免误操作带来不必要的麻烦. 在具体使用的时候,会利用相关的命令设置一 ...