亿级流量点赞系统第八章
项目地址
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
成功执行

✨Redis指标监控
用Redis Exporter监控reids
▼shell复制代码docker run --name redis-exporter \ -p 9121:9121 \ oliver006/redis_exporter \ --redis.addr=redis://IP:6379 \ --redis.password=pwd
执行成功

✨安装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
安装成功

✨启动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即可访问

💥配置Prometheus
选择Prometheus

💥连接数据源
连接成功

💥配置各项指标
进入控制面板

给出一些常用配置:
- QPS
▼shell复制代码sum(rate(http_server_requests_seconds_count[1m]))
由于是在公网部署的prometheus.yml中定义的biz连接不了本地,因此上面的数据面板是空的,当部署上线后即可看到。

本章总结
搭建 Granfana 作为数据展示平台,应用 Prometheus 作为可观测性解决方案。
项目踩坑
待补充
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
