springboot2.X 整合pushgateway推送到prometheus,最后以grafana展示
1 pom.xml中添加以下依赖<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.1.3</version></dependency>
·
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 展示效果(我自己删除了点东西)至此完成,谢谢
更多推荐
已为社区贡献1条内容
所有评论(0)