通过Prometheus + Grafana对线上应用进行观测、监控、预警...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
ID | 描述 |
---|---|
| 暴露当前应用程序的审核事件信息。需要一个 |
| 显示应用程序中所有Spring Bean的完整列表 |
| 暴露可用的缓存 |
| 显示自动配置的所有条件信息,包括匹配或不匹配的原因 |
| 显示所有 |
| 暴露Spring的属性 |
| 显示已应用的所有Flyway数据库迁移。需要一个或多个 |
| 显示应用程序运行状况信息 |
| 显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个 |
| 显示应用程序信息 |
| 显示Spring |
| 显示和修改应用程序中日志的配置 |
| 显示已应用的所有Liquibase数据库迁移。需要一个或多个 |
| 显示当前应用程序的“指标”信息 |
| 显示所有 |
| 显示应用程序中的计划任务 |
| 允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序 |
| 使应用程序正常关闭。默认禁用 |
| 显示由 |
| 执行线程转储 |
| 返回 |
| 通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖 |
| 返回日志文件的内容(如果已设置 |
| 以Prometheus服务器可以抓取的格式公开指标。需要依赖 |
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 MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @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);
}
}
class MyService{
Counter counter;
//默认一个构造时,参数会从ioc中拿
public MyService(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
public void hello() {
counter.increment();
}
}
基于 Prometheus + Grafana
安装 Prometheus + Grafana
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.10.6</version>
</dependency>
management:
endpoints:
web:
exposure: #暴露所有监控的端点
include: '*'
访问: http://localhost:8001/actuator/prometheus 验证,返回 prometheus 格式的所有指标
部署Java应用到服务器
确保可以访问到部署好的服务,http://192.168.254.129:8080/actuator/prometheus
http://192.168.254.129:8080/actuator
## 修改 prometheus.yml 配置文件
scrape_configs:
- job_name: 'spring-boot-actuator-exporter'
metrics_path: '/actuator/prometheus' #指定抓取的路径
static_configs:
- targets: ['192.168.254.129:8080']
labels:
nodename: 'app-demo'
配置完记得重启容器
填入面板id,选择刚刚创建好的数据源
等待应用运行一会后,就会显示出对应的监控数据
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。