SpringBoot四大神器之Actuator

使用Actuator可以在SpringBoot程序运行时通过http请求的方式动态的监控和管理程序,官方对其功能的介绍如下:

Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.

一、基础配置

想要在项目中使用Actuator,则需要导入Starter依赖:

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

Actuator中提供了端点的概念,即endpoint,想要监控或操作Actuator的某一功能,一定要将其对相应的端点开放。

server:
  port: 8080
management:
  endpoints:
    web:
      exposure:
        include: "*" # 暴露所有端点

配置完成后,启动项目,访问:http://localhost:8080/actuator,出现了一系列的可操作路径。
在这里插入图片描述
访问以上路径就可以对SpringBoot程序进行动态监控和操作了,大致包含以下的功能:

No.endpoint描述
1beans注册到Sprng容器中的Bean对象集合
2caches缓存信息
3health应用的健康状态
4info应用的基本信息,需要手工配置
5conditions自动配置生效的条件
6configprops获取所有的配置属性
7auditevents显示应用暴露的审计事件 (比如认证进入、订单失败)
8metrics应用多样的度量信息
9loggers日志配置
10httptraceHTTP足迹,显示最近100个HTTP request/repsponse
11env当前的环境特性
12flyway显示数据库迁移路径的详细信息
13shutdown关闭应用
14mappings所有的@RequestMapping路径
15scheduledtask应用中的调度任务
16threaddump线程信息
17heapdumpJVM堆dump

二、heapdump信息

SpringBoot程序运行在JVM上,是JVM的一个执行线程,Actuator提供了heapdump端点可以获取到Springboot所运行JVM的实时情况,访问:http://localhost:8080/actuator/heapdump,可以进行dump文件的下载。
在这里插入图片描述
使用visualvm可以打开dump文件,在jdk6到jdk8中,visualvm是默认集成的,从jdk9开始,jdk默认不再集成visualvm,需要到github下载安装(下载地址)。
在这里插入图片描述
打开visualvm,点击左上角file->load导入dump文件即可查看jvm运行信息(导出dump文件时刻的jvm信息)。
在这里插入图片描述

三、服务信息

访问info端点可以查看当前程序的描述信息,这需要开发者手工配置信息(实际上info是一个Map集合,再application,yml中编写信息即可)。

server:
  port: 8080
management:
  endpoints:
    web:
      exposure:
        include: "*" # 暴露所有端点
info:
  app:
    name: springboot项目
    group: com.it
    version: 1.0.0
    description: 测试info
    author: NicholasGUB

访问:http://localhost:8080/actuator/info:
在这里插入图片描述
以上的形式虽然能完成信息的自定义,但是将信息写在配置文件中导致程序启动后无法修改,想要灵活的修改程序的描述信息,那么描述信息应该在数据库中获取。

新建Info构建器需要实现InfoContributor接口:

@Component
public class ServerInfoContributor implements InfoContributor { // info构建器

    @Override
    public void contribute(Info.Builder builder) {
        // 此处可以读取数据库
        builder.withDetail("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

重新访问info端点,发现定义在构建器中的内容正常显示,并且和application.yml中的内容共存。
在这里插入图片描述

四、服务健康状态

访问health端点可以查看当前服务(程序)的健康状态,此功能在SpringCloud中较为常用,随着业务的增多和微服务的流行,难免要将一个服务拆分为多个。
在这里插入图片描述
当服务拆分为多个之后,开发者(运维人员)需要监控每一个服务的健康状态,当服务出现故障之后,需要将服务信息返回给管理者方便修复。通常情况下,服务的健康状态由服务内引入组件的健康状态决定,当所有组件全部正常运行时,此服务才是健康的。
在这里插入图片描述

actuator提供了健康断言接口HealthIndicator,实现此接口可以编写服务健康的判断逻辑。

@Component
public class DefaultHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        boolean isHealth = true;
        // 此处应判断各个组件是否连接正常,其中一个连接异常则设置isHealth = false
        isHealth = false; // 假如redis连接异常
        String errorMsg = "redis连接异常";
        if(!isHealth) {
            return Health.down().withDetail("message",errorMsg).build();
        }
        return Health.up().build();
    }
}

修改application.yml文件,显示不健康时的异常信息。

management:
  endpoint:
    health:
      show-details: always # 总是显示异常信息

访问:http://localhost:8080/actuator/health,此时status的值为down,服务是不健康的并且在details中显示了异常原因。
在这里插入图片描述

五、远程结束服务

actuator提供有shutdown端点可以在SpringBoot程序运行时,调用/actuator/shutdown接口远程结束程序,此功能默认是关闭的,需要配置application.yml文件开启。

server:
  port: 8080
management:
  server:
    port: 8081 # actuator监听8081端口
  endpoint:
    shutdown:
      enabled: true # 开启shutdown功能

shutdown端点只能通过POST请求的方式调用,可以使用curl工具模拟(curl下载地址)。
在这里插入图片描述
下载解压后,将curl根目录添加到环境变量,在cmd窗口输入:curl --version,显示如下内容则安装成功。

curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL

在命令行输入curl -X POST "http://localhost:8081/actuator/shutdown"显示如下结果,程序关闭成功。

{“message”:“Shutting down, bye…”}

六、自定义Endpoint

Actuator提供有@Endpoint注解帮助开发者定义自己的Endpoint,自定义端点的请求方式也分为以下三种:

No注解请求方式描述
1@ReadOperationGET读取数据
2@WriteOperationGET写入数据
3@DeleteOperationGET删除数据

新建SelfEndpoint类,添加@Endpoint注解声明endpoint并命名。

@Configuration
@Endpoint(id = "self")
public class SelfEndpoint {

    @ReadOperation
    public Map<String, Object> endpoint(@Selector String selector) { // 接收参数
        Map<String, Object> endpointMap = new HashMap<>();
        endpointMap.put("author", "NicholasGUB");
        endpointMap.put("selector", selector);
        return endpointMap;
    }
}

直接在浏览器输入:http://localhost:8081/actuator/self/selector
在这里插入图片描述

  • 6
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值