最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年06月20日 13:02:41   作者:Jeff.Smile  
這篇文章主要介紹了Spring Boot Actuator監(jiān)控的簡單使用,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Spring Boot Actuator幫助我們實(shí)現(xiàn)了許多中間件比如mysql、es、redis、mq等中間件的健康指示器。
通過 Spring Boot 的自動配置,這些指示器會自動生效。當(dāng)這些組件有問題的時(shí)候,HealthIndicator 會返回 DOWN 或 OUT_OF_SERVICE 狀態(tài),health 端點(diǎn) HTTP 響應(yīng)狀態(tài)碼也會變?yōu)?503,我們可以以此來配置程序健康狀態(tài)監(jiān)控報(bào)警。
使用步驟也非常簡單,這里演示的是線程池的監(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è)置commonTag
management.metrics.tags.application=${spring.application.name}
#去掉重復(fù)的metrics
spring.metrics.servo.enabled=false

management.endpoint.metrics.enabled=true
management.endpoint.metrics.sensitive=false
#顯式配置不需要權(quán)限驗(yàn)證對外開放的端點(diǎn)
management.endpoints.web.exposure.include=*
management.endpoints.jmx.exposure.include=*

management.endpoint.health.show-details=always
#Actuator 的 Web 訪問方式的根地址為 /actuator,可以通過 management.endpoints.web.base-path 參數(shù)進(jìn)行修改
management.endpoints.web.base-path=/actuator
management.metrics.export.prometheus.enabled=true

代碼

/**
  * @Author jeffSmile
  * @Date 下午 6:10 2020/5/24 0024
  * @Description 定義一個(gè)接口,來把耗時(shí)很長的任務(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ì)列長度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ì)列長度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,來聚合兩個(gè) ThreadPoolHealthIndicator 的實(shí)例,
 * 分別對應(yīng) ThreadPoolProvider 中定義的兩個(gè)線程池
 **/

@Component
public class ThreadPoolsHealthContributor implements CompositeHealthContributor {

 //保存所有的子HealthContributor
 private Map<String, HealthContributor> contributors = new HashMap<>();

 ThreadPoolsHealthContributor() {
  //對應(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();
 }
}

啟動springboot驗(yàn)證

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

在這里插入圖片描述

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

在這里插入圖片描述
在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

通過 Actuator 的 InfoContributor 功能,對外暴露程序內(nèi)部重要組件的狀態(tài)數(shù)據(jù)!
實(shí)現(xiàn)一個(gè) ThreadPoolInfoContributor 來展現(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 通過 Actuator 的 InfoContributor 功能,對外暴露程序內(nèi)部重要組件的狀態(tài)數(shù)據(jù)
 **/

@Component
public 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á)到過的池大小
  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()));
 }
}

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

在這里插入圖片描述

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

#開啟 JMX
spring.jmx.enabled=true

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

在這里插入圖片描述

不過,除了jconsole之外,我們可以把JMX協(xié)議轉(zhuǎn)為http協(xié)議,這里引入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

在這里插入圖片描述

監(jiān)控延伸

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

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

相關(guān)文章

  • SpringBoot自動配置@EnableAutoConfiguration過程示例

    SpringBoot自動配置@EnableAutoConfiguration過程示例

    這篇文章主要為大家介紹了SpringBoot自動配置@EnableAutoConfiguration的過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Mybatis查詢多條記錄并返回List集合的方法

    Mybatis查詢多條記錄并返回List集合的方法

    這篇文章主要介紹了Mybatis查詢多條記錄并返回List集合的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • intellij idea的快速配置使用詳細(xì)教程

    intellij idea的快速配置使用詳細(xì)教程

    這篇文章主要介紹了intellij idea的快速配置使用詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 基于java實(shí)現(xiàn)簡單發(fā)紅包功能

    基于java實(shí)現(xiàn)簡單發(fā)紅包功能

    這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)簡單發(fā)紅包功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 教你如何使用JAVA POI

    教你如何使用JAVA POI

    今天教大家怎么學(xué)習(xí)JAVA POI的用法,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Java泛型的類型擦除示例詳解

    Java泛型的類型擦除示例詳解

    Java泛型(Generic)的引入加強(qiáng)了參數(shù)類型的安全性,減少了類型的轉(zhuǎn)換,但有一點(diǎn)需要注意,Java 的泛型在編譯器有效,在運(yùn)行期被刪除,也就是說所有泛型參數(shù)類型在編譯后都會被清除掉,這篇文章主要給大家介紹了關(guān)于Java泛型的類型擦除的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Java數(shù)組索引異常產(chǎn)生及解決方案

    Java數(shù)組索引異常產(chǎn)生及解決方案

    這篇文章主要介紹了Java數(shù)組索引異常產(chǎn)生及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • SpringBoot實(shí)現(xiàn)異步事件Event詳解

    SpringBoot實(shí)現(xiàn)異步事件Event詳解

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)異步事件Event詳解,異步事件的模式,通常將一些非主要的業(yè)務(wù)放在監(jiān)聽器中執(zhí)行,因?yàn)楸O(jiān)聽器中存在失敗的風(fēng)險(xiǎn),所以使用的時(shí)候需要注意,需要的朋友可以參考下
    2023-11-11
  • springboot?sleuth?日志跟蹤問題記錄

    springboot?sleuth?日志跟蹤問題記錄

    Spring?Cloud?Sleuth是一個(gè)在應(yīng)用中實(shí)現(xiàn)日志跟蹤的強(qiáng)有力的工具,使用Sleuth庫可以應(yīng)用于計(jì)劃任務(wù)?、多線程服務(wù)或復(fù)雜的Web請求,尤其是在一個(gè)由多個(gè)服務(wù)組成的系統(tǒng)中,這篇文章主要介紹了springboot?sleuth?日志跟蹤,需要的朋友可以參考下
    2023-07-07
  • 關(guān)于Spring統(tǒng)一異常處理及說明

    關(guān)于Spring統(tǒng)一異常處理及說明

    這篇文章主要介紹了關(guān)于Spring統(tǒng)一異常處理及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論

故城县| 阿坝| 安义县| 屏边| 琼海市| 宣威市| 沿河| 长寿区| 文山县| 泰顺县| 南雄市| 石景山区| 湾仔区| 韶山市| 凉山| 霍山县| 辽宁省| 神农架林区| 蓬安县| 静安区| 工布江达县| 农安县| 于都县| 阆中市| 玉田县| 余姚市| 英山县| 武汉市| 东明县| 禹州市| 陆川县| 吉安市| 雅江县| 岑巩县| 泸水县| 许昌县| 黄山市| 东乡| 临洮县| 达拉特旗| 黔东|