SpringBoot+Prometheus+Grafana监控服务

发布时间 2023-04-03 17:37:20作者: 邹笑傲

服务模块

pom.xml文件引入依赖

基于SpringBoot版本 <2.5.3>

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.5.3</version>
        </dependency>

yml文件配置

management:
  endpoints:
    web:
      exposure:
        include: '*'

安装Prometheus

使用docker安装

1.拉取Prometheus镜像

docker pull prom/prometheus

2.创建prometheus.yml文件

global:
  scrape_interval:     60s
  evaluation_interval: 60s

scrape_configs:
# 可随意指定
   - job_name: 'spring'
# 采集的路径
     metrics_path: '/actuator/prometheus'
# 采集服务的地址,设置成Springboot应用所在服务器的具体地址
     static_configs:
       - targets: ['192.168.254.220:42244']

3.启动prometheus容器

/usr/local/ware/prometheus/config/prometheus.yml 是第2步创建prometheus文件的路径

 docker run  -d  -p 9090:9090  -v /usr/local/ware/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

4.访问prometheus页面。

localhost:9090
访问不通,需检查防火墙是否放行端口。

安装Grafana

使用docker安装

1. 拉取镜像

docker pull grafana/grafana

2. 启动容器

 docker run -d -p 3000:3000 --name=grafana -v /usr/local/ware/grafana:/var/lib/grafana  grafana/grafana

3. 访问页面

http://192.168.48.132:3000

4. 配置prometheus数据源

4.1选择数据源

4.2 配置prometheus数据源

4.3 点击控制面板

4.4 监控信息