亿级流量点赞系统第八章

项目地址

GitHub地址💥:https://github.com/tenyon61/likeboom

后端初始化模板💥:https://github.com/tenyon61/springboot3-demo/tree/single

快速开始

✨引入依赖

xml
复制代码
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
yaml
复制代码
management: endpoints: web: exposure: include: health, prometheus metrics: distribution: percentiles: http: server: requests: 0.5, 0.75, 0.9, 0.95, 0.99

✨代码改造

ThumbController.java

java
复制代码
private final Counter successCounter; private final Counter failureCounter; public ThumbController(MeterRegistry registry) { this.successCounter = Counter.builder("thumb.success.count") .description("Total sucessful thumb") .register(registry); this.failureCounter = Counter.builder("thumb.failure.count") .description("Total failed thumb") .register(registry); }

✨重启TiDB

执行命令

shell
复制代码
tiup playground --tag thumb --without-monitor --db.host 0.0.0.0

成功执行

2025-04-29-0aa09c.webp

✨Redis指标监控

Redis Exporter监控reids

shell
复制代码
docker run --name redis-exporter \ -p 9121:9121 \ oliver006/redis_exporter \ --redis.addr=redis://IP:6379 \ --redis.password=pwd

执行成功

2025-04-29-5bd9e2.webp

✨安装Prometheus

创建prometheus.yml,放到/opt/prometheus下面

yaml
复制代码
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'biz' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s metrics_path: '/api/actuator/prometheus' static_configs: - targets: ['IP:9199'] labels: group: 'thumb' - job_name: 'redis' scrape_interval: 5s static_configs: - targets: ['IP:9121']

执行启动命令

shell
复制代码
docker run -d \ --name prometheus \ -p 9199:9090 \ -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus

安装成功

2025-04-29-9e1fb6.webp

✨启动Grafana

shell
复制代码
docker run -d -p 3000:3000 --name=grafana \ --user "$(id -u)" \ --volume "$PWD/data:/var/lib/grafana" \ grafana/grafana-enterprise

访问IP:3000admin/admin即可访问

2025-04-29-6ba9af.webp

💥配置Prometheus

选择Prometheus

2025-04-29-aa8c1a.webp

💥连接数据源

连接成功

2025-04-29-7516a2.webp

💥配置各项指标

进入控制面板

2025-04-29-2db4e1.webp

给出一些常用配置:

  • QPS
shell
复制代码
sum(rate(http_server_requests_seconds_count[1m]))

由于是在公网部署的prometheus.yml中定义的biz连接不了本地,因此上面的数据面板是空的,当部署上线后即可看到。

2025-05-01-65f29a.webp

本章总结

搭建 Granfana 作为数据展示平台,应用 Prometheus 作为可观测性解决方案。

项目踩坑

待补充

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
下载 APP