欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springBoot1.X或2.X整合prometheus配置

程序员文章站 2022-04-30 17:46:24
...

1、首先需要引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.5.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-spring-legacy</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

spring-boot-starter-security依赖是为了开启对监控数据获取接口(我这里是/prometheus)进行用户名密码校验的,如果没有这个校验我们的监控接口相当于在裸奔。任何人都可以通过这个接口获取我们的监控数据

 

  • 2、添加一个配置类,使prometheus知道我们当前项目的名称以及其他信息

    @Configuration
    public class MicrometerConfiguration {
        @Bean
        MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
            return meterRegistry1 -> {
                meterRegistry.config()
                        .commonTags("application", "micrometer-gateway");
            };
        }
    }

    3、配置application.properties

    #开启安全验证
    management.security.enabled=true
    #监控地址接口,这里可以和项目端口区分开
    management.port=20028
    #监控地址前缀
    management.context-path=/monitor
    management.endpoints.web.exposure.include=*
    management.endpoints.jmx.exposure.include=*
    management.endpoints.shutdown.exposure.include=*
    management.metrics.distribution.percentiles-histogram[http.server.requests]=true
    
    #这是spring-boot-starter-security对应的basic_auth验证开启
    security.basic.enabled=true
    #对哪些地址开启校验
    security.basic.path=/monitor
    security.user.name=admin
    security.user.password=admin