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

网站建设和管理如何制作一个自己的网站

网站建设和管理,如何制作一个自己的网站,阎良网站建设,网站建设的结尾下面是一个完整的 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/213945.html

相关文章:

  • 网站怎么做翻页江门关键词排名工具
  • 怎么做音乐mp3下载网站抖音指数查询
  • 设计工作室装修效果图seo和sem的联系
  • 做网站用什么平台某产品网络营销推广方案
  • 招聘网站建设方案模板网站建设优化的技巧
  • 可以下载各种软件的网站google seo实战教程
  • 青岛做公司网站的多吗网推平台
  • 怎么样给公司做网站草根seo博客
  • 部标平台软件网站开发sem百度竞价推广
  • 网站建设找什么公司免费seo视频教程
  • 做网站需要做优化吗深圳百度推广
  • 免费自建手机网站百度网盘app下载安装 官方下载
  • 网站建设课程简介乐陵市seo关键词优化
  • 电子商务网站建设成都个人发布信息免费推广平台
  • 房地产网站广告销售怎么做营销管理
  • 东莞齐诺做网站上海百度推广方案
  • 网站建设安全吗东莞营销网站建设推广
  • 做网站应该学什么专业搜狗网站收录提交入口
  • 网站建设竞价托管外包做网站需要什么技术
  • 网站改版合同可以免费做网站推广的平台
  • 网站开发工单网站建设小程序开发
  • 网站服务器是注册域名平台吗站群seo
  • 艾米艾园wordpress商丘seo公司
  • 网站制作网站建设网络营销方法有哪几种
  • 时时彩怎么做网站上海百度公司地址
  • 郑州手机网站制作公司哪家好网站开发步骤
  • 网站建设交互效果seo精准培训课程
  • 电子商务网站建设 大纲巨量引擎广告投放平台代理
  • 南阳网站seo顾问投诉百度最有效的电话
  • 遵义做网站的网络公司十大基本营销方式