Actuator
监控Springboot应用,获取他的各个指标
如果你的Springboot引入了Actuator,那么你的应用就可以暴露一下指定的端口,外界可以通过这些端口拿到应用的运行情况
maven导入jar:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
配置:
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
添加一个配置项:
@SpringBootApplication
public class Trainingtest1Application {
public static void main(String[] args) {
SpringApplication.run(Trainingtest1Application.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
启动应用,访问/actuator:
访问/actuator/health
默认只有health和info是可以访问的
配置yml中开启了启用所有端点
endpoints:
web:
exposure:
include: "*"
Prometheus
Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合
定时收集你应用的各项指标
docker中安装:
拉取:
docker pull prom/prometheus
prometheus.yml:配置文件
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
scrape_interval: 5s
static_configs:
- targets: ['127.0.0.1:9090']
# 任意写,建议英文,不要包含特殊字符
- job_name: 'spring'
# 多久采集一次数据
scrape_interval: 15s
# 采集时的超时时间
scrape_timeout: 10s
# 采集的路径是啥
metrics_path: '/actuator/prometheus'
# 采集服务的地址,设置成上面Spring Boot应用所在服务器的具体地址。
static_configs:
- targets: ['应用IP:端口']
可以监控多个项目,每个job_name就是一个项目的名字,然后是配置这个项目
注意:应用IP不能是内网IP,需要是你的服务器的公网IP,一开始我在配置的时候用localhost,然后这个服务的监控就提示连接失败,然后我把应用部署到服务器上,把应用IP:端口换成服务器的IP和应用的端口就好了。
配置文件放一个地方,容器运行时需要映射到这个文件,比如放在/root/prometheus/prometheus.yml
运行:
docker run -d -p 9090:9090 --name prometheus -v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml docker.io/prom/prometheus
别忘了开发服务器9090端口
Grafana
图像化监控
docker中安装:
docker pull grafana/grafana
运行
docker run -d -p 3000:3000 --name grafana docker.io/grafana/grafana
点击:Add your first data source
输入prometheus的url,测试并保存
添加Dashboard:
可以自己配置Dashboard,也可以从Dashboard市场中选择别人已经配置好的Dashboard用:
选择并搜索配置好的D按时board
记住这个Dashboard的ID:9568
来到Grafana:
- -> 导入:
输入ID
选择你刚才配置的Prometheus
Dashboard面板: