springboot2.X 整合pushgateway推送到prometheus,最后以grafana展示

1 pom.xml中添加以下依赖

     <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_servlet</artifactId>
            <version>0.9.0</version>
        </dependency>
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_pushgateway</artifactId>
            <version>0.9.0</version>
        </dependency>
  

2 yaml配置

spring:
  application:
    name: springProject-name
management:
  endpoints:
    web:
      exposure:
        include: metrics
  metrics:
    tags:
      application: ${spring.application.name}
    export:
       prometheus:
          pushgateway:
            #pushgateway地址
            baseUrl: 127.0.0.1:8080
            #推送周期
            pushRate: 15s
            #job定义名
            job : ${spring.application.name}
            #启用推送
            enabled: true

3 在启动类中注入bean,除暴露的基础信息外自己添加一些额外的固定信息。

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

4 继承MeterBinder,添加一些额外的动态信息


public class CustomMetric implements MeterBinder {

    public static AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public void bindTo(MeterRegistry meterRegistry) {
        Gauge.builder("exception.error.count", atomicInteger, c->ServiceRuntimeInfo.getAccessNumAndClear())
                .description("exception of service error meter")
                .register(meterRegistry);
    }

}

5 启动一个pushgateway服务,不然java服务推送报错

docker run -d \ 
 --restart always \
-p 8080:9091\
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime\
-v /tmp/prometheus-data:/prometheus-data/ \ 
--name kw_prometheus_pushgateway\
prom/pushgateway:latest

6 启动java服务

7 访问pushgateway服务:127.0.0.1:8080,已经出现服务推送的信息。

 

8 在prometheus的配置文件中,添加pushgateway地址后重启。

scrape_configs:
  - job_name: 'pushgateway'
    scrape_interval: 15s
    static_configs:
    - targets: ['127.0.0.1:8080']

9 grafana 引用12856模板即可

10 展示效果(我自己删除了点东西)至此完成,谢谢

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值