<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.1.3</version>
</dependency>
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
spring.application.name=community
# 打开所有 Actuator 服务
management.endpoints.web.exposure.include=*
# 将应用名称添加到计量器的 tag 中去,以便 Prometheus 根据应用名区分不同服务
management.metrics.tags.application=${spring.application.name}
# 暴露Tomcat
server.tomcat.mbeanregistry.enabled=true
下载:Prometheus压缩包 解压并在安装目录下的prometheus.yml的最下方新增Job(要注意格式),代码如下:
- job_name: "community"
# 这里我们springboot暴露出来的endpoint
metrics_path: 'actuator/prometheus'
# 信息收集时间是间隔5秒
scrape_interval: 5s
static_configs:
- targets: ["localhost:后台端口号"]
运行Prometheus及后台程序,访问【localhost:9090】,点击【Status-Targets】可以查看监控的端口列表
下载:Grafana压缩包 解压后把conf/sample.ini复制一份然后重命名为conf/custom.ini,点击bin目录下的grafana-server.exe运行
访问localhost:3000,添加Prometheus数据源
SpringBoot模板: https://grafana.com/grafana/dashboards/10280
JVM模板: https://grafana.com/grafana/dashboards/12856
注意:若是阿里云服务器需要在阿里云管理后台新增两个端口的访问规则(9090、3000)
下载:Prometheus压缩包 并解压
tar -zxvf prometheus-2.39.1.linux-amd64.tar.gz -C /usr/local/
在安装目录下的prometheus.yml的最下方新增Job(要注意格式),代码如下:
- job_name: "community"
scheme: https
tls_config:
insecure_skip_verify: true
# 这里我们springboot暴露出来的endpoint
metrics_path: 'actuator/prometheus'
# 信息收集时间是间隔5秒
scrape_interval: 5s
static_configs:
- targets: ["服务器IP:后台端口"]
# 非后台启动(修改好配置文件后先运行此命令并访问【http://服务器IP:9090】,确定能访问后再用以下语句启动)
sudo ./prometheus
# 后台启动
nohup ./prometheus --config.file=prometheus.yml --web.enable-admin-api --web.listen-address=:9090 >/dev/null 2>&1 &
# 查看占用端口进程的PID
netstat -tunlp|grep 9090
# 杀死进程
kill -9 {PID}
a. 下载:https://grafana.com/grafana/download 中的grafana-enterprise-2.1-1.x86_64.rpm并运行以下命令
# 运行安装命令
sudo rpm -i --nodeps grafana-enterprise-9.2.1-1.x86_64.rpm
# 启动Grafana
sudo /bin/systemctl start grafana-server.service
b. 访问【http://服务器IP:3000】,添加Prometheus数据源
c. 下载并导入模板:
SpringBoot模板: https://grafana.com/grafana/dashboards/10280
JVM模板: https://grafana.com/grafana/dashboards/12856