Spring Boot -- Spring Boot之监控中心
一、监控管理
首先我们来了解一下监控中心是什么?
针对微服务的服务状态包括http请求资源、服务器内存变化(堆、内存、线程、日志管理等)、检测服务配置连接地址是否可用(模拟访问,懒加载情况下)、统计现在有多少个bean(是spring容器中的bean)、统计Spring MVC的@ResultMapping(统计http接口)。
Spring Boot提供了两种监控中心:
-
Actuator监控应用:没有界面,返回json格式;
-
AdminUI:底层使用的就是Actuator监控应用,实现可视化的界面;
1.1、Actuator监控应用
Actuator是Spring Boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用http的各种请求来监管、审计、收集应用的运行情况,特别对于微服务管理十分有意义。
1.2、新建Actuator项目
新建一个项目spring-boot-actuator,添加依赖:
<!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(各种引来信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
新建包com.zy.example.controller,在包下创建IndexController.java:
package com.zy.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 控制器 * * @author zy * @since 2020-2-7 */ @RestController public class IndexController { @RequestMapping("/index") public String index(){ return "index"; } }
在src/java/resources下新建application.yml文件:
#通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的;
management:
endpoints:
web:
exposure:
include: "*"
运行程序,访问http://localhost:8080/actuator/mappings可以查看当前支持哪些API请求:
根据端点的作用,可以将spring-boot-starter-actuator模块中原生端点分为三大类:
- 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息;
- 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如内存信息、线程池信息、HTTP请求统计等;
- 操作控制类:提供了对应用的关闭等操作类功能;
我们介绍几个常见的原生端点,通过actuator/+端点名就可以获取相应的信息:
路径 |
作用 |
/actuator/beans |
显示应用程序中所有Spring bean的完整列表。 |
/actuator/configprops |
显示所有配置信息。 |
/actuator/env |
陈列所有的环境变量。 |
/actuator/mappings |
显示所有@RequestMapping的url整理列表。 |
/actuator/health |
显示应用程序运行状况信息 up表示成功 down失败 |
/actuator/info |
查看自定义应用信息 |
更多相关信息可以参考博客:SpringBoot actuator 应用监控。
1.3、新建admin-ui-server项目
新建一个项目spring-boot-admin-ui-server,添加依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zy.example</groupId> <artifactId>spring-boot-admin-ui-server</artifactId> <version>1.0-SNAPSHOT</version> <!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(各种引来信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 支持admin-ui的关键配置 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
新建application.yml文件,配置端口号和服务名称:
server:
port: 8090
spring:
application:
name: admin-ui-server
新增启动类,在包com.zy.example下新建App.java文件,必须添加@EnableAdminServer注解:
package com.zy.example; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 启动类 * * @author zy * @since 2020-2-8 */ @SpringBootApplication @EnableAdminServer public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
启动springboot-admin-ui-server,浏览器中打开监控中心的UI管理页面:http://localhost:8090/applications:
1.4、新建admin-ui-client项目
新建一个项目spring-boot-admin-ui-client,添加依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zy.example</groupId> <artifactId>springboot-admin-ui-client</artifactId> <version>1.0-SNAPSHOT</version> <!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(各种引来信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 支持admin-ui的关键配置 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
亲爱的读者和支持者们,自动博客加入了打赏功能,陆陆续续收到了各位老铁的打赏。在此,我想由衷地感谢每一位对我们博客的支持和打赏。你们的慷慨与支持,是我们前行的动力与源泉。
日期 | 姓名 | 金额 |
---|---|---|
2023-09-06 | *源 | 19 |
2023-09-11 | *朝科 | 88 |
2023-09-21 | *号 | 5 |
2023-09-16 | *真 | 60 |
2023-10-26 | *通 | 9.9 |
2023-11-04 | *慎 | 0.66 |
2023-11-24 | *恩 | 0.01 |
2023-12-30 | I*B | 1 |
2024-01-28 | *兴 | 20 |
2024-02-01 | QYing | 20 |
2024-02-11 | *督 | 6 |
2024-02-18 | 一*x | 1 |
2024-02-20 | c*l | 18.88 |
2024-01-01 | *I | 5 |
2024-04-08 | *程 | 150 |
2024-04-18 | *超 | 20 |
2024-04-26 | .*V | 30 |
2024-05-08 | D*W | 5 |
2024-05-29 | *辉 | 20 |
2024-05-30 | *雄 | 10 |
2024-06-08 | *: | 10 |
2024-06-23 | 小狮子 | 666 |
2024-06-28 | *s | 6.66 |
2024-06-29 | *炼 | 1 |
2024-06-30 | *! | 1 |
2024-07-08 | *方 | 20 |
2024-07-18 | A*1 | 6.66 |
2024-07-31 | *北 | 12 |
2024-08-13 | *基 | 1 |
2024-08-23 | n*s | 2 |
2024-09-02 | *源 | 50 |
2024-09-04 | *J | 2 |
2024-09-06 | *强 | 8.8 |
2024-09-09 | *波 | 1 |
2024-09-10 | *口 | 1 |
2024-09-10 | *波 | 1 |
2024-09-12 | *波 | 10 |
2024-09-18 | *明 | 1.68 |
2024-09-26 | B*h | 10 |
2024-09-30 | 岁 | 10 |
2024-10-02 | M*i | 1 |
2024-10-14 | *朋 | 10 |
2024-10-22 | *海 | 10 |
2024-10-23 | *南 | 10 |
2024-10-26 | *节 | 6.66 |
2024-10-27 | *o | 5 |
2024-10-28 | W*F | 6.66 |
2024-10-29 | R*n | 6.66 |
2024-11-02 | *球 | 6 |
2024-11-021 | *鑫 | 6.66 |
2024-11-25 | *沙 | 5 |
2024-11-29 | C*n | 2.88 |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了