分布式项目:新建微服务当作监控中心 actuator+Prometheus +Grafana

需求:一个分布式项目 ,现在要新建一个微服务作为监控中心,监控其他微服务的状态,都用到actuator 具体怎么做,在新建的微服务中,怎么让新的微服务拿到所有微服务暴露的端点信息,怎么把这些暴露的端点存到prometheus中,分布式项目用到了nacos gatewey 等 

为了创建一个新的微服务作为监控中心,监控其他微服务的状态,并将这些状态暴露到 Prometheus 和 Grafana 中,可以按照以下详细步骤进行:

1. 创建监控中心微服务

步骤 1.1:创建新微服务

创建一个新的 Spring Boot 微服务项目,作为监控中心。这个微服务将用于从其他微服务中收集 Actuator 数据。

步骤 1.2:添加依赖

在新的微服务的 pom.xml 中添加 Actuator 和 Prometheus 的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. 配置微服务监控

步骤 2.1:配置 Actuator

在新微服务的 application.ymlapplication.properties 文件中,启用 Actuator 端点:

management:
  endpoints:
    web:
      exposure:
        include: '*'
步骤 2.2:配置 Prometheus

在新微服务的 application.ymlapplication.properties 文件中,启用 Prometheus 监控:

management:
  metrics:
    export:
      prometheus:
        enabled: true

3. 配置服务发现

由于分布式项目使用了 Nacos,可以通过 Nacos 动态发现其他微服务的地址。在新微服务中配置 Nacos 客户端:

步骤 3.1:添加 Nacos 依赖

pom.xml 中添加 Nacos 客户端依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
步骤 3.2:配置 Nacos 客户端

application.yml 中配置 Nacos:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

4. 实现服务监控功能

步骤 4.1:创建 Feign 客户端

使用 Feign 客户端从其他微服务中抓取 Actuator 数据。首先,在 pom.xml 中添加 Feign 依赖(如果还没有):

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

然后,定义 Feign 客户端接口:

@FeignClient(name = "microservice-name")
public interface ActuatorClient {
    @GetMapping("/actuator/prometheus")
    String getPrometheusMetrics();
}
步骤 4.2:实现定时任务

使用定时任务定期从其他微服务中抓取 Actuator 数据,并将这些数据存储到 Prometheus:

@Service
public class MonitoringService {
​
    @Autowired
    private ActuatorClient actuatorClient;
​
    @Scheduled(fixedRate = 60000)
    public void fetchMetrics() {
        // 从每个微服务抓取 Actuator 数据
        String metrics = actuatorClient.getPrometheusMetrics();
        // 处理抓取到的指标数据
    }
}

5. 配置 Prometheus 和 Grafana

步骤 5.1:配置 Prometheus

配置 Prometheus 以抓取监控中心微服务的指标。在 Prometheus 的配置文件 prometheus.yml 中,添加监控中心微服务的 scrape 配置:

scrape_configs:
  - job_name: 'monitoring-service'
    static_configs:
      - targets: ['http://monitoring-service:8080/actuator/prometheus']
步骤 5.2:配置 Grafana

在 Grafana 中配置 Prometheus 数据源,添加仪表板以展示从 Prometheus 收集到的数据。

  • 登录 Grafana。

  • 转到 Configuration -> Data Sources,选择 Prometheus,配置 Prometheus 的 URL。

  • 创建新的仪表板,使用 Prometheus 作为数据源,添加面板来展示指标数据。

总结

  1. 创建监控中心微服务:配置 Actuator 和 Prometheus。

  2. 配置服务发现:通过 Nacos 动态发现微服务。

  3. 实现服务监控功能:使用 Feign 客户端抓取 Actuator 数据,定期更新 Prometheus。

  4. 配置 Prometheus 和 Grafana:使 Prometheus 抓取监控中心的数据,并在 Grafana 中展示这些数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值