午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

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

Spring Boot Actuator幫助我們實(shí)現(xiàn)了許多中間件比如mysql、es、redis、mq等中間件的健康指示器。通過(guò) Spring Boot 的自動(dòng)配置,這些指示器會(huì)自動(dòng)生效。當(dāng)這些組件有問(wèn)題的時(shí)候,HealthIndicator 會(huì)返回 DOWN 或 OUT_OF_SERVICE 狀態(tài),health 端點(diǎn) HTTP 響應(yīng)狀態(tài)碼也會(huì)變?yōu)?503,我們可以以此來(lái)配置程序健康狀態(tài)監(jiān)控報(bào)警。使用步驟也非常簡(jiǎn)單,這里演示的是線程池的監(jiān)控。模擬線程池滿了狀態(tài)下將HealthInicator指示器變?yōu)镈own的狀態(tài)。

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)要求給應(yīng)用設(shè)置commonTagmanagement.metrics.tags.application=${spring.application.name}#去掉重復(fù)的metricsspring.metrics.servo.enabled=falsemanagement.endpoint.metrics.enabled=truemanagement.endpoint.metrics.sensitive=false#顯式配置不需要權(quán)限驗(yàn)證對(duì)外開(kāi)放的端點(diǎn)management.endpoints.web.exposure.include=*management.endpoints.jmx.exposure.include=*management.endpoint.health.show-details=always#Actuator 的 Web 訪問(wèn)方式的根地址為 /actuator,可以通過(guò) management.endpoints.web.base-path 參數(shù)進(jìn)行修改management.endpoints.web.base-path=/actuatormanagement.metrics.export.prometheus.enabled=true

代碼

/** * @Author jeffSmile * @Date 下午 6:10 2020/5/24 0024 * @Description 定義一個(gè)接口,來(lái)把耗時(shí)很長(zhǎng)的任務(wù)提交到這個(gè) demoThreadPool 線程池,以模擬線程池隊(duì)列滿的情況 **/ @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 { //一個(gè)工作線程的線程池,隊(duì)列長(zhǎng)度10 private static ThreadPoolExecutor demoThreadPool = new ThreadPoolExecutor( 1, 1, 2, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadFactoryBuilder().setNameFormat('demo-threadpool-%d').get()); //核心線程數(shù)10,最大線程數(shù)50的線程池,隊(duì)列長(zhǎng)度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 類,用于單一線程池的健康狀態(tài) **/public class ThreadPoolHealthIndicator implements HealthIndicator { private ThreadPoolExecutor threadPool; public ThreadPoolHealthIndicator(ThreadPoolExecutor threadPool) { this.threadPool = threadPool; } @Override public Health health() { //補(bǔ)充信息 Map<String, Integer> detail = new HashMap<>(); //隊(duì)列當(dāng)前元素個(gè)數(shù) detail.put('queue_size', threadPool.getQueue().size()); //隊(duì)列剩余容量 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 定義一個(gè) CompositeHealthContributor,來(lái)聚合兩個(gè) ThreadPoolHealthIndicator 的實(shí)例, * 分別對(duì)應(yīng) ThreadPoolProvider 中定義的兩個(gè)線程池 **/@Componentpublic class ThreadPoolsHealthContributor implements CompositeHealthContributor { //保存所有的子HealthContributor private Map<String, HealthContributor> contributors = new HashMap<>(); ThreadPoolsHealthContributor() { //對(duì)應(yīng)ThreadPoolProvider中定義的兩個(gè)線程池 this.contributors.put('demoThreadPool', new ThreadPoolHealthIndicator(ThreadPoolProvider.getDemoThreadPool())); this.contributors.put('ioThreadPool', new ThreadPoolHealthIndicator(ThreadPoolProvider.getIOThreadPool())); } @Override public HealthContributor getContributor(String name) { //根據(jù)name找到某一個(gè)HealthContributor return contributors.get(name); } @Override public Iterator<NamedContributor<HealthContributor>> iterator() { //返回NamedContributor的迭代器,NamedContributor也就是Contributor實(shí)例+一個(gè)命名 return contributors.entrySet().stream() .map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator(); }}

啟動(dòng)springboot驗(yàn)證

這里我訪問(wèn):http://localhost:8080/slowTask

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

每次訪問(wèn)都向demo線程池中提交一個(gè)耗時(shí)1小時(shí)的任務(wù),而demo線程池的核心和最大線程數(shù)都是1,隊(duì)列長(zhǎng)度為10,那么當(dāng)訪問(wèn)11次之后,任務(wù)將被直接拒絕掉!

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

此時(shí)訪問(wèn):http://localhost:8080/actuator/health

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

demo線程池隊(duì)列已經(jīng)滿了,狀態(tài)變?yōu)镈OWN。

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

監(jiān)控內(nèi)部重要組件的狀態(tài)數(shù)據(jù)

通過(guò) Actuator 的 InfoContributor 功能,對(duì)外暴露程序內(nèi)部重要組件的狀態(tài)數(shù)據(jù)!實(shí)現(xiàn)一個(gè) ThreadPoolInfoContributor 來(lái)展現(xiàn)線程池的信息:

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 通過(guò) Actuator 的 InfoContributor 功能,對(duì)外暴露程序內(nèi)部重要組件的狀態(tài)數(shù)據(jù) **/@Componentpublic class ThreadPoolInfoContributor implements InfoContributor { private static Map threadPoolInfo(ThreadPoolExecutor threadPool) { Map<String, Object> info = new HashMap<>(); info.put('poolSize', threadPool.getPoolSize());//當(dāng)前池大小 info.put('corePoolSize', threadPool.getCorePoolSize());//設(shè)置的核心池大小 info.put('largestPoolSize', threadPool.getLargestPoolSize());//最大達(dá)到過(guò)的池大小 info.put('maximumPoolSize', threadPool.getMaximumPoolSize());//設(shè)置的最大池大小 info.put('completedTaskCount', threadPool.getCompletedTaskCount());//總完成任務(wù)數(shù) return info; } @Override public void contribute(Info.Builder builder) { builder.withDetail('demoThreadPool', threadPoolInfo(ThreadPoolProvider.getDemoThreadPool())); builder.withDetail('ioThreadPool', threadPoolInfo(ThreadPoolProvider.getIOThreadPool())); }}

直接訪問(wèn)http://localhost:8080/actuator/info

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

如果開(kāi)啟jmx,還可以使用jconsole來(lái)查看線程池的狀態(tài)信息:

#開(kāi)啟 JMXspring.jmx.enabled=true

打開(kāi)jconcole界面之后,進(jìn)入MBean這個(gè)tab,可以在EndPoint下的Info操作這里看到我們的Bean信息。

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

不過(guò),除了jconsole之外,我們可以把JMX協(xié)議轉(zhuǎn)為http協(xié)議,這里引入jolokia:

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

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

Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用方法示例代碼詳解

監(jiān)控延伸

通過(guò)Micrometer+promethues+grafana的組合也可以進(jìn)行一些生產(chǎn)級(jí)別的實(shí)踐。

到此這篇關(guān)于Spring Boot Actuator監(jiān)控的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)Spring Boot Actuator監(jiān)控內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 黄色男女视频 | 四虎最新入口 | 99精品欧美一区二区三区综合在线 | 又紧又大又爽精品一区二区 | 久热99 | a级片黄色| 四虎色播| 亚洲激情国产 | 亚洲综合图区 | 国产香蕉97碰碰碰视频在线观看 | 狠狠久久久| 欧美日韩一区二区三区四区五区六区 | 日本一区免费看 | 9i看片成人免费看片 | 美国黄色小视频 | 欧美日韩中文 | 三上悠亚在线观看视频 | 华人永久免费 | 久久久久久爱 | 国产一区在线观看视频 | 午夜天堂av| 国产不卡在线观看视频 | 人成网站在线观看 | 日韩欧美国产一区二区三区 | 在线观看视频中文字幕 | 久操青青| 五月激情丁香 | 久久精品在线观看 | 久久国产热视频 | 日韩在线一区二区 | 一本一道波多野结衣一区二区 | 国产一区二区av | 黄色小视频在线观看 | 黄视频免费看在线 | 日韩视频免费观看 | 成人永久免费 | 免费成人深夜天涯网站 | 亚洲人毛茸茸 | 亚洲成人a∨ | 国产免费黄色 | 国产精品一区二区视频 |