栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot 监控

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot 监控

一、指标监控

引入jar包:

        
            org.springframework.boot
            spring-boot-actuator
        

以web方式开启:

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

 

二、常用的监控端点

看这个:传送门

 最常用的:

  • health:健康状况,查看应用是否可用
  • metrics:运行时指标,JVM、线程等相关内容(重要)
  • 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;


@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

    
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        Map 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);
    }
}

 

 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 read(){
        return Collections.singletonMap("MG", "MG GOGO");
    }

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

}

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

四、spring boot admin(可以使用)

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


    de.codecentric
    spring-boot-admin-starter-server

客户端注册:

        
            de.codecentric
            spring-boot-admin-starter-client
        

配置属性文件:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354387.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号