# nacos-consul-adapter **Repository Path**: wen_biao/nacos-consul-adapter ## Basic Information - **Project Name**: nacos-consul-adapter - **Description**: No description available - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-06 - **Last Updated**: 2024-03-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Nacos Consul Adapter(for Prometheus) Prometheus官方提供Consul为注册中心的配置方式,配置后可自动获取Consul中所有实例的信息并进行监控。 如果想使用Prometheus监控使用其他注册中心的服务就需要一些额外的适配,适配的目的是让项目提供Consul服务器相同接口。 本项目是以Nacos为注册中心的适配器,供Prometheus获取注册中心的所有实例信息。本项目受Eureka Consul Adapter项目的启发,实现了Nacos Consul Adapter。在Eureka的适配实现中作者使用的是Rx-Java,本系统是使用Reactor3实现和Spring Cloud GateWay的技术栈一致。之前也有Nacos Consul Adapter的实现在GitHub开源,但是由于Consul在某个版本修改了健康检测接口的路径,所以在高版本的Prometheus就无法使用了,并且之前的版本也未提供长轮询实现。 ## 特性 项目一共只有四个接口: 1. /v1/agent/self:返回数据中心的名称。这个接口暂时返回固定内容,未提供配置。 2. /v1/catalog/services:返回注册中心所有服务名称。 3. /v1/health/service/{appName}:返回指定名称服务的所有实例。 4. /v1/catalog/service/{appName}:返回指定名称服务的所有实例,兼容早期的Prometheus版本。 ## 长轮询和直接查询 本项目提供两种模式请求接口:`直接查询和长轮询` *直接查询:* Prometheus调用本项目,本项目直接请求Nacos服务端获取内容。 *长轮询:* 本项目使用Reactor3来实现长轮询,在指定的等待时间内如果注册信息发生变化会立即返回否则会一直等待直到指定时间流逝。在这种模式下不会直接调用Nacos服务器,而是请求的本地Nacos客户端的内容。 可以将本项目引入到Spring Cloud GateWay,在Prometheus中指定网关地址。 ## 配置选项 `nacos-consul-adapter.mode`:使用模式。`direct`:直接查询,`long-polling`:长轮询。默认为长轮询。 `nacos-consul-adapter.serviceNameIntervalMills`:在长轮询模式下,请求服务名称的间隔时间。 ## 要求 JDK 1.8+ spring-cloud-starter-alibaba-nacos-discovery 2.2+ (默认是2.2.4+,如果需要2.2.0+则需要从对应的2.2.0分支获取代码构建) spring-boot 2.3+ 较低版本Spring Boot未做测试 ## 快速开始 可以在Spring Cloud Gateway中引入下面的jar包(可以是服务中任意的节点,在Spring Cloud Gateway中原因是,它基于Reactor。如果不是使用Spring WebFlux则还需要引入额外的包),然后在Prometheus中配置Spring Cloud Gateway的一个实例的ip地址就可以了。 项目的开源地址为:https://github.com/chen-gliu/nacos-consul-adapter。对本项目有任何问题和优化建议都可以提issue。 ``` io.github.chen-gliu nacos-consul-adapter version ``` 如果拉取不到项目,可以在setting文件中添加如下配置: ```$xslt mvnrepository * 仓库 https://repo1.maven.org/maven2 ``` 在Prometheus中添加如下配置: ``` - job_name: 'consul-prometheus-test' metrics_path: '/actuator/prometheus' consul_sd_configs: - server: 引入nacos-consul-adapter实例的ip+端口 services: [] ``` 在每个Spring Cloud实例中引入Prometheus与actuactor监控包: ``` io.micrometer micrometer-registry-prometheus org.springframework.boot spring-boot-starter-actuator ``` 此外还需要开放actuator的端点,不清楚可以参考测试代码中的配置。 ```java @Bean MeterRegistryCustomizer configurer( @Value("${spring.application.name}") String applicationName) { return (registry) -> registry.config().commonTags("application", applicationName); } ``` 这样简单的配置后,启动服务就可以在Prometheus中看到注册在Nacos中的服务了。并且在Labels默认会给注册的微服务打一个nacos_application_name标签 ![Prometheus实际效果图](https://user-images.githubusercontent.com/36329283/206843239-3057b673-51e8-4c53-87cf-a94a2288f3c7.png) ![Grafana实际效果图](https://img-blog.csdnimg.cn/20210626172040746.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xDQlVTSElIQUhB,size_16,color_FFFFFF,t_70) ## 较低版本Spring Cloud如何使用本项目达到监控目的 - 另外重新创建一个新项目,新的项目使用spring-cloud-starter-alibaba-nacos-discovery 2.2+版本。 - 引入本项目,具体可参考快速开始。 - 将这个新项目的服务注册到Nacos。 - 在Prometheus中配置这个新项目的服务地址。 做完上面这些就可以监控低版本的Spring Cloud集群了,不过要注意低版本和高版本的Metric是不一样的,要合理选择Grafana的图表。 ## Prometheus增加自定义打标签 整合nacos-consul-adapter成功后默认会给注册的每一个微服打nacos_application_name标签, 如果想要增加自定义标签可以在微服务启动注册到nacos时增加元数据metadata ![nacos实际效果图](https://user-images.githubusercontent.com/36329283/206841787-5730385a-876d-40e9-96ef-529b753fa664.png) 接着配置prometheus.yml ```java - job_name: xxx metrics_path: '/actuator/prometheus' consul_sd_configs: - server: xxx relabel_configs: - source_labels: [__meta_consul_service] - regex: __meta_consul_service_metadata_(.+) action: labelmap ``` 然后Prometheus Labels会将nacos的metadata作为自定义标签 ![Prometheus实际效果图](https://user-images.githubusercontent.com/36329283/206841939-33f48649-5124-4810-8942-abd54fbee063.png) ## 感谢 感谢Eureka Consul Adapter项目开发人员,项目中部分代码借鉴了Eureka Consul Adapter的实现。