Spring Boot Actuators
Spring Boot 提供了开箱即用的应用监控功能,对于大厂来说可能比较鸡肋,但是对于一些没有基础建设团队的中小公司是非常好用的。
使用的时候直接引入对应的包 ,主要是 spring-boot-starter-actuator
<dependencies>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动应用程序后,可以使用对应的URI去访问指定的监控内容。
http://domain/actuator
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"health-component-instance":{
"href":"http://localhost:8080/actuator/health/{component}/{instance}",
"templated":true
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"health-component":{
"href":"http://localhost:8080/actuator/health/{component}",
"templated":true
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
}
}
}
因为里面的内容有应用的敏感信息默认开启的endpoint是health和info,如果需要开启更多的内容,进行配置。
// application.properties
management.endpoints.web.exposure.include=*
上面我们配置开启了所有的功能,可以继续从/actuator看出开启的 Endpoint
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"auditevents":{
"href":"http://localhost:8080/actuator/auditevents",
"templated":false
},
"beans":{
"href":"http://localhost:8080/actuator/beans",
"templated":false
},
"caches-cache":{
"href":"http://localhost:8080/actuator/caches/{cache}",
"templated":true
},
"caches":{
"href":"http://localhost:8080/actuator/caches",
"templated":false
},
"health-component-instance":{
"href":"http://localhost:8080/actuator/health/{component}/{instance}",
"templated":true
},
"health-component":{
"href":"http://localhost:8080/actuator/health/{component}",
"templated":true
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"conditions":{
"href":"http://localhost:8080/actuator/conditions",
"templated":false
},
"configprops":{
"href":"http://localhost:8080/actuator/configprops",
"templated":false
},
"env":{
"href":"http://localhost:8080/actuator/env",
"templated":false
},
"env-toMatch":{
"href":"http://localhost:8080/actuator/env/{toMatch}",
"templated":true
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
},
"loggers":{
"href":"http://localhost:8080/actuator/loggers",
"templated":false
},
"loggers-name":{
"href":"http://localhost:8080/actuator/loggers/{name}",
"templated":true
},
"heapdump":{
"href":"http://localhost:8080/actuator/heapdump",
"templated":false
},
"threaddump":{
"href":"http://localhost:8080/actuator/threaddump",
"templated":false
},
"metrics-requiredMetricName":{
"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated":true
},
"metrics":{
"href":"http://localhost:8080/actuator/metrics",
"templated":false
},
"scheduledtasks":{
"href":"http://localhost:8080/actuator/scheduledtasks",
"templated":false
},
"httptrace":{
"href":"http://localhost:8080/actuator/httptrace",
"templated":false
},
"mappings":{
"href":"http://localhost:8080/actuator/mappings",
"templated":false
}
}
}
从上面可以找出不少比较有用的信息线程栈或者堆信息,在线上需要使用启动应用的用户来进行导出,一般是运维同学才有权限,但是这种日常的操作使用管理后台的功能来导出是非常方便的。
Endpoint |
功能 |
---|---|
/actuator/mappings |
@RequestMapping 定义的HTTP Endpoint的信息 |
/actuator/threaddump |
当前的线程栈信息 |
/actuator/heapdump |
当前堆信息 |
/actuator/beans |
应用实例化的bean的信息 |
/actuator/caches |
应用的cache相关信息,使用到了Spring CacheManager的组件 |
/actuator/conditions |
SpringBoot 各类@OnCondition 注解的执行情况 |
上面几个工具更加倾向于问题的定位,而Spring Boot 2.x后加入的 /actuator/metrics 则是使用了 micrometer进行相关的指标采集,很多开源的监控软件,比如普罗米修斯,可以很容易将其作为数据源对指标进行摄入。
{
"names":[
"jvm.threads.states",
"jvm.gc.pause",
"http.server.requests",
"jvm.memory.used",
"jvm.gc.memory.promoted",
"jvm.memory.max",
"jvm.gc.max.data.size",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"tomcat.global.request",
"tomcat.sessions.expired",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"tomcat.threads.current",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"tomcat.threads.busy",
"process.start.time"
]
}
/actuator/metrics 所采集的指标如上所示,可以根据上面的单个进行采集。比如其中的/actuator/metrics/jvm.memory.used 表示JVM的内存的使用情况。访问后返回的结果是
{
"name":"jvm.memory.used",
"description":"The amount of used memory",
"baseUnit":"bytes",
"measurements":[
{
"statistic":"VALUE",
"value":95404360
}
],
"availableTags":[
{
"tag":"area",
"values":[
"heap",
"nonheap"
]
},
{
"tag":"id",
"values":[
"CodeHeap 'profiled nmethods'",
"G1 Old Gen",
"CodeHeap 'non-profiled nmethods'",
"G1 Survivor Space",
"Compressed Class Space",
"Metaspace",
"G1 Eden Space",
"CodeHeap 'non-nmethods'"
]
}
]
}
有了这些基础的功能,做一个集中式的监控系统就更加方便了。事实上,开源社区贡献的 spring-boot-admin 就是在此为基础上做了一个 管理平台。可以比较方便地使用上述功能。
- [C#2] 5-迭代器
- 服务器未能识别 HTTP 标头 SOAPAction 的值
- 实用代码-C#获取本机网络适配器信息及MAC地址
- WordPress 自定义 login (登录页面)CSS 样式
- [C#1] 12-特性
- HTTP Basic Authentication for RESTFul Service
- [C#2] 4-可空类型、静态类
- jquery 操作css 尺寸
- Windows 7上IIS出现http 500错误
- [C#2] 2-匿名方法
- jquery 操作css 选择器
- 主页后台源码及释义
- [C#2] 3-局部类型、属性访问器保护级别、命名空间别名限定符
- (2013.09更新)最新W3School 离线完整版CHM 电子书下载
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- flutter TextField换行自适应的实现
- 如何造个android Flow流式响应的轮子
- Flutter混合开发详解
- Android 单双击实现的方法步骤
- Android多渠道打包时获取当前渠道的方法
- Flutter Dio二次封装的实现
- android开发通过Scroller实现过渡滑动效果操作示例
- Android9.0 静默安装源码的实现
- Flutter路由框架Fluro使用简介
- Android倒计时神器(CountDownTimer)
- Kotlin如何安全访问lateinit变量的实现
- Android实现加载对话框
- Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果
- Android监听键盘状态获取键盘高度的实现方法
- android实现小音频频繁播放