0


快速搭建SpringBoot3+Prometheus+Grafana

快速搭建SpringBoot3+Prometheus+Grafana

一、搭建SpringBoot项目

1.1 创建SpringBoot项目

image-20241023112543699

1.2 修改pom文件配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.3.4</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.javagpt</groupId>
  12. <artifactId>prometheus</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>prometheus</name>
  15. <description>prometheus</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <!-- spring boot -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>jakarta.json</groupId>
  27. <artifactId>jakarta.json-api</artifactId>
  28. <version>${jakarta-json.version}</version>
  29. </dependency>
  30. <!-- spring validation -->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-validation</artifactId>
  34. </dependency>
  35. <!-- mysql-->
  36. <dependency>
  37. <groupId>mysql</groupId>
  38. <artifactId>mysql-connector-java</artifactId>
  39. <version>8.0.31</version>
  40. <scope>runtime</scope>
  41. </dependency>
  42. <!-- druid -->
  43. <dependency>
  44. <groupId>com.alibaba</groupId>
  45. <artifactId>druid-spring-boot-starter</artifactId>
  46. <version>1.2.23</version>
  47. </dependency>
  48. <!-- lombok -->
  49. <dependency>
  50. <groupId>org.projectlombok</groupId>
  51. <artifactId>lombok</artifactId>
  52. <optional>true</optional>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-actuator</artifactId>
  57. </dependency>
  58. <!-- mybatis plus -->
  59. <dependency>
  60. <groupId>com.baomidou</groupId>
  61. <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
  62. <version>3.5.8</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>io.micrometer</groupId>
  66. <artifactId>micrometer-registry-prometheus</artifactId>
  67. <version>1.13.6</version>
  68. </dependency>
  69. </dependencies>
  70. <build>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. <configuration>
  76. <excludes>
  77. <exclude>
  78. <groupId>org.projectlombok</groupId>
  79. <artifactId>lombok</artifactId>
  80. </exclude>
  81. </excludes>
  82. </configuration>
  83. </plugin>
  84. </plugins>
  85. </build>
  86. </project>

1.3 创建controller+service+mapper三层文件

image-20241023112650341

1.3.1 controller
  1. ArticleServiceImpl.java
  1. importio.micrometer.core.annotation.Counted;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/**
  2. * @BelongsProject: prometheus
  3. * @BelongsPackage: com.javagpt.prometheus.controller
  4. * @Author: JavaGPT
  5. * @CreateTime: 2024-10-23 00:01
  6. * @Description:
  7. * @Version: 1.0
  8. */@RestController@RequestMapping("/article")publicclass c {@Counted(value ="article.get", extraTags ="get", description ="test")@GetMappingpublicStringindex(){return"Hello World";}}
1.3.2 service
  1. ArticleService.java
  1. importcom.javagpt.prometheus.entity.ArticleEntity;importcom.baomidou.mybatisplus.extension.service.IService;/**
  2. * @author 26314
  3. * @description 针对表【tb_article】的数据库操作Service
  4. * @createDate 2024-10-22 23:59:42
  5. */publicinterfaceArticleServiceextendsIService<ArticleEntity>{}
  1. ArticleServiceImpl.java
  1. importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;importcom.javagpt.prometheus.entity.ArticleEntity;importcom.javagpt.prometheus.service.ArticleService;importcom.javagpt.prometheus.mapper.ArticleMapper;importorg.springframework.stereotype.Service;/**
  2. * @author 26314
  3. * @description 针对表【tb_article】的数据库操作Service实现
  4. * @createDate 2024-10-22 23:59:42
  5. */@ServicepublicclassArticleServiceImplextendsServiceImpl<ArticleMapper,ArticleEntity>implementsArticleService{}
1.3.3 mapper
  1. ArticleMapper.java
  1. importcom.javagpt.prometheus.entity.ArticleEntity;importcom.baomidou.mybatisplus.core.mapper.BaseMapper;importorg.apache.ibatis.annotations.Mapper;/**
  2. * @author 26314
  3. * @description 针对表【tb_article】的数据库操作Mapper
  4. * @createDate 2024-10-22 23:59:42
  5. * @Entity com.javagpt.prometheus.entity.ArticleEntity
  6. */@MapperpublicinterfaceArticleMapperextendsBaseMapper<ArticleEntity>{}
1.3.4 mapper.xml
  1. ArticleMapper.xml
  1. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.javagpt.prometheus.mapper.ArticleMapper"><resultMapid="BaseResultMap"type="com.javagpt.prometheus.entity.ArticleEntity"><idproperty="id"column="id"jdbcType="INTEGER"/><resultproperty="webId"column="web_id"jdbcType="VARCHAR"/><resultproperty="articleId"column="article_id"jdbcType="VARCHAR"/><resultproperty="author"column="author"jdbcType="VARCHAR"/><resultproperty="title"column="title"jdbcType="VARCHAR"/><resultproperty="abstractContent"column="abstract_content"jdbcType="VARCHAR"/><resultproperty="content"column="content"jdbcType="VARCHAR"/><resultproperty="tag"column="tag"jdbcType="VARCHAR"/><resultproperty="url"column="url"jdbcType="VARCHAR"/><resultproperty="publishTime"column="publish_time"jdbcType="TIMESTAMP"/><resultproperty="commentCount"column="comment_count"jdbcType="INTEGER"/><resultproperty="likeCount"column="like_count"jdbcType="INTEGER"/><resultproperty="readCount"column="read_count"jdbcType="INTEGER"/><resultproperty="createTime"column="create_time"jdbcType="TIMESTAMP"/><resultproperty="creator"column="creator"jdbcType="VARCHAR"/><resultproperty="updateTime"column="update_time"jdbcType="TIMESTAMP"/><resultproperty="updater"column="updater"jdbcType="VARCHAR"/><resultproperty="remark"column="remark"jdbcType="VARCHAR"/></resultMap><sqlid="Base_Column_List">
  2. id,web_id,article_id,
  3. author,title,abstract_content,
  4. content,tag,url,
  5. publish_time,comment_count,like_count,
  6. read_count,create_time,creator,
  7. update_time,updater,remark
  8. </sql></mapper>

1.4 修改application.yml 文件

  1. spring:application:name: prometheus
  2. # mysql??datasource:type: com.alibaba.druid.pool.DruidDataSource
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://106.55.xx.xx:3306/blog?serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=trueusername: root
  5. password: root
  6. management:endpoints:enabled-by-default:true#暴露所有端点信息web:exposure:include:'*'#以web方式暴露server:servlet:context-path: /prometheus
  7. port:8080# mybatisPlus配置mybatis-plus:# mapper映射地址mapper-locations: classpath:mapper/*.xml# 实体类扫描包路径type-aliases-package: com.javagpt.prometheus.entity
  8. configuration:# sql打印log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  9. # 开启驼峰命名map-underscore-to-camel-case:trueglobal-config:db-config:# 数据库表前缀table-prefix: t_

1.5 一键启动

  1. Connected to the target VM, address: '127.0.0.1:5815', transport: 'socket'
  2. . ____ _ __ _ _
  3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  6. ' |____| .__|_| |_|_| |_\__, | / / / /
  7. =========|_|==============|___/=/_/_/_/
  8. :: Spring Boot :: (v3.3.4)
  9. 2024-10-23T08:52:37.974+08:00 INFO 33412 --- [prometheus] [ main] c.j.prometheus.PrometheusApplication : Starting PrometheusApplication using Java 17.0.8 with PID 33412 (D:\develop\java_develop\springboot\prometheus\target\classes started by 26314 in D:\develop\java_develop\springboot\prometheus)
  10. 2024-10-23T08:52:37.976+08:00 INFO 33412 --- [prometheus] [ main] c.j.prometheus.PrometheusApplication : No active profile set, falling back to 1 default profile: "default"
  11. 2024-10-23T08:52:38.809+08:00 INFO 33412 --- [prometheus] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
  12. 2024-10-23T08:52:38.817+08:00 INFO 33412 --- [prometheus] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  13. 2024-10-23T08:52:38.818+08:00 INFO 33412 --- [prometheus] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.30]
  14. 2024-10-23T08:52:38.855+08:00 INFO 33412 --- [prometheus] [ main] o.a.c.c.C.[.[localhost].[/prometheus] : Initializing Spring embedded WebApplicationContext
  15. 2024-10-23T08:52:38.855+08:00 INFO 33412 --- [prometheus] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 831 ms
  16. Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
  17. Get /192.168.216.1 network interface
  18. Get network interface info: name:eth9 (VMware Virtual Ethernet Adapter for VMnet8)
  19. Initialization Sequence datacenterId:0 workerId:5
  20. _ _ |_ _ _|_. ___ _ | _
  21. | | |\/|_)(_| | |_\ |_)||_|_\
  22. / |
  23. 3.5.8
  24. 2024-10-23T08:52:39.626+08:00 INFO 33412 --- [prometheus] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 16 endpoints beneath base path '/actuator'
  25. 2024-10-23T08:52:39.681+08:00 INFO 33412 --- [prometheus] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/prometheus'
  26. 2024-10-23T08:52:39.690+08:00 INFO 33412 --- [prometheus] [ main] c.j.prometheus.PrometheusApplication : Started PrometheusApplication in 2.003 seconds (process running for 2.618)
  27. 2024-10-23T08:52:39.869+08:00 INFO 33412 --- [prometheus] [nio-8080-exec-1] o.a.c.c.C.[.[localhost].[/prometheus] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  28. 2024-10-23T08:52:39.869+08:00 INFO 33412 --- [prometheus] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  29. 2024-10-23T08:52:39.869+08:00 INFO 33412 --- [prometheus] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
  30. 2024-10-23T08:52:40.332+08:00 INFO 33412 --- [prometheus] [)-192.168.82.82] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited

1.6 查看默认指标信息

本地访问 http://localhost:8080/prometheus/actuator/prometheus

image-20241023112224311

二、搭建Prometheus

到官网下载最新版 https://prometheus.io/download/

image-20241023122219263

修改

  1. Prometheus.yml

文件

  1. # my global configglobal:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:alertmanagers:-static_configs:-targets:# - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:# - "first_rules.yml"# - "second_rules.yml"# 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"metrics_path:'/prometheus/actuator/prometheus'#指定抓取的路径static_configs:-targets:['127.0.0.1:8080']labels:nodename:'app-demo'# metrics_path defaults to '/metrics'# scheme defaults to 'http'.# - targets: ["localhost:9090"]

CMD 进入命令行启动

  1. prometheus.exe

image-20241023122348405

三、安装Grafana

3.1 下载

去官网下载最新版Garafana https://grafana.org.cn/grafana/download?platform=windows

image-20241023122637575

3.2 安装

解压之后进入CMD命令行启动

  1. grafana-server.exe

3.3 配置数据源

进入http://localhost:3000 登录,初始账号和密码都是admin,然后找到Data Source,进行如下操作,填好之后点击确认。

image-20241023122855314

3.4 配置SpringBoot程序仪表盘模板

先进入Grafana官网寻找合适的SpringBoot程序模板 https://grafana.com/grafana/dashboards/?search=springboot ,然后将模板ID复制下来。

image-20241023123226041

image-20241023123140615

image-20241023123307746

image-20241023123327341

至此,整个SpringBoot3+Prometheus+Grafana就已经完成搭建啦。

让我们来看看最终的效果图吧。

image-20241023123429133

写在最后

编程精选网(www.codehuber.com),程序员的终身学习网站已上线!

如果这篇【文章】有帮助到你,希望可以给【JavaGPT】点个赞👍,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【JavaGPT】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💝💝💝!


本文转载自: https://blog.csdn.net/weixin_46294086/article/details/143181066
版权归原作者 JavaGPT 所有, 如有侵权,请联系我们删除。

“快速搭建SpringBoot3+Prometheus+Grafana”的评论:

还没有评论