1、简介

干嘛的:
主要运用在微服务架构,所以我建议你先学微服务,否则可能get不到它的用处,只有大型的分布式系统才会用到指标监控… Why:?

对于一个大型的几十个、几百个微服务构成的微服务架构系统,在线上时通常会遇到下面一些问题,比如:

  1. 如何知道哪些服务除了问题,如何快速定位? (健康状况)
  2. 如何统一监控各个微服务的性能指标(内存、jvm、并发数、线程池、Http 请求统计)
  3. 如何统一管理各个微服务的日志?(切换线上日志等级,快速搜索日志…)
  4. 如何优雅管理服务下线(正在运行的线程不发生中断)

So: 在这种大型分布式应用的环境下,我们如何能够快速发现问题、快速解决问题, 必须要有监控平台、(链路追踪、日志)

2、SpringBoot Actuator

介绍:
SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变 量、日志信息、线程信息等
实现:

1 <dependency> 
2 <groupId>org.springframework.boot</groupId> 
3 <artifactId>spring‐boot‐starter‐actuator</artifactId> 
4 </dependency>


引入场景
访问 http://localhost:8080/actuator/**
暴露所有监控信息为HTTP

1 management: 
2 endpoints: 
3 enabled‐by‐default: true # 默认开启所有监控端点4 web: 
5 exposure: 
6 include: '*' # 以web方式暴露所有端点 
7 

测试
http://localhost:8080/actuator/beans http://localhost:8080/actuator/configprops http://localhost:8080/actuator/metrics http://localhost:8080/actuator/metrics/jvm.gc.pause http://localhost:8080/actuator/endpointName/detailPath 。。。。。。
Actuator Endpoint

常用的端点

Health:监控状况
一个组件指标状态为Down则总状态信息Down,很多组件中都定制了Health子节点指标: 比如jdbc、redis、等等
shutdown:优雅关闭
注意需要web服务器的支持

1 server: 
2 shutdown: graceful

Metrics:运行时指标

Loggers:日志记录

1 logging: 
2 file: 
3 name: D:/logs/xushu.log

3、Spring Boot Admin

可视化监控平台, 是一个基于 Spring Boot Actuator 端点之上的 Vue.js 应用程序。
https://github/codecentric/spring-boot-admin


GitHub官方地址:https://github/codecentric/spring-boot-admin

3.1、使用:

1、设置Spring Boot Admin Server
创建服务器并引入依赖,如一个springboot项目
版本建议: Spring Boot 2.x=Spring Boot Admin 2.x (比如Spring Boot 2.3.x 可以用Spring Boot Admin 2.3.x)

1 <dependency> 
2 <groupId>de.codecentric</groupId> 
3 <artifactId>spring‐boot‐admin‐starter‐server</artifactId> 
4 <version>2.4.0‐SNAPSHOT</version> 
5 </dependency> 
6 <dependency> 
7 <groupId>org.springframework.boot</groupId> 
8 <artifactId>spring‐boot‐starter‐web</artifactId> 
9 </dependency> 

通过添加@EnableAdminServer到配置中来引入Spring Boot Admin Server配置:

1 @SpringBootApplication 
2 @EnableAdminServer 
3 public class Boot05AdminserverApplication { 
4 public static void main(String[] args) { 
5 SpringApplication.run(Boot05AdminserverApplication.class, args); 
6 } 
7
8 } 

注册客户端应用程序并引入依赖,如一个springboot项目

1 <dependency> 
2 <groupId>de.codecentric</groupId> 
3 <artifactId>spring‐boot‐admin‐starter‐client</artifactId> 
4 <version>2.3.1</version> 
5 </dependency>

配置Spring Boot Admin Server的URL,客户端连接Spring Boot Admin Server的地址

1 spring.boot.admin.client.url=http://localhost:8080 
2 management.endpoints.web.exposure.include=*

若连接不上,可能是地址使用计算机名称作为地址,可以改变使用ip注册

1 spring: 
2 boot: 
3 admin: 
4 client: 
5 url: http://localhost:8080 
6 instance: 
7 prefer‐ip: true # 使用ip注册进来 
8 application: 
9 name: boot‐05‐web‐admin # 客户端名称 

访问 服务器的根目录如http://localhost:8080/ 即可浏览

3.2、通过注册中心集成客户端

如果你有成百上千个微服务, 这样配置未免太麻烦。如果您已经为您的应用程序使用了 Spring Cloud (Alibaba- nacos) Discovery,那么您就不需要 SBA 客户端。只需在 Spring Boot Admin Server 中添加一个 DiscoveryClient,剩下的工作由我们的 AutoConfiguration 完成。
下面的步骤使用 Nacos,但是也支持其他 Spring Discovery实现。

SBA服务端:

  1. 添加依赖
1 <!‐‐nacos‐服务注册发现‐‐> 
2 <dependency> 
3 <groupId>com.alibaba.cloud</groupId> 
4 <artifactId>spring‐cloud‐starter‐alibaba‐nacos‐discovery</artifactId> 
5 </dependency> 
  1. 配置Nacos
1 spring: 
2 cloud:3 nacos: 
4 discovery: 
5 server‐addr: 127.0.0.1:8848 
6 username: nacos 
7 password: nacos 
8 application: 
9 name: spring‐boot‐admin‐server 
1 # 也会将SBA服务配置为客户端, 所以也可以配置自己的endpoint规则(可选) 
2 management: 
3 endpoints: 
4 web: 
5 exposure: 
6 include: '*' 
7 endpoint: 
8 health: 
9 show‐details: always 

SBA客户端:

  1. 依赖
    a. 只需添加actuator即可
1
2 <dependency> 
3 <groupId>org.springframework.boot</groupId> 
4 <artifactId>spring‐boot‐starter‐actuator</artifactId> 
5 </dependency>
  1. 配置
    a. 如需公开更多端点:(不配置该选项则只显示(health和info端点)
1 management: 
2 endpoints: 
3 web: 
4 exposure: 
5 include: '*' 

今天就分享到这里了,文章后续及更多java学习资料,关注我,免费领取

更多推荐

Spring Boot Actuator 指标监控