详解spring-boot actuator(监控)配置和使用

作者:Clement-Xu 时间:2022-07-12 17:20:37 

在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:

1、引入依赖:


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

如果使用http调用的方式,还需要这个依赖:


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

2、配置:

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


server:
port: 8082
management:
port: 54001
health:
 mail:
  enabled: false

3、使用:

查看health指标:http://localhost:54001/health


{"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}

4、自定义指标:

4.1 /health:在某个类中implements HealthIndicator接口,然后实现其中的health()方法即可:

代码:


@SpringBootApplication
@EnableScheduling
public class MySpringBootApplication implements HealthIndicator{
 private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class);

public static void main(String[] args) {
   SpringApplication.run(MySpringBootApplication.class, args);
   logger.info("My Spring Boot Application Started");
 }

/**
  * 在/health接口调用的时候,返回多一个属性:"mySpringBootApplication":{"status":"UP","hello":"world"}
  */
 @Override
 public Health health() {
   return Health.up().withDetail("hello", "world").build();
 }
}

/health 运行结果(注意第二个指标):

{"status":"UP","mySpringBootApplication":{"status":"UP","hello":"world"},"diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}

4.2 /info:配置如下,可以直接给一个字符串,也可以从pom.xml配置中获取


info:
app:
 name: "@project.name@" #从pom.xml中获取
 description: "@project.description@"
 version: "@project.version@"
 spring-boot-version: "@project.parent.version@"

/info的结果如下:

{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}

官网:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready

源代码参考:https://github.com/xujijun/my-spring-boot

来源:http://blog.csdn.net/clementad/article/details/52045495

标签:spring,boot,actuator
0
投稿

猜你喜欢

  • java实现切割wav音频文件的方法详解【附外部jar包下载】

    2021-07-08 22:29:56
  • Mybatis接口式编程的原理

    2023-11-27 22:16:05
  • 使用@Validated 和 BindingResult 遇到的坑及解决

    2022-12-18 20:36:28
  • Spring MVC接口防数据篡改和重复提交

    2023-11-29 15:02:11
  • Spring依赖注入的三种方式小结

    2022-08-09 15:56:41
  • Java构建JDBC应用程序的实例操作

    2023-08-07 12:09:13
  • Java线程池submit阻塞获取结果的实现原理详解

    2021-08-29 03:55:45
  • java面试题之try中含return语句时代码的执行顺序详解

    2023-11-24 07:34:16
  • Java数据结构与算法之循环队列的实现

    2023-11-02 11:51:29
  • 浅谈自定义校验注解ConstraintValidator

    2023-07-06 03:10:53
  • Java基本语法之内部类示例详解

    2023-02-06 14:05:32
  • java_object的简单使用详解

    2023-08-22 11:35:57
  • 详解Lombok安装及Spring Boot集成Lombok

    2023-11-28 23:39:55
  • Spring boot配置绑定和配置属性校验的方式详解

    2022-04-21 03:06:06
  • 基于Java信号量解决死锁过程解析

    2023-05-13 22:23:02
  • Java基础之finally语句与return语句详解

    2021-11-27 19:21:22
  • Flutter 分页功能表格控件详细解析

    2023-09-22 20:02:45
  • 聊聊Redis的单线程模型

    2022-02-21 09:20:42
  • JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解

    2023-11-14 00:27:55
  • Swing拆分窗格控件JSplitPane使用详解

    2022-11-14 21:13:48
  • asp之家 软件编程 m.aspxhome.com