spring-boot-actuator 模块通过 HTTP endpoints 来获取 SpringBoot 项目中的审计、健康状况以及一些配置项、容器注册 bean 以及heapdump 等信息,
actuator 通过开放 http 接口方式来表示各个指标详情,但这种方式并不直观,并且针对一些指标阈值并没有提供一些报警规则,因此需要引入另外一个服务Prometheus ,专用于事件监视以及报警的开源工具。
Prometheus 指标是基于事件戳方式进行记录的,通过 key、value 方式存储,记录指标在时间维度方面颗粒度更小;除了Prometheus 外,针对指标可视化还需要一个工具Grafana,专用于数据查询、以及指标可视化。
具体使用如下:
1,引入 spring-boot-starter-actuator 以及 micrometer-registry-prometheus 依赖
<!-- spring-boot-actuator依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- prometheus依赖 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
2,SpringBoot启动入口,将服务名注册到 micrometer 中的 application,用于prometheus 数据抓取
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public MeterRegistryCustomizer<MeterRegistry> configure(
@Value("${spring.application.name}") String applicationName){
return (registry) -> registry.config().commonTags("application",applicationName);
}
}
3,配置yaml
management:
server:
port: 9002
# 开启 actuator 服务
endpoints:
web:
exposure:
# 暴露信息
include: '*'
# 暴露 metrics 端口
endpoint:
metrics:
enabled: true
# 配置 prometheus 服务
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true
tags:
application: ${spring.application.name}
4,启动项目,访问 http://localhost:9002/actuator/prometheus
5,配置Prometheus
下载安装Prometheus prometheus.io/download/
修改prometheus.yml文件
scrape_configs:
- job_name: 'exporter' #服务信息(cpu、内存、磁盘、网络等)
metrics_path: '/metrics' #指定抓取的路径
static_configs:
- targets: ['localhost:9182']
labels:
nodename: 'exporter-node'
- job_name: 'pv_warning-1'
metrics_path: '/actuator/prometheus' #指定抓取的路径
static_configs:
- targets: ['localhost:9003']
labels:
nodename: 'pv_warning-1'
- job_name: 'pv_warning-2'
metrics_path: '/actuator/prometheus' #指定抓取的路径
static_configs:
- targets: ['localhost:9002']
labels:
nodename: 'pv_warning-2'
启动prometheus 访问http://localhost:9090 即可看到监控的项目
6,安装 Grafana
下载安装后 浏览器打开 http://localhost:3000/ 账号密码 默认都是admin
7,配置数据源
这里选择的是prometheus数据源,如果需要查看springboot项目相关的信息,如api调用情况,需要配置配置Promtail 和 Loki
Promtail 和 Loki 下载地址:github.com/grafana/lok… 选择最新的版本下载这两个文件,解压文件
在github.com/grafana/lok… 页面分别找到 loki-local-config.yaml 和 promtail-local-config.yaml
然后修改配置文件里面的路径为本机的路径即可
uth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096 # 监听grpc端口
log_level: debug
grpc_server_max_concurrent_streams: 1000
common:
instance_addr: 127.0.0.1
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
ingester_rf1:
enabled: false
query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
pattern_ingester:
enabled: true
metric_aggregation:
enabled: true
log_push_observations: true
ruler:
alertmanager_url: http://localhost:9093 # 预警地址
frontend:
encoding: protobuf
然后使用配置文件分别启动loki和promtail
./loki-linux-amd64.sh--config.file=config/loki-config.yaml
./promtail-linux-amd64.sh--config.file=config/promtail-local-config.yaml
8,导入模板
可以从官网找到需要的dashboard模板 导入 到项目中,以springboot项目指标为例,官网搜索springboot的模板,
复制ID
导入模板
输入id
选择对应的数据源即可
最终效果:
服务相关(CPU。磁盘、网络等)
JVM相关:
spring boot相关: