温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot starter actuator怎么用

发布时间:2021-08-09 11:05:57 来源:亿速云 阅读:172 作者:小新 栏目:编程语言

这篇文章主要介绍了spring boot starter actuator怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

添加POM依赖:

<!-- spring-boot-监控-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.yml中指定监控的HTTP端口(如果不指定,则使用和Server相同的端口);指定去掉某项的检查(比如不监控health.mail):

server:
 port: 8083
management:
  port: 8083
  security:
   enabled: false #

监控和管理端点

端点名描述
autoconfig所有自动配置信息( positiveMatches :运行的, negativeMatches 未运行组件)
auditevents审计事件
beans所有Bean的信息
configprops所有配置属性
dump线程状态信息
env当前环境信息
health应用健康状况
info当前应用信息
metrics应用的各项指标
mappings应用@RequestMapping映射路径
shutdown关闭当前应用(默认关闭)
trace追踪信息(最新的http请求)
heapdump下载内存快照

http://localhost:8083/info 读取配置文件application.properties的 info.*属性

  在InfoProperties 读取

  application.properties :

info.app.version=v1.2.0
info.app.name=abc

在GitProperties  获取git.properties 的信息 

info.app.version=v1.2.0
info.app.name=abc
#远程关闭开启
endpoints.shutdown.enabled=true 
#访问:http://localhost:8083/shutdown  关闭服务

metrics

{
mem: 573549,  //内存大小
mem.free: 388198, //内存剩余大小
processors: 4, //处理器数量
instance.uptime: 338426,
uptime: 345091,
systemload.average: -1,
heap.committed: 489984,
heap.init: 131072,
heap.used: 101785,
heap: 1842688,
nonheap.committed: 85056,
nonheap.init: 2496,
nonheap.used: 83566,
nonheap: 0,
threads.peak: 46,
threads.daemon: 36,
threads.totalStarted: 72,
threads: 39, //线程
classes: 12109,
classes.loaded: 12109, //加载的类
classes.unloaded: 0, //没加载的类
gc.ps_scavenge.count: 10,
gc.ps_scavenge.time: 103,
gc.ps_marksweep.count: 3,
gc.ps_marksweep.time: 219,
httpsessions.max: -1,
httpsessions.active: 0,
gauge.response.mappings: 3,
gauge.response.autoconfig: 4,
gauge.response.trace: 167,
counter.status.200.mappings: 1,
counter.status.200.autoconfig: 2,
counter.status.200.trace: 1
}

自定义配置说明:

#关闭metrics功能
endpoints.metrics.enabled=false
#开启shutdown远程关闭功能
endpoints.shutdown.enabled=true
#设置beansId
endpoints.beans.id=mybean
#设置beans路径
endpoints.beans.path=/bean
#关闭beans 功能
endpoints.beans.enabled=false
#关闭所有的
endpoints.enabled=false 
#开启单个beans功能
endpoints.beans.enabled=true
#所有访问添加根目录
management.context-path=/manage
management.port=8181

org.springframework.boot.actuate.health 包下对于所有的健康状态检查例如:RedisHealthIndicator ,当有redis的starter 时候就会检查

{
  status: "DOWN", //状态
  diskSpace: {
  status: "UP",
  total: 395243941888,
  free: 367246643200,
  threshold: 10485760
  },
  rabbit: {
  status: "DOWN",
  error: "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect"
  },
  redis: {
  status: "UP",
  version: "4.0.9"
  },
  db: {
  status: "UP",
  database: "MySQL",
  hello: 1
  }
}

自定义health

•自定义健康状态指示器

•1、编写一个指示器 实现 HealthIndicator 接口

•2、指示器的名字 xxxxHealthIndicator

•3、加入容器中

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyAppHealthIndicator implements HealthIndicator {

  @Override
  public Health health() {

    //自定义的检查方法
    //Health.up().build()代表健康
    return Health.down().withDetail("msg","服务异常").build();
  }
}

感谢你能够认真阅读完这篇文章,希望小编分享的“spring boot starter actuator怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI