SpringBoot--集成actuator
actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节,本文只介绍如何集成actuator,及actuator最最简单的使用,对于自定义endPoint及actuator的实现原理后续单独介绍。
1、导入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
如果要访问info接口访问maven中配置的内容,需要配置如下插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
2、增加相关配置
# 描述信息
info:
blog-url: http://lcl.com
author: lcl
version: @project.version@ # 加载所有的端点/默认只加载了 info / health
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always # 可以关闭制定的端点
shutdown:
enabled: false # 路径映射,将 health 路径映射成 rest_health 那么在访问 health 路径将为404,因为原路径已经变成 rest_health 了,一般情况下不建议使用
# management.endpoints.web.path-mapping.health=rest_health
此处注意一点,如果使用的是properties配置文件,使用星号时,不需要加单引号,但是使用yml配置文件时,必须要加单引号,否则启动报错。
3、测试
输入http://localhost:8080/actuator/info进行测试
4、自定义检测
虽然actuator拥有强大的功能,但是仍可能不满足我们的需求,就需要自定义端点进行测试,自定义端点测试主要有两种实现方式,实现HealthIndicator接口或继承
AbstractHealthIndicator类,话不多说,直接上代码。 (1)实现HealthIndicator接口:
package com.example.demo.health; import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component; @Component("myhealth1")
public class MyHealth implements HealthIndicator { private static final String VERSION = "v1.0.0"; @Override
public Health health() {
int code = check();
if(code != 0){
Health.down().withDetail("code",code).withDetail("version", VERSION).build();
}
return Health.up().withDetail("code",code).withDetail("version", VERSION).up().build();
} private int check() {
return 0;
}
}
测试:http://localhost:8080/actuator/health
(2)继承AbstractHealthIndicator类
package com.example.demo.health; import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component; @Component("myhealth2")
public class MyHealth2 extends AbstractHealthIndicator { private static final String VERSION = "v1.0.0"; @Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int code = check();
if (code != 0) {
builder.down().withDetail("code", code).withDetail("version", VERSION).build();
}
builder.withDetail("code", code)
.withDetail("version", VERSION).up().build();
} private int check() {
return 0;
}
}
测试:http://localhost:8080/actuator/health
由上可以查看系统集成了redis、mysql和rabbitMQ,其中redis和mysql连接正常,rabbitMQ连接异常。
下列是依赖spring-boot-xxx-starter
后相关HealthIndicator
的实现(通过management.health.defaults.enabled
属性可以禁用它们),但想要获取一些额外的信息时,自定义的作用就体现出来了…
名称 | 描述 |
---|---|
CassandraHealthIndicator | 检查 Cassandra 数据库是否启动。 |
DiskSpaceHealthIndicator | 检查磁盘空间不足。 |
DataSourceHealthIndicator | 检查是否可以获得连接 DataSource 。 |
ElasticsearchHealthIndicator | 检查 Elasticsearch 集群是否启动。 |
InfluxDbHealthIndicator | 检查 InfluxDB 服务器是否启动。 |
JmsHealthIndicator | 检查 JMS 代理是否启动。 |
MailHealthIndicator | 检查邮件服务器是否启动。 |
MongoHealthIndicator | 检查 Mongo 数据库是否启动。 |
Neo4jHealthIndicator | 检查 Neo4j 服务器是否启动。 |
RabbitHealthIndicator | 检查 Rabbit 服务器是否启动。 |
RedisHealthIndicator | 检查 Redis 服务器是否启动。 |
SolrHealthIndicator | 检查 Solr 服务器是否已启动。 |
SpringBoot--集成actuator的更多相关文章
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成actuator模块的基本使用
© 版权声明:本文为博主原创文章,转载请注明出处 1. 版本 SpringBoot:2.0.0.RELEASE 2. 集成 SpringBoot集成actuator模块非常简单,只需要引入actuat ...
- SpringBoot集成Actuator监控管理
1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...
- SpringBoot集成Actuator端点配置
1.说明 Actuator端点可以监控应用程序并与之交互. Spring Boot包括许多内置的端点, 比如health端点提供基本的应用程序运行状况信息, 并允许添加自定义端点. 可以控制每个单独的 ...
- SpringBoot集成Actuator健康指示器health
1.说明 本文详细介绍Actuator提供的HealthIndicators, 即健康指示器的配置使用, 利用自动配置的健康指标, 检查正在运行的应用程序的状态, 以及自定义健康指标的方法. 监控软件 ...
- SpringBoot系列: Actuator监控
Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...
- SpringBoot之actuator
在springBoot中集成actuator可以很方便的管理和监控应用的状态. 暴露的Restful接口有: HTTP方法 路径 描述 鉴权 GET /autoconfig 查看自动配置的使用情况 t ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
随机推荐
- .net core Docker 容器添加ffmpeg 获取视频信息和截图
最近在处理上传视频,需要获取视频信息和截图,这里就需要用到ffmpeg; 由于我的项目是在docker compose中运行调试,所以ffmpeg也需要在docker中能调用: 网上找到的方法在Doc ...
- 使用VUE开发用户后台时的动态路由问题、按钮权限问题以及其他页面处理问题
如今前后端分离是大势所趋,笔者虽然是做后台的,但也不得不学学前端的流行框架VUE -_-||| . 为了学习VUE,笔者搭建了一个简单的用户后台,以此来了解VUE的开发思路(注:本项目不用于实际开发, ...
- jchdl - GSL Wire
https://mp.weixin.qq.com/s/4w_wwwCd6iBhh0QR2wK81Q org.jchdl.model.gsl.core.datatype.net.Wire.java ...
- (String),toString(),String.valueOf()
String.valueOf("")的源码:(推荐这种写法) 注意:obj问null时,返回值是字符串"null" toString("") ...
- Java实现 蓝桥杯VIP 算法训练 数的统计
问题描述 在一个有限的正整数序列中,有些数会多次重复出现在这个序列中. 如序列:3,1,2,1,5,1,2.其中1就出现3次,2出现2次,3出现1 次,5出现1次. 你的任务是对于给定的正整数序列,从 ...
- Java实现 LeetCode 264 丑数 II(二)
264. 丑数 II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, ...
- Java实现 蓝桥杯VIP 算法提高 递归倒置字符数组
算法提高 递归倒置字符数组 时间限制:1.0s 内存限制:512.0MB 问题描述 完成一个递归程序,倒置字符数组.并打印实现过程 递归逻辑为: 当字符长度等于1时,直接返回 否则,调换首尾两个字符, ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- 数据的存储结构浅析LSM-Tree和B-tree
目录 顺序存储与哈希索引 SSTable和LSM tree B-Tree 存储结构的比对 小结 本篇主要讨论的是不同存储结构(主要是LSM-tree和B-tree),它们应对的不同场景,所采用的底层存 ...
- Spring源码之自动装配
我们使用Spring开发过程中经常会用到Autowired注解注入依赖的bean,这部分也是面试的热点问题之一.今天咱们一起来深入研究下自动注入的背后实现原理.首先上一个例子,如下所示: @RestC ...