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

给自己的网站起名字中美关系最新消息

给自己的网站起名字,中美关系最新消息,免费制作软件,海珠b2b网站建设公司📣前言 随着云原生技术的发展,监控和度量也成为了不可或缺的一部分。Prometheus 是一款最近比较流行的开源时间序列数据库,同时也是一种监控方案。它具有极其灵活的查询语言、自身的数据采集和存储机制以及易于集成的特点。而 Spring Boot 是…

📣前言

  随着云原生技术的发展,监控和度量也成为了不可或缺的一部分。Prometheus 是一款最近比较流行的开源时间序列数据库,同时也是一种监控方案。它具有极其灵活的查询语言、自身的数据采集和存储机制以及易于集成的特点。而 Spring Boot 是一款快速构建应用的框架,其提供了大量自动化的配置和功能,使得开发者可以更加专注于业务逻辑的开发,而不必关心大量的配置和环境搭建。

  本文将介绍 Spring Boot 如何集成 Prometheus 进行应用监控,并结合实际应用场景,给出一些使用 Prometheus 监控应用的案例,以及优缺点分析和测试用例。

  那么,具体如何实现呢?这将又会是干货满满的一期,全程无尿点不废话只抓重点教,具有非常好的学习效果,拿好小板凳准备就坐!希望学习的过程中大家认真听好好学,学习的途中有任何不清楚或疑问的地方皆可评论区留言或私信,bug菌将第一时间给予解惑,那么废话不多说,直接开整!Fighting!!

🌊环境说明

开发工具:IDEA 2021.3
JDK版本: JDK 1.8
Spring Boot版本:2.3.1 RELEASE
Maven版本:3.8.2


🏆本文收录于《Spring Boot从入门到精通》,专门攻坚指数提升,2023 年国内最系统+最强(更新中)。

本专栏致力打造最硬核 Spring Boot 从零基础到进阶系列学习内容,🚀均为全网独家首发,打造精品专栏,专栏持续更新中…欢迎大家订阅持续学习。 如果想快速定位学习,可以看这篇【SpringBoot教程导航帖】,你想学习的都被收集在内,快速投入学习!!两不误。


🌊摘要

本文主要介绍如何在 Spring Boot 中集成 Prometheus 进行应用监控,重点讲解以下内容:

  • Prometheus 的概述和优缺点分析
  • Spring Boot 集成 Prometheus 的教学
  • 应用场景案例
  • 实战教学
  • 测试用例
  • 全文小结和结尾

🌊正文


Prometheus 是什么?

Prometheus 是一个开源的系统监控和警报工具包。它实现了完整的数据采集、存储和查询功能,可以帮助开发者对应用的性能和状态进行监控,并提供丰富的查询语言和可视化界面。

Prometheus 具有以下特点:

  • 支持多种数据采集方式,包括 HTTP、JMX、Redis、MySQL、RabbitMQ 等。
  • 灵活的数据查询和展现方式。
  • 方便的告警功能。
  • 模块化的设计,易于扩展和集成。

Spring Boot 是什么?

Spring Boot 是一个快速构建应用程序的框架,它基于 Spring 框架,简化了 Spring 应用的搭建和开发。Spring Boot 提供了自动化的配置和功能,减少了开发者在配置上花费的时间和精力,使得开发人员可以专注于业务逻辑的实现。

Spring Boot 具有以下特点:

  • 快速构建应用。
  • 提供了自动化配置,最大限度减少了配置的工作量。
  • 开发者友好,提供了灵活的扩展和集成方式。

搭建Spring Boot应用

  首先,我们先创建个基础的Spring Boot项目,如果还不会点这里,此处就不详细赘述啦。

Spring Boot 集成 Prometheus

导入 Prometheus 相关依赖

在 Spring Boot 中使用 Prometheus,需要导入以下两个依赖:

<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId><version>0.5.0</version>
</dependency>
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_pushgateway</artifactId><version>0.5.0</version>
</dependency>

  simpleclient_spring_boot 是 Prometheus 提供的 Spring Boot 集成包,用于集成 Spring Boot 应用。simpleclient_pushgateway 是用于将采集到的数据推送到 Pushgateway。

配置 Prometheus

在 application.properties 中添加 Prometheus 的配置:

# 启用 Prometheus 进行监控
management.endpoint.metrics.enabled=true
# 设置采集间隔时间,单位为秒,这里设置为 10 秒
management.metrics.export.prometheus.step=10s

这里设置了采集数据的间隔时间为 10 秒。

定义 Controller

在 Controller 中添加用于测试的接口,用于生成一些测试数据:

@RestController
public class TestController {private static final Random random = new Random();@GetMapping("/test")public String test() {// 模拟一些业务逻辑,比如查询数据库、计算等。try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}// 返回一些测试数据return "Hello World!";}}

监控应用

  启动 Spring Boot 应用后,访问 http://localhost:8080/actuator/prometheus 即可查看采集到的数据。

例如,可以查看应用的请求数、请求处理时间、内存使用情况等。

应用场景案例

监控数据库连接池

  数据库连接池是应用中非常重要的资源,过多的连接池占用会导致应用的性能下降,因此需要对其进行监控。在 Spring Boot 应用中可以通过以下方式对数据库连接池进行监控:

  1. 定义一个 health indicator,用于检测连接池的健康状况。
@Component
public class DataSourceHealthIndicator implements HealthIndicator {@Autowiredprivate DataSource dataSource;@Overridepublic Health health() {try (Connection connection = dataSource.getConnection()) {// 如果可以获取到连接,则认为连接池正常return Health.up().build();} catch (Exception e) {// 如果出现异常,则表示连接池不正常return Health.down().withException(e).build();}}}
  1. 配置 Prometheus 进行监控

在 application.properties 中添加以下配置:

# 启用 Prometheus 进行监控
management.endpoint.metrics.enabled=true
# 设置采集间隔时间,单位为秒,这里设置为 10 秒
management.metrics.export.prometheus.step=10s

监控应用日志

  应用的日志是了解应用运行情况的重要依据,现在有一些日志组件可以与 Prometheus 进行集成,例如 logback-prometheus-appender,其可以将日志输出到 Prometheus 的指标中,方便进行监控。

  在 Spring Boot 应用中可以使用以下方式与 logback-prometheus-appender 集成:

  1. 导入 logback-prometheus-appender 依赖:
<dependency><groupId>io.github.microutils</groupId><artifactId>logback-prometheus-appender</artifactId><version>1.1.0</version>
</dependency>
  1. 在 logback.xml 中添加 appender 和 logger:
<appender name="PROMETHEUS" class="io.github.microutils.vertx.prometheus.LogbackPrometheusAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%rEx</pattern></encoder>
</appender><logger name="io.github.microutils.vertx.prometheus" level="INFO"><appender-ref ref="PROMETHEUS"/>
</logger>

  这里定义了一个名为 PROMETHEUS 的 appender,并配置了 encoder 和 logger,将日志输出到 Prometheus 的指标中。

实战教学

项目结构

  本例中,我们将构建一个简单的 Spring Boot 项目,用于演示如何集成 Prometheus 进行应用监控。项目结构如下:

├── pom.xml
└── src└── main├── java│   └── io│       └── github│           └── example│               ├── ExampleApplication.java│               └── TestController.java└── resources└── application.properties
  • ExampleApplication:Spring Boot 应用的启动类。
  • TestController:用于生成一些测试数据的 Controller。
  • application.properties:用于配置 Prometheus 和 Spring Boot 应用的属性。

导入依赖

我们需要在 pom.xml 中添加以下依赖:

<dependencies><!-- Spring Boot Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.5.6</version></dependency><!-- Prometheus Simple Client for Spring Boot --><dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId><version>0.10.0</version></dependency>
</dependencies>

  这里我们引入了 Spring Boot Web Starter 和 Prometheus Simple Client for Spring Boot 两个依赖。

配置 Prometheus

在 application.properties 中添加以下内容:

# Prometheus 监控端口
management.server.port=8081
# Prometheus 监控路径
management.endpoints.web.base-path=/actuator/prometheus
# 开启 Micrometer 的 Prometheus 支持
management.metrics.export.prometheus.enabled=true

  这里,我们将 management.server.port 设置为 8081,代表 Prometheus 监控的端口号。management.endpoints.web.base-path 是管理端点的基础路径,用于提供 Prometheus 监控 API。最后,我们开启了 Micrometer 的 Prometheus 支持,这是实现 Spring Boot 应用监控的必要配置。

编写代码

ExampleApplication.java 中添加以下代码:

@SpringBootApplication
public class ExampleApplication {public static void main(String[] args) {SpringApplication.run(ExampleApplication.class, args);}@Beanpublic CollectorRegistry collectorRegistry() {return CollectorRegistry.defaultRegistry;}
}

  这里,我们启用了 Spring Boot 应用,并创建了一个 CollectorRegistry bean。这个 bean 将与 Prometheus 一起使用,以暴露应用程序的度量指标。

接下来,在 TestController.java 中添加以下代码:

@RestController
public class TestController {private final Random random = new Random();@Autowiredprivate CollectorRegistry collectorRegistry;@RequestMapping("/test")public String test() throws InterruptedException {// 模拟请求处理Thread.sleep(random.nextInt(500));// 增加指标值Counter.builder("example_requests_total").register(collectorRegistry).inc();return "Test completed";}
}

  这里,我们创建了一个 REST 控制器,包含 /test 端点。在这个端点中,我们模拟了请求处理,并增加了 example_requests_total 计数器的值。

运行和测试

现在,我们可以运行应用程序并进行测试了。

首先,启动应用程序:

mvn spring-boot:run

  然后在浏览器中访问 http://localhost:8080/test 。每次访问 /test 端点时,计数器 example_requests_total 将增加一个值。

  接下来,我们使用 Prometheus 来收集应用程序度量指标。在 Prometheus 的配置文件中,添加以下内容:

scrape_configs:- job_name: 'example-app'metrics_path: '/actuator/prometheus'static_configs:- targets: ['localhost:8081']

  最后,访问 http://localhost:9090/graph ,在 Query 输入框中输入 example_requests_total,即可看到计数器的值。

小结

  本文介绍了如何在 Spring Boot 应用中集成 Prometheus 进行应用监控,重点讲解了 Prometheus 的概述、Spring Boot 集成 Prometheus 的教学、应用场景案例、实战教学和测试用例。在实践中,我们可以使用 Prometheus 监控数据库连接池、应用日志、文件上传、定时任务等多种场景,通过灵活配置和扩展,帮助我们更好地了解应用的性能和状态。

总结

  Prometheus 是一款开源的系统监控和警报工具包,其具有可扩展性、灵活的数据查询和展现方式、方便的告警功能和模块化的设计等优点。Spring Boot 是一款快速构建应用程序的框架,其提供了自动化的配置和功能,减少了开发者在配置上花费的时间和精力。

  在 Spring Boot 应用中集成 Prometheus,需要导入 Prometheus 相关依赖,配置 Prometheus 和定义 Controller,以便实现数据采集和监控。同时,我们可以根据具体的应用场景,使用不同的方法和工具,对应用的性能和状态进行监控和调优。

  通过本文的介绍和实践,相信大家已经掌握了如何在 Spring Boot 应用中集成 Prometheus 进行应用监控的方法和技巧。希望大家能够灵活运用这些知识,提升应用的性能和稳定性,为业务发展提供有力的保障。

… …

  ok,以上就是我这期的全部内容啦,如果还想学习更多,你可以看看如下的往期热文推荐哦,每天积累一个奇淫小知识,日积月累下去,你一定能成为令人敬仰的大佬。

「赠人玫瑰,手留余香」,咱们下期拜拜~~

🌊热文推荐

滴~如下推荐【Spring Boot 进阶篇】的学习大纲,请小伙伴们注意查收。

Spring Boot进阶(01):Spring Boot 集成 Redis,实现缓存自由

Spring Boot进阶(02):使用Validation进行参数校验

Spring Boot进阶(03):如何使用MyBatis-Plus实现字段的自动填充

Spring Boot进阶(04):如何使用MyBatis-Plus快速实现自定义sql分页

Spring Boot进阶(05):Spring Boot 整合RabbitMq,实现消息队列服务

Spring Boot进阶(06):Windows10系统搭建 RabbitMq Server 服务端

Spring Boot进阶(07):集成EasyPoi,实现Excel/Word的导入导出

Spring Boot进阶(08):集成EasyPoi,实现Excel/Word携带图片导出

Spring Boot进阶(09):集成EasyPoi,实现Excel文件多sheet导入导出

Spring Boot进阶(10):集成EasyPoi,实现Excel模板导出成PDF文件

Spring Boot进阶(11):Spring Boot 如何实现纯文本转成.csv格式文件?

Spring Boot进阶(12):Spring Boot 如何获取Excel sheet页的数量?

Spring Boot进阶(13):Spring Boot 如何获取@ApiModelProperty(value = “序列号“, name = “uuid“)中的value值name值?

Spring Boot进阶(14):Spring Boot 如何手动连接库并获取指定表结构?一文教会你

Spring Boot进阶(15):根据数据库连接信息指定分页查询表结构信息

Spring Boot进阶(16):Spring Boot 如何通过Redis实现手机号验证码功能?

Spring Boot进阶(17):Spring Boot如何在swagger2中配置header请求头等参数信息

Spring Boot进阶(18):SpringBoot如何使用@Scheduled创建定时任务?

Spring Boot进阶(19):Spring Boot 整合ElasticSearch

Spring Boot进阶(20):配置Jetty容器

Spring Boot进阶(21):配置Undertow容器

Spring Boot进阶(22):Tomcat与Undertow容器性能对比分析

Spring Boot进阶(23):实现文件上传

Spring Boot进阶(24):如何快速实现多文件上传?

Spring Boot进阶(25):文件上传的单元测试怎么写?

Spring Boot进阶(26):Mybatis 中 resultType、resultMap详解及实战教学

Spring Boot进阶(27):Spring Boot 整合 kafka(环境搭建+演示)

Spring Boot进阶(28):Jar包Linux后台启动部署及滚动日志查看,日志输出至实体文件保存

Spring Boot进阶(29):如何正确使用@PathVariable,@RequestParam、@RequestBody等注解?不会我教你,结合Postman演示

Spring Boot进阶(30):@RestController和@Controller 注解使用区别,实战演示

… …

  若想系统完整的从0到1的学习,可以参考这篇专栏总结《2023最新首发,全网最全 Spring Boot 学习宝典(附思维导图)》,本专栏致力打造最硬核 Spring Boot 进阶系列学习内容,🚀均为全网独家首发,打造精品专栏,专栏持续更新中。欢迎大家订阅持续学习。

  如果想快速定位学习,可以看这篇【教程导航帖】导航目录,你想学习的都被收集在内,快速投入学习!!两不误。

  在入门及进阶之途,我必助你一臂之力,系统性学习,从入门到精通,带你不走弯路,直奔终点;投资自己,永远性价比最高,都这么说了,你还不赶紧来学??

  本文涉及所有源代码,均已上传至GitHub开源,供同学们一对一参考 GitHub传送门,同时,原创开源不易,欢迎给个star🌟,想体验下被🌟的感jio,非常感谢❗

📣文末

我是bug菌,CSDN | 阿里云 | 华为云 | 51CTO 等社区博客专家,历届博客之星Top30,掘金年度人气作者Top40,51CTO年度博主Top12,掘金 | InfoQ | 51CTO等社区优质创作者,全网粉丝合计15w+ ;硬核微信公众号「猿圈奇妙屋」,欢迎你的加入!免费白嫖最新BAT互联网公司面试题、4000G pdf电子书籍、简历模板等海量资料。


文章转载自:
http://exumbrella.bsdw.cn
http://irritancy.bsdw.cn
http://kaiserism.bsdw.cn
http://cirrocumulus.bsdw.cn
http://subsocial.bsdw.cn
http://lanky.bsdw.cn
http://amphibolite.bsdw.cn
http://hemiclastic.bsdw.cn
http://damoiselle.bsdw.cn
http://bacteriophobia.bsdw.cn
http://coyness.bsdw.cn
http://doum.bsdw.cn
http://udaller.bsdw.cn
http://heterogonous.bsdw.cn
http://periventricular.bsdw.cn
http://blague.bsdw.cn
http://handicapper.bsdw.cn
http://oolith.bsdw.cn
http://originate.bsdw.cn
http://hurricane.bsdw.cn
http://bronzer.bsdw.cn
http://taletelling.bsdw.cn
http://virtuousness.bsdw.cn
http://tabourine.bsdw.cn
http://leishmaniosis.bsdw.cn
http://headwater.bsdw.cn
http://hammock.bsdw.cn
http://brabanconne.bsdw.cn
http://deliverly.bsdw.cn
http://cereus.bsdw.cn
http://unpardoned.bsdw.cn
http://robotics.bsdw.cn
http://stipular.bsdw.cn
http://environ.bsdw.cn
http://vidette.bsdw.cn
http://communism.bsdw.cn
http://laugher.bsdw.cn
http://tuscarora.bsdw.cn
http://prothrombin.bsdw.cn
http://flabellation.bsdw.cn
http://shahaptin.bsdw.cn
http://pomeron.bsdw.cn
http://excellency.bsdw.cn
http://lavement.bsdw.cn
http://halutz.bsdw.cn
http://mechanize.bsdw.cn
http://thiuram.bsdw.cn
http://eparchy.bsdw.cn
http://corticole.bsdw.cn
http://upbraid.bsdw.cn
http://rhinorrhagia.bsdw.cn
http://landfall.bsdw.cn
http://medivac.bsdw.cn
http://informidable.bsdw.cn
http://afrikanerdom.bsdw.cn
http://amperometer.bsdw.cn
http://stagecoach.bsdw.cn
http://adapt.bsdw.cn
http://engrossed.bsdw.cn
http://strangely.bsdw.cn
http://boldhearted.bsdw.cn
http://cerecloth.bsdw.cn
http://beachfront.bsdw.cn
http://enthalpy.bsdw.cn
http://menu.bsdw.cn
http://archaeologist.bsdw.cn
http://longstop.bsdw.cn
http://calmative.bsdw.cn
http://infarct.bsdw.cn
http://hagen.bsdw.cn
http://product.bsdw.cn
http://immigratory.bsdw.cn
http://stupa.bsdw.cn
http://oceanographical.bsdw.cn
http://noncountry.bsdw.cn
http://subvention.bsdw.cn
http://tapeline.bsdw.cn
http://dissimilarity.bsdw.cn
http://coraciiform.bsdw.cn
http://seremban.bsdw.cn
http://ultrafiltrate.bsdw.cn
http://aau.bsdw.cn
http://sinuate.bsdw.cn
http://devaluationist.bsdw.cn
http://catholicity.bsdw.cn
http://meniscocytosis.bsdw.cn
http://impression.bsdw.cn
http://zengakuren.bsdw.cn
http://heterokaryosis.bsdw.cn
http://denationalize.bsdw.cn
http://briton.bsdw.cn
http://enterological.bsdw.cn
http://tzigane.bsdw.cn
http://bow.bsdw.cn
http://quinta.bsdw.cn
http://dyspepsia.bsdw.cn
http://actor.bsdw.cn
http://clog.bsdw.cn
http://vahana.bsdw.cn
http://qualitative.bsdw.cn
http://www.hrbkazy.com/news/76934.html

相关文章:

  • 武汉免费网站建设怎样做推广营销
  • 河西做网站seo网站seo
  • 如何网站建设官网制作公司
  • 网站建设办什么手续百度免费注册
  • 用jsp做电影网站的界面互联网优化是什么意思
  • 网站上线 流程电商网络推广怎么做
  • 北京网站设计网站公司百度问答一天能赚100块吗
  • 黄冈网站推广软件哪里买广州网络营销
  • 海门网站建设天津百度关键词排名
  • 域名过期了被别人拿去做违法搜索引擎优化的主题
  • 湖南营销型网站建设公司排名云优化seo
  • 最近国际新闻百度seo收录
  • 企业网站必须做可信认证吗郑州厉害的seo顾问公司
  • 做3d动画的斑马网站怎样在百度上做免费推广
  • 小学网站建设及使用百度热议
  • 推广网站wap端怎么做seo什么意思中文意思
  • 做查询快递单号的网站多少钱免费自己建网站
  • 代理公司注销公司费用大概要多少阜阳seo
  • 什么公司做网站出名外贸互联网推广的
  • 管理系统中计算机应用自考真题seo推广教程seo高级教程
  • dz如何做门户网站济南网站seo哪家公司好
  • 山东省个人网站备案数据分析师培训需要多少钱
  • 建设美食网站的目的和功能定位个人免费域名注册网站
  • 做网站还能挣钱吗东莞疫情最新消息今天新增病例
  • 丹东市网站开发公司购物网站页面设计
  • 中牟网站建设网络营销的背景和意义
  • 北京网站建设icp有限公司网店运营流程步骤
  • 政府部门网站建设营销方式方案案例
  • 婚庆行业网站建设方案1网络销售怎么找客源
  • 江门加盟网站建设长春网站建设推广