micrometer + grafana + prometheus搭建JVM监控


micrometer

在spring2.x中,spring-boot-actuator集成了micrometer, 可以直接导入

https://micrometer.io/docs/registry/prometheus

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.metrics

  1. pom.xml导入包

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    	<version>${spring.boot.version}</version>
    </dependency>
    <dependency>
    	<groupId>io.micrometer</groupId>
    	<artifactId>micrometer-registry-prometheus</artifactId>
    	<version>${micrometer.prometheus.version}</version>
    </dependency>
    
  2. Application启动类中添加micrometer监控输出

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}")String applicationName) {
    	return registry -> registry.config().commonTags("application", applicationName);
    }
    
  3. 添加配置

    1. 配置中将 /actuator/prometheus 访问开放出来, 设置标签名

      management.endpoints.web.exposure.include = prometheus
      management.metrics.tags.application = ${spring.application.name}
      
    2. actuator包下的actuate.jdbc.DataSourceHealthIndicator会在启动时进行一次jdbc配置的健康检查,对于某些系统可能会产生warning级别的报警。因此需要在配置中禁用

      management.health.db.enabled=false
      
  4. 启动项目, 打开本地接口 localhost:8090/actuator/prometheus 查看输出

prometheus

  1. 创建docker配置

    1. 创建docker目录prometheus-docker

    2. 在目录下创建 docker-compose.yml

      version: "3"
      services:
        prometheus_server:
          image: prom/prometheus
          ports:
            - "9090:9090"
          volumes:
            - ./prometheus.yml:/etc/prometheus/prometheus.yml
      
    3. 在目录下创建 prometheus.yml

      https://prometheus.io/docs/prometheus/latest/getting_started/

      需要注意修改job的targets

      global:
        scrape_interval:     15s
        external_labels:
          monitor: 'codelab-monitor'
      scrape_configs:
        - job_name: 'prometheus'
          scrape_interval: 5s
          static_configs:
            - targets: ['localhost:9090']
      	- job_name: 'spring'
          scrape_interval: 5s
          metrics_path: '/actuator/prometheus'
          static_configs:
            - targets: ['192.168.180.17:8090'] #改成自己的内网/外网IP和端口, 如果设置为localhost那只会访问容器自身
      
  2. 创建完毕docker配置后, 在目录下运行命令

    docker-compose up -d
    
  3. 打开 localhost:9090 , 点击status→target检查job运行情况

grafana搭建

  1. 在docker上搭建grafana, 运行命令安装部署

    # 默认使用最新版本, 绑定3000端口
    docker run -d --name=grafana -p 3000:3000 grafana/grafana
    
  2. 部署完毕后登录localhost:3000, 默认账号密码为admin, admin

  3. 配置Prometheus数据源

    1. 点击Configuration→Prometheus进入,配置URL为本机IP,Access使用默认Server,配置HTTP为POST

    2. 配置完毕后点击 Save & test 进行检查

  4. 配置仪表盘,可以自行进行设置或导入其他人的模板,这里我们进行导入

    1. 打开https://grafana.com/grafana/dashboards, 可以从官网中选择其他人上传的模板, 可以选择ID进行联网导入或是下载json离线导入。本文中使用的模板是https://grafana.com/grafana/dashboards/4701

    2. 导入完毕后选择Prometheus数据源

  5. 数据源和仪表盘配置完毕后,打开仪表盘进行查看

以上JVM监控搭建完毕。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM