软件编程
位置:首页>> 软件编程>> java编程>> 深入理解spring boot 监控

深入理解spring boot 监控

作者:MG-net  发布时间:2023-11-04 02:25:15 

标签:spring,boot,监控

一、指标监控

引入jar包:


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

深入理解spring boot 监控

以web方式开启:


#开启全部的
management.endpoints.enabled-by-default=true
#web 方式暴露
management.endpoints.web.exposure.include=*

深入理解spring boot 监控

二、常用的监控端点

看这个:传送门

 最常用的:

health:健康状况,查看应用是否可用

深入理解spring boot 监控深入理解spring boot 监控metrics:

运行时指标,JVM、线程等相关内容(重要)

深入理解spring boot 监控loggers:

日志记录

 三、定制EndPoint

 定制组件健康信息,比较简单,同时也可以实现接口方式:


package com.example.demo;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
* @author Administrator
*/
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

/**
    * 真实的检查方法
    * @param builder
    * @throws Exception
    */
   @Override
   protected void doHealthCheck(Health.Builder builder) throws Exception {
       Map<String, Object> map = new HashMap<>();
       if(1==1){
           builder.up();
           map.put("count", 1);
           map.put("msg", "健康");
       }else{
           builder.down();
           map.put("msg", "超时");
       }
       builder.withDetail("code", 100)
               .withDetails(map);
   }
}

深入理解spring boot 监控

 INFO Endpoint 的定义:

1、配置文件直接定义:


info.mavenProjectName = @project.artifactId@
info.mavenProjectVersion=@project.version@

2、写代码:


package com.example.demo;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class AppInfo implements InfoContributor {

@Override
   public void contribute(Info.Builder builder) {
       builder.withDetail("msg", "真他吗帅!");
   }
}

metrics定制endpoint,直接使用MeterRegistry。

自定义Endpoint,监控端点:


package com.example.demo;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Map;

@Component
@Endpoint(id = "myEndPoint")
public class EndPoint {

@ReadOperation
   public Map<String, Object> read(){
       return Collections.singletonMap("MG", "MG GOGO");
   }

@WriteOperation
   public void write(){
       System.out.println("累");
   }

}

访问自定义的指标的时候,访问的就是read方法

深入理解spring boot 监控

四、spring boot admin(可以使用)

准备一个 server,会定时去获取各个服务的相关内容。


<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

客户端注册:


       <dependency>
           <groupId>de.codecentric</groupId>
           <artifactId>spring-boot-admin-starter-client</artifactId>
       </dependency>

配置属性文件:


spring:
 application:
   name: admin-client
 boot:
   admin:
     client:
       url: http://localhost:8769
       interface:#使用IP注册
           prefer-ip: ture
server:
 port: 8768

management:
 endpoints:
   web:
     exposure:
       include: '*'
 endpoint:
   health:
     show-details: ALWAYS

来源:https://blog.csdn.net/liming0025/article/details/120980720

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com