Prometheus+Grafana实现服务监控
项目引入依赖:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
yml暴露端点:
management:
endpoints:
web:
exposure:
include: '*' #暴露所有
启动项目访问http://localhost:PORT/actuator/prometheus
整合Prometheus
使用docker安装:
- 检查docker是否运行(systemctl status docker)
- 安装运行Prometheus
官网地址:https://prometheus.io/docs/prometheus/latest/getting_started/
-
在/home/admin/prometheus目录下建一个prometheus.yml
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # 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' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090']
-
运行下面语句进行安装运行
docker run -p 9090:9090 -v /home/admin/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
-
访问 http://虚拟机IP:9090/
-
整合项目
修改prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
- job_name: 'abs-gateway' #名称,任意不重复
metrics_path: '/actuator/prometheus' #制定获取路径
scrape_interval: 5s #间隔
static_configs:
- targets: ['192.168.0.105:8000'] #实例的IP,默认协议是http
重新启动容器:
重新访问 http://虚拟机IP:9090/
整合Grafana
docker运行安装Grafana
docker run -d --name=grafana-3000 -p 3000:3000 grafana/grafana
访问 http://虚拟机IP:3000/
![image-20210105101437439](https://i-blog.csdnimg.cn/blog_migrate/6b0474f6202de59a7de21b7b8e6bdf83.png)
默认账号密码都是admin,登陆后会让你重新输入密码,可选择跳过。
![image-20210105101624450](https://i-blog.csdnimg.cn/blog_migrate/62587abe2f6792d1c3f9147d7492dc52.png)
选择Prometheus数据源
然后点击保存并测试。
创建一个panel
选择你刚刚配置的prometheus数据源:
然后选择你要查询的内容
至此整合完成。docker安装下次更新。。。