当前位置: 首页 > wzjs >正文

宁波房产信息网官方网站win10系统优化软件

宁波房产信息网官方网站,win10系统优化软件,免费静态网页托管,深圳网站开发是什么下面是一个完整的 Spring Boot 项目示例,集成 Prometheus 和 Grafana 进行性能监控,包括: Prometheus 作为监控数据收集工具Micrometer 作为指标采集库Grafana 作为可视化展示工具 步骤概览 引入依赖:在 pom.xml 中添加 Promet…

下面是一个完整的 Spring Boot 项目示例,集成 Prometheus 和 Grafana 进行性能监控,包括:

  • Prometheus 作为监控数据收集工具
  • Micrometer 作为指标采集库
  • Grafana 作为可视化展示工具

步骤概览

  1. 引入依赖:在 pom.xml 中添加 Prometheus 和 Actuator 相关依赖。
  2. 配置 Spring Boot:启用 Actuator 并暴露 Prometheus 端点。
  3. 编写示例代码:使用 Micrometer 记录应用性能数据。
  4. 配置 Prometheus:拉取 Spring Boot 端点的数据。
  5. 配置 Grafana:可视化 Prometheus 数据。

1. 添加 Maven 依赖

pom.xml 文件中加入:

<dependencies><!-- Spring Boot Actuator 监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Micrometer Prometheus 监控支持 --><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency>
</dependencies>

2. 配置 application.yml

server:port: 8080management:endpoints:web:exposure:include: "prometheus, health, info, metrics"metrics:export:prometheus:enabled: true

此配置:

  • 允许 Actuator 通过 http://localhost:8080/actuator/prometheus 公开 Prometheus 监控数据。
  • 允许 /actuator/health/actuator/info 等端点。

3. 编写 Spring Boot 监控代码

创建 MonitorController.java,提供测试接口并记录自定义监控指标:

package com.example.monitoring.controller;import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.Duration;
import java.util.Random;@RestController
public class MonitorController {private final Counter requestCounter;private final Timer responseTimer;private final Random random = new Random();public MonitorController(MeterRegistry registry) {this.requestCounter = Counter.builder("api_requests_total").description("Total API Requests").register(registry);this.responseTimer = Timer.builder("api_response_time").description("API Response Time").register(registry);}@GetMapping("/hello")public String hello(@RequestParam(defaultValue = "World") String name) {requestCounter.increment(); // 记录请求数return responseTimer.record(() -> {  // 记录执行时间try {Thread.sleep(random.nextInt(500)); // 模拟延迟} catch (InterruptedException ignored) {}return "Hello, " + name;});}
}
  • api_requests_total:记录 /hello 接口的总访问量。
  • api_response_time:记录 /hello 的执行时间。

4. 启动 Prometheus

配置 prometheus.yml

prometheus.yml 添加:

global:scrape_interval: 5s # 每 5 秒抓取数据scrape_configs:- job_name: 'spring-boot-app'metrics_path: '/actuator/prometheus'static_configs:- targets: ['host.docker.internal:8080']  # 或者改成 'localhost:8080' 取决于你的运行环境

然后运行 Prometheus:

./prometheus --config.file=prometheus.yml

访问 http://localhost:9090,进入 Prometheus 界面,查询 api_requests_total 指标。

5. 启动 Grafana

配置数据源

  1. 访问 http://localhost:3000/(默认账号 admin/admin)。
  2. 添加数据源,选择 Prometheus
  3. 设置 http://localhost:9090 作为数据源地址。
  4. 保存后,在 Explore 面板查询 api_requests_totalapi_response_time

总结

  1. Spring Boot 通过 Actuator 和 Micrometer 采集监控数据。
  2. Prometheus/actuator/prometheus 端点拉取数据。
  3. Grafana 通过 Prometheus 进行数据可视化。

这样,你就成功在 Spring Boot 项目中集成了 Prometheus 和 Grafana 进行性能监控! 🚀

http://www.dtcms.com/wzjs/10896.html

相关文章:

  • logo设计多少钱seo优化搜索推广
  • 怎么把个人做的网站上传到网上附近广告公司
  • 新手去哪个网站做翻译如何在百度投放广告
  • 简述建设一个网站的具体步骤自媒体平台排名前十
  • wordpress博客好吗seo排名软件
  • 天汇大厦网站建设公司百度账户安全中心
  • 服装公司网站网页设计亚马逊关键词排名提升
  • 虎门有没有做网站公司网页链接制作生成
  • 做网上任务赚钱的网站网址注册
  • 网站建设四个步骤泉州百度推广咨询
  • 阿里云做网站号码短视频推广
  • 建设政府网站多少钱国内十大软件培训机构
  • 新洲建设投标网站怎么推广网址
  • 香港旅游网站开发百度一下 你知道首页
  • 做网站怎么买服务器有广告位怎么找广告商
  • 宜昌网站推广优化技巧关键词优化精灵
  • 自学电脑做网站月入过万上海比较好的seo公司
  • 台州网站建设网站推广推广平台排名
  • 外网服务器优化设计答案
  • 手赚网站哪里可以做seo优化标题
  • 深圳营销型企业网站网络营销方式
  • 做百度网站分录公司做个网站多少钱
  • html仿淘宝首页电子商务网站首页如何做网络销售平台
  • 网站建设前的分析推广方式有哪些
  • 贵阳网站建设培训学校百度搜索引擎营销
  • 都网站建设马鞍山seo
  • 河南做网站公司排名排名优化系统
  • 专做韩餐网站佛山网站优化排名推广
  • 电商网站建西安百度推广优化
  • 兰州网站优化服务seo排名赚挂机