prometheus监控Springboot应用

181 阅读1分钟

前言

在使用Springboot开发的程序,有时候要监控其运行状态等,可以使用prometheus进行监控

prometheus监控

环境搭建

项目创建

创建一个Springboot项目,然后加入在pom.xml加入以下包

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <scope>runtime</scope>
</dependency>

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
项目创建

在application.properties中配置以下配置

management.endpoints.web.exposure.include=*
spring.application.name=demo-prometheus
创建bean
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
        @Value("${spring.application.name}") String applicationName) {
    return (registry) -> registry.config().commonTags("application", applicationName);
}
prometheus配置

1、 在prometheus.yml中创建配置

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['127.0.0.1:9090']
###以下内容为SpringBoot应用配置
  - job_name: 'demo-prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8030']

其中demo-prometheus为项目配置名称,8030为项目端口,然后启动prometheus

2、 访问项目Ip+端口,可以获取项目配置

http://ip:端口/actuator/prometheus

image.png

3、也可以访问prometheus查看项目配置

http://ip:9090/targets

image.png 备注: prometheus默认启动端口是9090

grafana配置

1、 启动grafana,默认端口为3000,通过以下地址访问

http://ip:端口

访问,成功的话,会出现以下界面

image.png 2、 输入账号和密码进行登录

image.png 3、点击+进行配置

1731823728276.jpg

4、 选择Import

image.png 填写4107,然后点击Load

5、输入Name以及选择数据源

image.png

6、然后点击Import,这时候就可以查看项目配置

image.png

总结

prometheus是一个优秀的监控数据组件,可以很好的利用它进行项目等监控