当前位置:首页 > 通信资讯 > 正文

spring boot应用监控(spring boot 链路监控)

目录
  • 一、指标监控
  • 二、常用的监控端点
  • 三、定制EndPoint
  • 四、spring boot admin(可以使用)

一、指标监控

引入jar包:

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

spring boot应用监控(spring boot 链路监控)

以web方式开启:

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

spring boot应用监控(spring boot 链路监控)

二、常用的监控端点

看这个:传送门

最常用的:

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

spring boot应用监控(spring boot 链路监控)spring boot应用监控(spring boot 链路监控)metrics:

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

spring boot应用监控(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应用监控(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 链路监控)

四、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

到此这篇关于spring boot 监控的文章就介绍到这了,更多相关spring boot 监控内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/liming0025/article/details/120980720

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。