应用监控(Prometheus + Grafana)

可用于应用监控的系统有很多,有的需要埋点(切面)、有的需要配置Agent(字节码增强)。现在使用另外一个监控系统 —— Grafana。

Grafana 监控面板

 这套监控主要用到了 SpringBoot Actuator + Prometheus + Grafana 三个模块组合的起来使用的监控。非常轻量好扩展使用。

  1. Actuator - 数据上报、Prometheus - 数据采集、Grafana - 数据展示

  2. 本章节的内容主要为代码中的配置和监控的配置。

环境配置

 本节所需的监控配置,已经放到了 chatgpt-data 的 dev-ops 包下了。你只需要确保本地或者云服务器已经安装了Docker,那么就可以执行安装了。

1. Grafana.ini

这一部分是小傅哥通过第一次默认安装后,再通过 docker 脚本 docker container cp grafana:/etc/grafana/ ./docs/dev-ops/ 从容器中拷贝下来的配置。因为我们需要做一些默认的配置处理。

端口修改

# The http port to use

http_port = 4000

  1. Grafana 默认配置的是 3000 端口,但这个端口很多时候都被占用了。所以如果你的也占用了,那么可以在这里修改下。

连接配置

[database]

# You can configure the database connection by specifying type, host, name, user and password

# as separate properties or as on string using the url properties.

# Either "mysql", "postgres" or "sqlite3", it's your choice

type = mysql

host = host.docker.internal:3306

name = grafana

user = root

# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""

password = 123456

  1. 为了让 Grafana 的配置具有迁移性,也不至于删除在安装就丢失配置,那么这里可以选择配置数据库进行使用。

  2. 注意;你需要先在本地安装MySQL以及创建出一个grafana数据库。—— 连接后,会自动建表。

注意:host那里设置localhost或者ipv4地址都连接失败,设置host.docker.internal成功,目前不知道原因

2. datasource.yml

apiVersion: 1

datasources:

- name: Prometheus

type: prometheus

access: proxy

url: http://prometheus:9090

isDefault: true

  1. 注意;因为 Grafana 使用的是 Prometheus 数据源,所以你需要在这里配置上。当然也可以不配置,在启动的 Grafana 线上进行配置。

3. prometheus.yml

global:

scrape_interval: 15s

scrape_configs:

- job_name: 'x-api-app'

metrics_path: '/actuator/prometheus'

static_configs:

- targets: [ '192.168.158.77:8080' ]

  1. 这里配置的是 prometheus.yml 对需要采集的 SpringBoot 应用访问地址。注意你需要替换为你的服务器IP和服务端口。

应用配置

POM 配置

chatgpt-data-app 模块下

<!-- 监控;actuator-上报、prometheus-采集、grafana-展示 -->
<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.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
  1. actuator、prometheus 是监控所需的内容,aspectjweaver 是本节需要使用 prometheus 添加自定义的埋点,而这个会用到切面。

chatgpt-data-trigger 模块下

<!-- 监控;actuator-上报、prometheus-采集、grafana-展示 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

代码配置

1. 启动监听服务

@EnableAspectJAutoProxy
@Configuration
public class PrometheusConfiguration {

    @Bean
    public CollectorRegistry collectorRegistry() {
        return new CollectorRegistry();
    }

    @Bean
    public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig config, CollectorRegistry collectorRegistry) {
        return new PrometheusMeterRegistry(config, collectorRegistry, Clock.SYSTEM);
    }

    @Bean
    public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
    }

    @Bean
    public CountedAspect countedAspect(MeterRegistry registry) {
        return new CountedAspect(registry);
    }

}

2. 自定义监控埋点

@Timed(value = "no_pay_notify_order_job", description = "定时任务,订单支付状态更新")
    @Scheduled(cron = "0/3 * * * * ?")
    public void exec() {
    // ...
    }

  1. 你可以使用监控提供的注解,来对需要监控的方法进行埋点。@Timed 这样就可以采集到数据,在监控中配置了。

3. YML 配置

# 监控
management:
  endpoints:
    web:
      exposure:
        include: "*" # 暴露所有端点,包括自定义端点
  endpoint:
    metrics:
      enabled: true
    health:
      show-details: always # 显示详细的健康检查信息
  metrics:
    export:
      prometheus:
        enabled: true # 启用Prometheus
  prometheus:
    enabled: true # 启用Prometheus端点
  jmx:
    enabled: true # 启用JMX监控
  system:
    cpu:
      enabled: true # 启用CPU监控
    memory:
      enabled: true # 启用内存监控

 可以访问 http://127.0.0.1:9090/service-discovery?search= - 查看采集数据。

监控配置

地址:http://127.0.0.1:4000/dashboards

介绍:Grafana 的监控,需要新建监控仪表。也可以使用导入功能。导入功能可以导入 Grafana 官网提供的各项模板,非常好用。

**常用代码**

4.1 CPU

sum(system_cpu_usage{job="x-api-app"}) / sum(system_cpu_count{job="x-api-app"}) * 100

4.2 磁盘

disk_free_bytes{job="x-api-app"}

4.3 连接池

hikaricp_connections{pool="HikariPool-1", job="x-api-app"}

4.4 请求量

sum by(instance, uri, exception) (increase(no_pay_notify_order_job_seconds_count{method="exec", job="x-api-app"}[5m]))

4.5 响应时间

sum by (uri) (increase(http_server_requests_seconds_sum{uri=~"/api/v1/chatgpt/chat/completions"}[1m]))

/sum by (uri) (increase(http_server_requests_seconds_count{uri=~"/api/v1/chatgpt/chat/completions"}[1m]))

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值