Springboot与监控
一、原因
一个完整的项目必然要监控,不然出了问题就沙雕了。而prometheus+grafana因为:
- 炫酷好看(重点)
- 天然支持k8s+docker
- 完美适配springboot
所以被选为监控的第一技术选型
二、springboot使用
先从简单的部分开始
我们先不考虑怎么部署prometheus+grafana环境,默认已经有了,那springboot怎么用?
step1 pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 借助 Micrometer 对接 Prometheus 监控系统 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
这俩是必须的
step2 yml
spring:
application:
name: monitoring-prometheus-grafana
management:
endpoints:
web:
exposure:
# 将 Actuator 的 /actuator/prometheus 端点暴露出来
include: 'prometheus'
metrics:
tags:
application: ${spring.application.name}
server:
port: 9876
port随意,application-name必须有,management就是prometheus的部分,其中会引用到application-name属性
step3 启动项目
step4 在prometheus中配置服务,重启prometheus
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.
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'agent-node'
static_configs:
- targets: ['localhost:9100']
- job_name: 'spring-node'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:9876']
最后那个job_name就是我们的服务,会被普罗米修斯监控到。这部分看不懂不要紧,下面会详细说普罗米修斯在单机和docker的玩法
三、prometheus+grafana in local
本机配普罗米修斯
以MAC为例
1、安装
brew install promethus或从官网下载
brew install node_exporter或从官网下载
brew install grafana或从官网下载
复制前文的yml内容,生成一个配置文件,如:
vi /Users/xxx/prometheus.yml
记好这个位置
prometheus /Users/xxx/prometheus.yml启动,访问http://localhost:9090/targets全绿则没问题
node_exporter直接起就行
grafana同理
2、访问grafana
http://localhost:3000
不赘述grafana添加数据源什么的了,百度很多
四、in docker
创建虚拟机,本次使用centos7
cd /etc/sysconfig/network-scripts/
ls查看,会有一个你的网卡配置文件
vi ipcfg-xxx,把onboot字段的no改成yes
service network restart启用网络
yum update更新系统环境到latest
systemctl disable firewalld关闭/禁用防火墙
systemctl stop firewalld
yum install docker安装docker
service docker start启动docker
docker pull prom/prometheus拉取镜像
docker pull prom/node-exporter
docker pull grafana/grafana
准备一个如上的配置文件promethus,yml
docker run –restart=unless-stopped -d –name=grafana -p 3000:3000 grafana/grafana
docker run –restart=unless-stopped -d –name=prometheus -p 9090:9090 -v /home/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
docker run –restart=unless-stopped -d –name=node-exporter -p 9100:9100 -v /etc/localtime:/etc/localtime prom/node-exporter
主机访问虚拟机ip:3000打开grafana页面则成功
五、常见问题
如果docker run prometheus不能访问yml,则先把yml文件cp到docker里
docker cp /xx/xx/prometheus.yml prometheus:/etc/prometheus/prometheus.yml
然后
docker restart prometheus
如果docker run提示已经存在某个名字的容器:
docker rm -f prometheus
完毕
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153526.html