Prometheus 监控Nacos注册中心微服务、自动暴露
本文要讲的不是使用Prometheus+Grafana监控Nacos服务本身,而是监控注册在Nacos中的服务。Prometheus和Grafana的整合网上有很多教程也就不在详细介绍了如果实在不会我抽空在写一篇安装、本篇的主要内容是注册在Nacos中的服务如何被Prometheus自动监控,不需要在Prometheus中为每个微服务单独的进行配置。
项目的开源地址: https://github.com/chen-gliu/nacos-consul-adapter
将Nacos伪装成Consul
在Spring Cloud Gateway引入jar包
<dependency>
<groupId>io.github.chen-gliu</groupId>
<artifactId>nacos-consul-adapter</artifactId>
<version>version</version>
</dependency>
如果拉取不到包,可以在setting文件中添加如下配置:
<mirror>
<id>mvnrepository</id>
<mirrorOf>*</mirrorOf>
<name>仓库</name>
<url>https://repo1.maven.org/maven2</url>
</mirror>
在Prometheus中添加如下配置:
- job_name: "nacos"
scrape_interval: 2s
metrics_path: '/nacos/actuator/prometheus'
static_configs:
- targets: ['192.168.10.141:8848']
#或者
- job_name: nacos
scrape_interval: 2s
metrics_path: '/nacos/actuator/prometheus'
static_configs:
consul_sd_configs:
- server: '192.168.10.141:8848'
services: []
prometheus每隔2秒钟从http://192.168.10.141:8848/nacos/actuator/prometheus这个url拉取指标数据。
配置application.properties文件,暴露metrics数据
management.endpoints.web.exposure.include=*
访问{ip}:8848/nacos/actuator/prometheus,看是否能访问到metrics数据
在每个Spring Cloud实例中的配置
引入Prometheus监控包
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
暴露每个应用的指标接口
在每个需要监控的项目里面加上:
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}