一、概述
微服务架构中,由于子系统众多,集群的监控运维变得非常重要。SpringBoot在Starter POMs中提供了spring-boot-starter-actuator模块,这个模块可以自动为SpringBoot构建的应用提供一系列和于监控的端点。
二、使用
1. 准备工作
- 新建一个springboot webservice项目。
- jdk1.8
- idea
- gradle或maven
2. 添加依赖
maven项目里在pom.xml加入依赖:
org.springframework.boot spring-boot-starter-actuator
或在gradle项目里 build.gradle加入:
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.2.2.RELEASE'
加入模块后,重启应用,在控制台看到输出的一些端点定义:
在浏览器输入: http://localhost:9090/actuator,看到输出:
{ "_links":{ "self":{ "href":"http://localhost:9090/actuator", "templated":false }, "health-path":{ "href":"http://localhost:9090/actuator/health/{*path}", "templated":true }, "health":{ "href":"http://localhost:9090/actuator/health", "templated":false }, "info":{ "href":"http://localhost:9090/actuator/info", "templated":false } }}
三、开启验证
1. 配置actuator端口
在application.yaml里设置:
spring: application: name: demoserviceserver: port: 9090management: port: 54001
访问端口的地址:http://localhost:54001/actuator
2. 修改访问地址
spring: application: name: demoservicemanagement: endpoints: web: base-path: "/monitor"
访问平台地址改为:
http://localhost:8080/monitor
3. 开启安全认证
添加依赖
org.springframework.boot spring-boot-starter-security ${boot.starter.version}
或
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.2.2.RELEASE'
配置文件设置:
spring: application: name: demoserviceserver: port: 9090management: server: port: 54001 security: enabled: true
重启应用,控制台可以看到打印的密码:
默认账号是user
5. 设置访问账号
spring: application: name: demoservice security: user: name: admin password: admin
6. 针对某节点加授权
spring: security: basic: path: /bean
四、原生端口说明
原生端点分成三大类:
- 应用配置类
- 度量指标类
- 操作控制类
1. 应用配置类
Springboot采用包扫描和自动化配置的机制加载配置,通过应用配置类端点可以获取关于Spring应用配置内容的详细报告,比如自动化配置的报告、Bean创建的创造、环境属性的报告等。
/autoconfig
用来获取应用的自动化配置报告,包括所有自动化配置的候选项,同时还列出每个候选项是否满足自动化配置的各个先决条件。该端点可以帮助找到一些自动化配置为什么没有生效的具体原因。
该报告把自动化配置分为两个部分:
- positive返回条件匹配成功的自动化配置
- negativeMatches 返回条件匹配不成功的自动化配置
/beans
该端点用来获取应用上下文中创建的所有Bean
访问: http://localhost:8080/actuator/beans
/env
用来获取应用所有可用的环境属性报告,包括:
- 环境变量
- JVM属性
- 应用的配置属性
- 命令行参数
当配置文件里包含一些敏感信息,返回内容中会用*显示。
/mappings
用来返回所有SpringMVC的控制器映射关系报告
/info
用来返回一些应用自定义的信息。默认情况下,该端点只会返回一个空的JSON内容。
可以在application.yaml加入:
info: app: name: spring-boot-acuator version: v1.0.0
再访问http://localhost:54001/actuator/info 节点,可以看到返回报告。
2. 度量指标类
/metrics
用来返回当前应用的各类重要度量指标,如内存、线程、垃圾回收信息。
新版本中有些节点默认不能直接访问,在yaml中按如下配置:
spring: application: name: demoservicemanagement: endpoints: web: exposure: include: "*" //或者 include: metrics,info,health 用,隔开多个
/health
获取应用的各类健康指标信息。
/dump
用来暴露程序运行中的线程信息,它使用java.lang.management.ThreadMXBean的dumpAllThreads方法来返回所有含有同步信息的活动线程详情。
/threaddump
线程dump信息
/trace
该端点用来返回基本的HTTP跟踪信息。默认情况下,跟踪信息的存储采用org.springframework.boot.actuate.trace.InMemoryTraceRepository实现的内存方式,始终保留最近的100条请求记录。
3. 操作控制类
/shutdown
关闭应用
要启动该功能,在配置文件输入:
spring: application: name: demoservicemanagement: endpoint: shutdown: enabled: true sensitive: false endpoints: web: exposure: include: "*"
使用POST请求 http://localhost:8080/acuator/shutdown