欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Spring Boot Actuator幫助我們實現(xiàn)了許多中間件比如mysql、es、redis、mq等中間件的健康指示器。
通過 Spring Boot 的自動配置,這些指示器會自動生效。當這些組件有問題的時候,HealthIndicator 會返回 DOWN 或 OUT_OF_SERVICE 狀態(tài),health 端點 HTTP 響應狀態(tài)碼也會變?yōu)?503,我們可以以此來配置程序健康狀態(tài)監(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)要求給應用設置commonTag
management.metrics.tags.application=${spring.application.name}
#去掉重復的metrics
spring.metrics.servo.enabled=false

management.endpoint.metrics.enabled=true
management.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 參數(shù)進行修改
management.endpoints.web.base-path=/actuator
management.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());
 //核心線程數(shù)10,最大線程數(shù)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 類,用于單一線程池的健康狀態(tài)
 **/

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<>();
  //隊列當前元素個數(shù)
  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 中定義的兩個線程池
 **/

@Component
public 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) {
  //根據(jù)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

在這里插入圖片描述

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

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

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

在這里插入圖片描述

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

在這里插入圖片描述

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

通過 Actuator 的 InfoContributor 功能,對外暴露程序內部重要組件的狀態(tài)數(shù)據(jù)!
實現(xiàn)一個 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 功能,對外暴露程序內部重要組件的狀態(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());//當前池大小
  info.put("corePoolSize", threadPool.getCorePoolSize());//設置的核心池大小
  info.put("largestPoolSize", threadPool.getLargestPoolSize());//最大達到過的池大小
  info.put("maximumPoolSize", threadPool.getMaximumPoolSize());//設置的最大池大小
  info.put("completedTaskCount", threadPool.getCompletedTaskCount());//總完成任務數(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界面之后,進入MBean這個tab,可以在EndPoint下的Info操作這里看到我們的Bean信息。

在這里插入圖片描述

不過,除了jconsole之外,我們可以把JMX協(xié)議轉為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的組合也可以進行一些生產級別的實踐。

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

相關文章

  • 新手了解java基礎知識(一)

    新手了解java基礎知識(一)

    這篇文章主要介紹了Java基礎知識,本文介紹了Java語言相關的基礎知識、歷史介紹、主要應用方向等內容,需要的朋友可以參考下,希望對你有所幫助
    2021-07-07
  • Java的正則表達式深入分析

    Java的正則表達式深入分析

    本篇文章介紹了,Java的正則表達式深入分析。需要的朋友參考下
    2013-04-04
  • Springboot調整接口響應返回時長詳解(解決響應超時問題)

    Springboot調整接口響應返回時長詳解(解決響應超時問題)

    當后端對于數(shù)據(jù)量較大的處理或是某些耗時的操作時,需要先對請求接口的請求進行響應,下面這篇文章主要給大家介紹了關于Springboot調整接口響應返回時長(解決響應超時問題)的相關資料,需要的朋友可以參考下
    2023-01-01
  • java開發(fā)的工廠方法模式及抽象工廠驗證示例

    java開發(fā)的工廠方法模式及抽象工廠驗證示例

    這篇文章主要為大家介紹了java開發(fā)中的工廠方法模式以及抽象工廠的驗證示例,有需要的朋友可以借鑒參考下希望能夠有所幫助祝大家多多進步
    2021-10-10
  • 詳解JAVA 連等賦值問題

    詳解JAVA 連等賦值問題

    這篇文章主要介紹了JAVA 連等賦值問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 利用Springboot實現(xiàn)Jwt認證的示例代碼

    利用Springboot實現(xiàn)Jwt認證的示例代碼

    這篇文章主要介紹了利用Springboot實現(xiàn)Jwt認證的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Object.wait()與Object.notify()的用法詳細解析

    Object.wait()與Object.notify()的用法詳細解析

    以下是對java中Object.wait()與Object.notify()的用法進行了詳細的分析介紹,需要的朋友可以過來參考下
    2013-09-09
  • Spring Security和Shiro的相同點與不同點整理

    Spring Security和Shiro的相同點與不同點整理

    在本篇文章里小編給大家整理的是關于Spring Security和Shiro的相同不同點整理,需要的朋友們可以參考下。
    2020-02-02
  • java基礎之 Arrays.toString()方法詳解

    java基礎之 Arrays.toString()方法詳解

    這篇文章主要介紹了java基礎之 Arrays.toString()方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBoot登錄驗證token攔截器的實現(xiàn)

    SpringBoot登錄驗證token攔截器的實現(xiàn)

    本文主要介紹了SpringBoot登錄驗證token攔截器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07

最新評論