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

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

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

瀏覽:58日期:2023-09-01 13:53:00

Spring Boot Actuator幫助我們實現了許多中間件比如mysql、es、redis、mq等中間件的健康指示器。通過 Spring Boot 的自動配置,這些指示器會自動生效。當這些組件有問題的時候,HealthIndicator 會返回 DOWN 或 OUT_OF_SERVICE 狀態,health 端點 HTTP 響應狀態碼也會變為 503,我們可以以此來配置程序健康狀態監控報警。使用步驟也非常簡單,這里演示的是線程池的監控。模擬線程池滿了狀態下將HealthInicator指示器變為Down的狀態。

pom中引入jar

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>

引入properties配置

spring.application.name=boot# server.servlet.context-path=/boot# management.server.servlet.context-path=/boot# JVM (Micrometer)要求給應用設置commonTagmanagement.metrics.tags.application=${spring.application.name}#去掉重復的metricsspring.metrics.servo.enabled=falsemanagement.endpoint.metrics.enabled=truemanagement.endpoint.metrics.sensitive=false#顯式配置不需要權限驗證對外開放的端點management.endpoints.web.exposure.include=*management.endpoints.jmx.exposure.include=*management.endpoint.health.show-details=always#Actuator 的 Web 訪問方式的根地址為 /actuator,可以通過 management.endpoints.web.base-path 參數進行修改management.endpoints.web.base-path=/actuatormanagement.metrics.export.prometheus.enabled=true

代碼

/** * @Author jeffSmile * @Date 下午 6:10 2020/5/24 0024 * @Description 定義一個接口,來把耗時很長的任務提交到這個 demoThreadPool 線程池,以模擬線程池隊列滿的情況 **/ @GetMapping('slowTask') public void slowTask() { ThreadPoolProvider.getDemoThreadPool().execute(() -> { try { TimeUnit.HOURS.sleep(1); } catch (InterruptedException e) { } }); }

package com.mongo.boot.service;import jodd.util.concurrent.ThreadFactoryBuilder;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class ThreadPoolProvider { //一個工作線程的線程池,隊列長度10 private static ThreadPoolExecutor demoThreadPool = new ThreadPoolExecutor( 1, 1, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadFactoryBuilder().setNameFormat('demo-threadpool-%d').get()); //核心線程數10,最大線程數50的線程池,隊列長度50 private static ThreadPoolExecutor ioThreadPool = new ThreadPoolExecutor( 10, 50, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100), new ThreadFactoryBuilder().setNameFormat('io-threadpool-%d').get()); public static ThreadPoolExecutor getDemoThreadPool() { return demoThreadPool; } public static ThreadPoolExecutor getIOThreadPool() { return ioThreadPool; }}

package com.mongo.boot.service;import org.springframework.boot.actuate.health.Health;import org.springframework.boot.actuate.health.HealthIndicator;import java.util.HashMap;import java.util.Map;import java.util.concurrent.ThreadPoolExecutor;/** * @Author jeffSmile * @Date 下午 6:12 2020/5/24 0024 * @Description 自定義的 HealthIndicator 類,用于單一線程池的健康狀態 **/public class ThreadPoolHealthIndicator implements HealthIndicator { private ThreadPoolExecutor threadPool; public ThreadPoolHealthIndicator(ThreadPoolExecutor threadPool) { this.threadPool = threadPool; } @Override public Health health() { //補充信息 Map<String, Integer> detail = new HashMap<>(); //隊列當前元素個數 detail.put('queue_size', threadPool.getQueue().size()); //隊列剩余容量 detail.put('queue_remaining', threadPool.getQueue().remainingCapacity()); //如果還有剩余量則返回UP,否則返回DOWN if (threadPool.getQueue().remainingCapacity() > 0) { return Health.up().withDetails(detail).build(); } else { return Health.down().withDetails(detail).build(); } }}

package com.mongo.boot.service;import org.springframework.boot.actuate.health.CompositeHealthContributor;import org.springframework.boot.actuate.health.HealthContributor;import org.springframework.boot.actuate.health.NamedContributor;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/*** * @Author jeffSmile * @Date 下午 6:13 2020/5/24 0024 * @Description 定義一個 CompositeHealthContributor,來聚合兩個 ThreadPoolHealthIndicator 的實例, * 分別對應 ThreadPoolProvider 中定義的兩個線程池 **/@Componentpublic class ThreadPoolsHealthContributor implements CompositeHealthContributor { //保存所有的子HealthContributor private Map<String, HealthContributor> contributors = new HashMap<>(); ThreadPoolsHealthContributor() { //對應ThreadPoolProvider中定義的兩個線程池 this.contributors.put('demoThreadPool', new ThreadPoolHealthIndicator(ThreadPoolProvider.getDemoThreadPool())); this.contributors.put('ioThreadPool', new ThreadPoolHealthIndicator(ThreadPoolProvider.getIOThreadPool())); } @Override public HealthContributor getContributor(String name) { //根據name找到某一個HealthContributor return contributors.get(name); } @Override public Iterator<NamedContributor<HealthContributor>> iterator() { //返回NamedContributor的迭代器,NamedContributor也就是Contributor實例+一個命名 return contributors.entrySet().stream() .map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator(); }}

啟動springboot驗證

這里我訪問:http://localhost:8080/slowTask

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

每次訪問都向demo線程池中提交一個耗時1小時的任務,而demo線程池的核心和最大線程數都是1,隊列長度為10,那么當訪問11次之后,任務將被直接拒絕掉!

Spring Boot Actuator監控的簡單使用方法示例代碼詳解Spring Boot Actuator監控的簡單使用方法示例代碼詳解

此時訪問:http://localhost:8080/actuator/health

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

demo線程池隊列已經滿了,狀態變為DOWN。

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

監控內部重要組件的狀態數據

通過 Actuator 的 InfoContributor 功能,對外暴露程序內部重要組件的狀態數據!實現一個 ThreadPoolInfoContributor 來展現線程池的信息:

package com.mongo.boot.config;import com.mongo.boot.service.ThreadPoolProvider;import org.springframework.boot.actuate.info.Info;import org.springframework.boot.actuate.info.InfoContributor;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.Map;import java.util.concurrent.ThreadPoolExecutor;/** * @Author jeffSmile * @Date 下午 6:37 2020/5/24 0024 * @Description 通過 Actuator 的 InfoContributor 功能,對外暴露程序內部重要組件的狀態數據 **/@Componentpublic class ThreadPoolInfoContributor implements InfoContributor { private static Map threadPoolInfo(ThreadPoolExecutor threadPool) { Map<String, Object> info = new HashMap<>(); info.put('poolSize', threadPool.getPoolSize());//當前池大小 info.put('corePoolSize', threadPool.getCorePoolSize());//設置的核心池大小 info.put('largestPoolSize', threadPool.getLargestPoolSize());//最大達到過的池大小 info.put('maximumPoolSize', threadPool.getMaximumPoolSize());//設置的最大池大小 info.put('completedTaskCount', threadPool.getCompletedTaskCount());//總完成任務數 return info; } @Override public void contribute(Info.Builder builder) { builder.withDetail('demoThreadPool', threadPoolInfo(ThreadPoolProvider.getDemoThreadPool())); builder.withDetail('ioThreadPool', threadPoolInfo(ThreadPoolProvider.getIOThreadPool())); }}

直接訪問http://localhost:8080/actuator/info

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

如果開啟jmx,還可以使用jconsole來查看線程池的狀態信息:

#開啟 JMXspring.jmx.enabled=true

打開jconcole界面之后,進入MBean這個tab,可以在EndPoint下的Info操作這里看到我們的Bean信息。

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

不過,除了jconsole之外,我們可以把JMX協議轉為http協議,這里引入jolokia:

<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId></dependency>

重啟后訪問:http://localhost:8080/actuator/jolokia/exec/org.springframework.boot:type=Endpoint,name=Info/info

Spring Boot Actuator監控的簡單使用方法示例代碼詳解

監控延伸

通過Micrometer+promethues+grafana的組合也可以進行一些生產級別的實踐。

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 合金ICP光谱仪(磁性材料,工业废水)-百科| 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 企业管理培训,企业培训公开课,企业内训课程,企业培训师 - 名课堂企业管理培训网 | H型钢切割机,相贯线切割机,数控钻床,数控平面钻,钢结构设备,槽钢切割机,角钢切割机,翻转机,拼焊矫一体机 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 中国玩具展_玩具展|幼教用品展|幼教展|幼教装备展 | 自动化改造_智虎机器人_灌装机_贴标机-上海圣起包装机械 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 电动葫芦|手拉葫芦|环链电动葫芦|微型电动葫芦-北京市凌鹰起重机械有限公司 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 卓能JOINTLEAN端子连接器厂家-专业提供PCB接线端子|轨道式端子|重载连接器|欧式连接器等电气连接产品和服务 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | 耐驰泵阀管件制造-耐驰泵阀科技(天津)有限公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 协议书_协议合同格式模板范本大全| 水冷式工业冷水机组_风冷式工业冷水机_水冷螺杆冷冻机组-深圳市普威机械设备有限公司 | 厌氧反应器,IC厌氧反应器,厌氧三相分离器-山东创博环保科技有限公司 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 |