SpringBoot2--- 指标监控,Java 架构师之路
发布于: 2 小时前
web:
exposure:
include: '*' #以web方式暴露
复制代码
**测试**
复制代码
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
。。。。。。
* * *
[](
)2、Actuator Endpoint
==================================================================================
[](
)1、最常使用的端点
------------------------------------------------------------------------



### [](
)最常用的Endpoint
**Health:监控状况**
**Metrics:运行时指标**
**Loggers:日志记录**
* * *
[](
)2、Health Endpoint
--------------------------------------------------------------------------------
**健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要Health Endpoint可以为平台返回当前应用的一系列组件健康状况的集合。**
开启health的显示详细endpoint.health.show-details=always
**重要的几点:**
* health endpoint返回的结果,应该是一系列健康检查后的一个汇总报告 (有任何一个应用是宕机状态,整个就是宕机状态)
* 很多的健康检查默认已经自动配置好了,比如:数据库、redis等
* 可以很容易的添加自定义的健康检查机制

* * *
[](
)3、Metrics Endpoint
---------------------------------------------------------------------------------
**提供详细的、层级的、空间指标信息,这些信息可以被pull(主动推送)或者push(被动获取)方式得到;**
* 通过Metrics对接多种监控系统
* 简化核心Metrics开发
* 添加自定义Metrics或者扩展已有Metrics

* * *
[](
)4、管理Endpoints
----------------------------------------------------------------------------
### [](
)1、开启与禁用Endpoints
* 默认所有的Endpoint除过shutdown都是开启的。
* 需要开启或者禁用某个Endpoint。配置模式为 management.endpoint..enabled = true
复制代码
management:
endpoint:
beans:
enabled: true
复制代码
* 或者禁用所有的Endpoint然后手动开启指定的Endpoint (management.endpoints.enabled-by-default默认是true)
复制代码
management:
endpoints:
enabled-by-default: false
复制代码
endpoint:
beans:
enabled: true
health:
enabled: true
复制代码
* * *
[](
)2、暴露Endpoints (监控端点)
===================================================================================
**支持的暴露方式**
**HTTP:默认只暴露health和info Endpoint**
**JMX:默认暴露所有Endpoint (例如Jconsole)**
**除过health和info,剩下的Endpoint都应该进行保护访问。如果引入SpringSecurity,则会默认配置安全访问规则**


* * *
[](
)定制 Endpoint
==========================================================================
[](
)1、定制一个组件的Health信息(自定义的Health类名必须叫xxxHealthIndicator.xxx则是组件名字)
-----------------------------------------------------------------------------------------------------------------------------
### [](
)这里定制监控端点有两种方式,一种是实现HealthIndicator 接口,一种是继承 AbstractHealthIndicator抽象类
* * *
### [](
)方法一: 继承接口
复制代码
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
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();
复制代码
management:
health:
enabled: true
show-details: always #总是显示详细信息。可显示每个模块的状态信息
```
* * *
### [](
)方法二:实现抽象类
```
@Component
public class MyConHealthIndicator extends AbstractHealthIndicator {
//这里可以通过获取容器中某个我们要监测的组件,判断其健康情况,来决定是up还是down
/**
* 真实的检查方法
*
* @param builder
* @throws Exception
*/
@Override
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);
}
}
```

* * *
### [](
)设置为总是显示详细信息
```
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
show-details: always #总是显示详细信息。可显示每个模块的状态信息
```

* * *
[](
)2、定制info信息—用于展示当前应用详细信息
--------------------------------------------------------------------------------------
#### [](
)方式一: 编写配置文件
```
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
show-details: always #总是显示详细信息。可显示每个模块的状态信息
info:
appName: boot-admin
version: 2.0.1
mavenProjectName: @project.artifactId@ #使用@@可以获取maven的pom文件值
mavenProjectVersion: @project.version@
```
#### [](
)方式二: 编写InfoContributor
```
@Component
public class ExampleInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("example",
Collections.singletonMap("key", "value"));
}
}
```

**会输出以上方式返回的所有info信息,即配置文件和代码迭代的所有info信息**
* * *
[](
)3、定制Metrics信息 (运行时指标)
------------------------------------------------------------------------------------
### [](
)1、SpringBoot支持自动适配的Metrics

* * *
[](
)2.增加定制Metrics
----------------------------------------------------------------------------
```
@RestController
public class TestController
{
Counter counter;
public TestController(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
@GetMapping("/test")
public String test()
{
//该请求每发一次,就会增加一次记录数,来记录当前请求被调用的次数
counter.increment();
return "hhhhh";
}
}
//也可以使用下面的方式
@Bean
MeterBinder queueSize(Queue queue) {
return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
# **最后**
**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://ali1024.coding.net/public/P7/Java/git)**

复制代码
划线
评论
复制
发布于: 2 小时前阅读数: 3
程序猿一枚
关注
VX:vip204888 领取资料 2021.07.29 加入
还未添加个人简介
评论