电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

SpringBoot使用prometheus監控的示例代碼

瀏覽:92日期:2023-03-20 17:09:09

本文介紹SpringBoot如何使用Prometheus配合Grafana監控。

1.關于Prometheus

Prometheus是一個根據應用的metrics來進行監控的開源工具。相信很多工程都在使用它來進行監控,有關詳細介紹可以查看官網:https://prometheus.io/docs/introduction/overview/。

2.有關Grafana

Grafana是一個開源監控利器,如圖所示。

SpringBoot使用prometheus監控的示例代碼

從圖中就可以看出來,使用Grafana監控很高大上,提供了很多可視化的圖標。

官網地址:https://grafana.com/

3.SpringBoot使用Prometheus3.1 依賴內容

在SpringBoot中使用Prometheus其實很簡單,不需要配置太多的東西,在pom文件中加入依賴,完整內容如下所示。

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.dalaoyang</groupId><artifactId>springboot2_prometheus</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot2_prometheus</name><description>springboot2_prometheus</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.1.3</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>3.2 配置文件

配置文件中加入配置,這里就只進行一些簡單配置,management.metrics.tags.application屬性是本文配合Grafana的Dashboard設置的,如下所示:

spring.application.name=springboot_prometheusmanagement.endpoints.web.exposure.include=*management.metrics.tags.application=${spring.application.name}3.3 設置application

修改啟動類,如下所示.

@SpringBootApplicationpublic class Springboot2PrometheusApplication {public static void main(String[] args) {SpringApplication.run(Springboot2PrometheusApplication.class, args);}@BeanMeterRegistryCustomizer<MeterRegistry> configurer(@Value('${spring.application.name}') String applicationName) {return (registry) -> registry.config().commonTags('application', applicationName);}}

SpringBoot項目到這里就配置完成了,啟動項目,訪問http://localhost:8080/actuator/prometheus,如圖所示,可以看到一些度量指標。

SpringBoot使用prometheus監控的示例代碼

4.Prometheus配置4.1 配置應用

在prometheus配置監控我們的SpringBoot應用,完整配置如下所示。

# 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: - job_name: ’prometheus’ static_configs: - targets: [’127.0.0.1:9090’]###以下內容為SpringBoot應用配置 - job_name: ’springboot_prometheus’ scrape_interval: 5s metrics_path: ’/actuator/prometheus’ static_configs: - targets: [’127.0.0.1:8080’]4.2 啟動Prometheus

啟動Prometheus,瀏覽器訪問,查看Prometheus頁面,如圖所示。

SpringBoot使用prometheus監控的示例代碼

點擊如圖所示位置,可以查看Prometheus監控的應用。

SpringBoot使用prometheus監控的示例代碼

列表中UP的頁面為存活的實例,如圖所示。

SpringBoot使用prometheus監控的示例代碼

也可以查看很多指數,如下所示。

SpringBoot使用prometheus監控的示例代碼

5.Grafana配置

啟動Grafana,配置Prometheus數據源,這里以ID是4701的Doshboard為例(地址:https://grafana.com/dashboards/4701)如圖。

SpringBoot使用prometheus監控的示例代碼

在Grafana內點擊如圖所示import按鈕

SpringBoot使用prometheus監控的示例代碼

在如圖所示位置填寫4701,然后點擊load。

SpringBoot使用prometheus監控的示例代碼

接下來導入Doshboard。

SpringBoot使用prometheus監控的示例代碼

導入后就可以看到我們的SpringBoot項目對應的指標圖表了,如圖。

SpringBoot使用prometheus監控的示例代碼

6.源碼

源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus

到此這篇關于SpringBoot使用prometheus監控的示例代碼的文章就介紹到這了,更多相關SpringBoot prometheus監控內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 西安烟道厂家_排气道厂家_包立管厂家「陕西西安」推荐西安天宇烟道 | 游动电流仪-流通式浊度分析仪-杰普仪器(上海)有限公司 | 庭院灯_太阳能景观灯_草坪灯厂家_仿古壁灯-重庆恒投科技 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 儋州在线-儋州招聘找工作、找房子、找对象,儋州综合生活信息门户! | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 锂离子电池厂家-山东中信迪生电源 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 地图标注|微信高德百度地图标注|地图标记-做地图[ZuoMap.com] | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | U拓留学雅思一站式服务中心_留学申请_雅思托福培训 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 淄博不锈钢无缝管,淄博不锈钢管-鑫门物资有限公司 | 锻造液压机,粉末冶金,拉伸,坩埚成型液压机定制生产厂家-山东威力重工官方网站 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 北京京云律师事务所 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 计算机毕业设计源码网| 便携式表面粗糙度仪-彩屏硬度计-分体式粗糙度仪-北京凯达科仪科技有限公司 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 探鸣起名网-品牌起名-英文商标起名-公司命名-企业取名包满意 | 欧洲MV日韩MV国产_人妻无码一区二区三区免费_少妇被 到高潮喷出白浆av_精品少妇自慰到喷水AV网站 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 暴风影音| 光栅尺_Magnescale探规_磁栅尺_笔式位移传感器_苏州德美达 | 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 安平县鑫川金属丝网制品有限公司,防风抑尘网,单峰防风抑尘,不锈钢防风抑尘网,铝板防风抑尘网,镀铝锌防风抑尘网 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 塑料异型材_PVC异型材_封边条生产厂家_PC灯罩_防撞扶手_医院扶手价格_东莞市怡美塑胶制品有限公司 |