当前位置:网站首页>Boot指标监测
Boot指标监测
2022-06-26 18:39:00 【51CTO】
1、SpringBoot Actuator
1、简介
- 未来每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等。
- SpringBoot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控、审计等功能。
2、1.x与2.x的不同
3、如何使用
- 引入场景
- 访问 http://localhost:8080/actuator/ 可以查看各种指标
- 记录了各种 http://localhost:8080/actuator/xxx xxx为监控端点
- springboot提供了很多的监控端点---->查看官方文档
- 所有的监控端点都是默认开启的,但并不是全部暴露的
- 很多的监控端点 JMX方式暴露,但没有以WEB的方式暴露
- 具体什么端点以啥方式默认暴露---->看官方文档
- 怎么以JMX、WEB方式暴露端点??
- 暴露监控端点的配置文件设置
- 测试各种监控端点
- http://localhost:8080/actuator/beans 当前容器中的组件
- http://localhost:8080/actuator/conditions生效|不生效 的自动配置(当前的环境)
- http://localhost:8080/actuator/configprops 当前应用配置的属性
- http://localhost:8080/actuator/env 环境变量信息
- http://localhost:8080/actuator/metrics 指标信息
- http://localhost:8080/actuator/metrics/jvm.gc.pause 具体某一项的指标
- http://localhost:8080/actuator/端点名/详细信息路径
- 。。。。。。
4、可视化
- 整合可视化平台---->方面查看指标的监控
- 免费的一个开源项目: https://github.com/codecentric/spring-boot-admin
- 该项目可以指标监控和安全监控 --只使用它的指标监控功能
- 详细使用步骤:可看上面地址的gethub介绍
- 使用步骤:
- 1、创建一个 监控服务器 (相当于一个项目)
- 在pom文件中引入 jar包
<!--指标监控的-->
< dependency >
< groupId >de.codecentric </ groupId >
< artifactId >spring-boot-admin-starter-server </ artifactId >
< version >2.3.1 </ version >
</ dependency >
<!-web的开发常见-->
< dependency >
< groupId >org.springframework.boot </ groupId >
< artifactId >spring-boot-starter-web </ artifactId >
</ dependency >
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 在启动入口类上添加:@EnableAdminServer 注解
- 修改Tomcat端口,不要项目(微服务)的端口冲突
- 2、注册客户端,把每一个微服务(项目)添加到监控服务器
- 引入jar包(指标监控的(安全的使用到在引入))
<!--指标监控的 -->
< 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-security </ artifactId >
</ dependency >
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 在微服务(项目)中配置监控服务器的端口位置 并且 公开所有端点
- spring.boot.admin.client.url=http://localhost:8080 (监控服务器位置)
- management.endpoints.web.exposure.include=*
- spring.boot.admin.client.instance.prefer=true(使用ip方式注入监控器)
- spring.application.name=当前项目的名称
- 其实该监控服务器还是调用的是项目的底层的 http://localhost:8080/actuator ,我们自己也可以写一个前端监控页面
2、Actuator Endpoint (监控端点)
1、最常使用的端点
ID | 描述 |
| 暴露当前应用程序的审核事件信息。需要一个 |
| 显示应用程序中所有Spring Bean的完整列表。 |
| 暴露可用的缓存。 |
| 显示自动配置的所有条件信息,包括匹配或不匹配的原因。 |
| 显示所有 |
| 暴露Spring的属性 |
| 显示已应用的所有Flyway数据库迁移。 需要一个或多个 |
| 显示应用程序运行状况信息。 |
| 显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个 |
| 显示应用程序信息。 |
| 显示Spring |
| 显示和修改应用程序中日志的配置。 |
| 显示已应用的所有Liquibase数据库迁移。需要一个或多个 |
| 显示当前应用程序的“指标”信息。 |
| 显示所有 |
| 显示应用程序中的计划任务。 |
| 允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序。 |
| 使应用程序正常关闭。默认禁用。 |
| 显示由 |
| 执行线程转储。 |
如果您的应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点:
ID | 描述 |
| 返回 |
| 通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖 |
| 返回日志文件的内容(如果已设置 |
| 以Prometheus服务器可以抓取的格式公开指标。需要依赖 |
最常用的Endpoint
- Health:健康状况
- Metrics:运行时指标
- Loggers:日志记录
2、Health Endpoint (健康检查端点)
健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要Health Endpoint可以为平台返回当前应用的一系列组件健康状况的集合。检查当前程序的运行状况
重要的几点:
- 默认只显示一个当前应用 总的状态
- 开启 Health监控端点的 详细显示
- health endpoint返回的结果,应该是一系列健康检查后的一个汇总报告(数据库健康、redis健康等)
- 很多的健康检查默认已经自动配置好了,比如:数据库、redis等
- 可以很容易的添加自定义的健康检查机制
3、Metrics Endpoint (运行时指标端点)
提供详细的、层级的、空间指标信息,这些信息可以被pull(主动推送)或者push(被动获取)方式得到;
- 通过Metrics对接多种监控系统
- 简化核心Metrics开发
- 添加自定义Metrics或者扩展已有Metrics
- 各种的Metrics 指标见下图
- http://localhost:8080/actuator/metrics/具体指标 具体某一项的指标
4、管理监控端点
1、开启与禁用 监控端点
- 默认所有的Endpoint除过shutdown都是开启的。
- 需要开启或者禁用某个Endpoint。
- 配置模式为management.endpoint.端点名称.enabled = true
- 禁用或者开启所有的Endpoint
- management.endpoints.enabled-by-default = true
2、暴露 监控端点
支持的暴露方式
- HTTP:默认只暴露health和info Endpoint
- JMX:默认暴露所有Endpoint
- JMX的方式是java提供的底层的数据监控
- DOS命令:jconsole 就可以看到当前java运行的具体情况
- 除过health和info,剩下的Endpoint都应该进行保护访问。如果引入SpringSecurity,则会默认配置安全访问规则
ID | JMX | Web |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | Yes |
| N/A | No |
| Yes | No |
| Yes | Yes |
| Yes | No |
| N/A | No |
| N/A | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| N/A | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
| Yes | No |
3、定制 Endpoint
1、定制 某个组件的 健康信息
- 自定义的状态检查信息
- 实现 HealthIndicator 接口或者继承 AbstractHealthIndicator 抽象类
- 对于实现接口的 实现health方法
- 在该方法里面判断 组件什么情况 健康/不健康
- Health.up(); 健康
- Health.down();不健康
- 对于继承AbstractHealthIndicator抽象类的 实现分类中的doHealthCheck方法
- 在该方法里面判断 组件什么情况 健康/不健康
- builder.up(); 健康
- builder.budn();不健康
- builder.status(Status.状态);返回详细的状态
- 设置具体显示的详细信息
- Health.down() .withDetail("key1", "value1") .withDetail("key2", "value2")......;
//实现接口的
public class MyHealthIndicator implements HealthIndicator {
public Health health() {
//在该方法里面判断 组件什么情况 健康/不健康
int errorCode = check(); // perform some specific health check
if ( errorCode != 0) {
return Health. down(). withDetail( "Error Code", errorCode). build();
}
return Health. up(). build();
}
}
//构建Health,返回的具体的向详细信息
Health build = Health. down()
. withDetail( "msg", "error service")
. withDetail( "code", "500")
. withException( new RuntimeException())
. build();
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
//继承AbstractHealthIndicator抽象类的
public class MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @param builder
* @throws Exception
*/
protected void doHealthCheck( Health. Builder builder) throws Exception {
//在该方法里面判断 组件什么情况 健康/不健康
//mongodb。 获取连接进行测试
Map < String, Object > map = new HashMap <>();
// 检查完成
if( 1 == 2){
// builder.up(); //健康
builder. status( Status. UP);
map. put( "count", 1);
map. put( "ms", 100);
} else {
// builder.down();
builder. status( Status. OUT_OF_SERVICE);
map. put( "err", "连接超时");
map. put( "ms", 3000);
}
builder. withDetail( "code", 100)
. withDetails( map);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
2、定制info信息(定制当前应用的详细信息)
- 默认定制信息里面什么都没有
常用两种定制方式:
1、编写配置文件
info
appName boot-admin #当前应用的名字
version 2.0.1 #当前应用的版本
mavenProjectName @[email protected] #使用@@可以获取maven的pom文件值
mavenProjectVersion @[email protected]
- 1.
- 2.
- 3.
- 4.
- 5.
2、编写InfoContributor
- 编写配置类实现InfoContributor接口中的contribute方法
- 在contribute方法编写提示信息
- builder .withDetail("key1", "value1") .withDetail("key2", "value2")......;
import java. util. Collections;
import org. springframework. boot. actuate. info. Info;
import org. springframework. boot. actuate. info. InfoContributor;
import org. springframework. stereotype. Component;
public class ExampleInfoContributor implements InfoContributor {
public void contribute( Info. Builder builder) {
builder. withDetail( "example",
Collections. singletonMap( "key", "value"));
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
访问 http://localhost:8080/actuator/info 会输出以上方式返回的所有info信息
3、定制Metrics信息
1、SpringBoot支持自动适配的Metrics指标
- JVM metrics, report utilization of:
- Various memory and buffer pools
- Statistics related to garbage collection
- Threads utilization
- Number of classes loaded/unloaded
- CPU metrics
- File descriptor metrics
- Kafka consumer and producer metrics
- Log4j2 metrics: record the number of events logged to Log4j2 at each level
- Logback metrics: record the number of events logged to Logback at each level
- Uptime metrics: report a gauge for uptime and a fixed gauge representing the application’s absolute start time
- Tomcat metrics (
server.tomcat.mbeanregistry.enabled
- must be set to
true
- for all Tomcat metrics to be registered)
- Spring Integration metrics
2、增加定制Metrics指标
方式一:
- 想在哪一个类中添加指标监控信息-----在该类中添加一个Counter counter属性
- 给该类的构造函数中传一个MeterRegistry指标工厂
- 构造方法中调用counter = meterRegistry.counter ("指标名称");
- 在其他方法上添加:
- counter.increment(); 方法每调用一次值+1
方式二:
- 配置类中添加MeterBinder组件
- 方法返回值:return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
//方式一:
class MyService{ //这个是一个普通的Service类
Counter counter;
public MyService( MeterRegistry meterRegistry){ //该类的构造函数
//MeterRegistry指标工厂
counter = meterRegistry. counter( "myservice.method.running.counter"); //给指标气名称
}
public void hello() {
counter. increment(); //没调用这个方法计数+1
}
}
//方式二:
MeterBinder queueSize( Queue queue) {
return ( registry) -> Gauge. builder( "queueSize", queue:: size). register( registry);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
4、定制监控端点
1、SpringBoot的默认监控端点
- 上文我们使用的都是springboot的默认监控端点
- springboot的默认监控点有很多--可以查看官方文档 | 上文也有
- 当我们访问: http://localhost:8080/actuator 的时候可以看到以暴露的监控端点
2、自定义一个监控端点
- 定制监控端点
- 定义一个类
- 在该类上 使用@Endpoint(id = "container") 标注该类是一个监控端点,并指明端点名称
- 在该类上 使用@Component放到容器中
- 写一个读方法(方法名、返回值自定义),该方法上添加@ReadOperation
- 写一个写方法(方法名、void自定义),该方法上添加@WriteOperation
- 使用监控端点
- 打开DOS:jconsole使用我们的监控端点中的方法
( id = "container")
public class DockerEndpoint {
public Map getDockerInfo(){
//端点的读操作
return Collections. singletonMap( "info", "docker started...");
}
private void restartDocker(){
//端点的写操作
System. out. println( "docker restarted....");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
场景:
开发ReadinessEndpoint来管理程序是否就绪,或者LivenessEndpoint来管理程序是否存活;
当然,这个也可以直接使用
边栏推荐
- sqlite数据库的系统表sqlite_master
- Using cache in vuex to solve the problem of data loss in refreshing state
- 自己创建一个时间拦截器
- System table SQLite of SQLite database_ master
- Redis Basics
- How to create and enforce indexes
- Soft test preparation multimedia system
- Development principle analysis and source code of dapp-lp single and dual currency liquidity pledge mining system
- IK分词器
- tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
猜你喜欢
The cross compilation environment appears So link file not found problem
IK word breaker
微服务架构
项目实战四:用户登录及token访问验证(reids+jwt)
Commodity seckill system
Numpy之matplotlib
wm_concat()和group_concat()函数
Leetcode 128 longest continuous sequence
Project practice 6: distributed transaction Seata
Record of user behavior log in SSO microservice Engineering
随机推荐
项目实战五:搭建ELk日志收集系统
IK word breaker
Résumé des points de connaissance
VCD video disc
Map and list < map > transfer to corresponding objects
读书笔记:《过程咨询 III》
元宇宙链游开发案例版 NFT元宇宙链游系统开发技术分析
项目实战四:用户登录及token访问验证(reids+jwt)
JSONUtils工具类(基于alibaba fastjson)
Image binarization
Crawl Douban to read top250 and import it into SqList database (or excel table)
NFTGameFi链游系统开发详解方案丨链游系统开发原理解析
8VC Venture Cup 2017 - Final Round C. Nikita and stack
DVD digital universal disc
The eigen library calculates the angle between two vectors
CD-CompactDisk
Idea collection code, quickly open favorites collection window
JVM入個門(1)
ROS查询话题具体内容常用指令
Convex hull problem