Apereo CAS 之 startup 执行器端点:用 BufferingApplicationStartup 剖析应用启动过程
Apereo CAS 之 startup 执行器端点:用 BufferingApplicationStartup 剖析应用启动过程
Apereo CAS 通过 startup actuator 端点暴露 Spring Boot 在应用启动阶段采集的每一步事件数据,用于回答“CAS 启动到底慢在哪里”这类问题。该端点并不开箱即用:它要求 SpringApplication 在构建时配置了 BufferingApplicationStartup,而 CAS 又默认不启用启动事件记录(no-op),必须通过系统属性 -DCAS_APP_STARTUP=buffering 显式打开。读完本文,你可以掌握三件事:如何用 JVM 参数让 CAS 开始记录启动事件、如何用 actuator 属性将 startup 端点暴露到 Web 并配置访问安全规则,以及如何在 CAS 源码中验证这一整套机制。
端点的作用与前提
startup 端点显示由 Spring ApplicationStartup 接口采集的启动步骤数据:Spring 容器在解析配置类、创建 Bean、评估自动装配条件等生命周期节点上会调用 ApplicationStartup.startup(StartupStep) 记录事件,这些数据最终可以转换为 JFR/JSON 事件序列,帮助你在 Spring context 生命周期内定位耗时段。
前提条件在官方文档 startup 端点 中一句话说明:
Shows the startup steps data collected by the
ApplicationStartup. Requires theSpringApplicationto be configured with aBufferingApplicationStartup.
也就是说,仅仅暴露端点还不够——如果底层没有 BufferingApplicationStartup 在内存中缓冲事件,端点将没有数据可返回。
第一步:用系统属性开启启动事件采集
CAS 将启动事件采集设计为**可选开启(opt-in)**的 JVM 系统属性。文档 Configuration-Management.md 给出了完整的取值表:
| Type | Description |
|---|---|
default |
Default startup type which acts as a no-op. |
buffering |
Record events into memory using a pre-defined capacity and expose them via the startup actuator endpoint. |
jfr |
Add startup events to a Java Flight Recorder session for profiling applications and correlating their Spring context lifecycle. |
对应的启动命令为:
java -DCAS_APP_STARTUP=buffering -jar cas.war
三种取值分别对应内存缓冲(供 startup 端点消费)与 JFR 飞行记录器(供事后用 JFR 工具分析)。不设置该属性时行为等同于 default,即不记录任何事件。
源码实现:ApplicationUtils.getApplicationStartup()
这一行为的核心实现在 ApplicationUtils,值得完整看一下:
public static final String SYSTEM_PROPERTY_APP_STARTUP = "CAS_APP_STARTUP";
private static final int APPLICATION_EVENTS_CAPACITY = 10_000;
public static ApplicationStartup getApplicationStartup() {
val type = StringUtils.defaultIfBlank(System.getProperty(SYSTEM_PROPERTY_APP_STARTUP), "default");
return switch (type.toLowerCase(Locale.ROOT)) {
case "jfr" -> new FlightRecorderApplicationStartup();
case "buffering" -> new BufferingApplicationStartup(APPLICATION_EVENTS_CAPACITY);
default -> ApplicationStartup.DEFAULT;
};
}
从源码结构看有几个关键点:
- 缓冲容量固定为 10,000:
BufferingApplicationStartup(APPLICATION_EVENTS_CAPACITY)中容量是常量10_000,事件在内部ArrayDeque中环形覆盖,超出部分丢弃。对 CAS 这种 Bean 数量庞大的应用来说,这个容量足以覆盖完整的启动序列。 - 默认值与未知值都回落到 no-op:
default -> ApplicationStartup.DEFAULT。未设置属性、拼错属性值(如Buffering之外的写法,如unknown-strategy)都不会开启记录,避免生产环境无意中付出记录每个 Bean 实例化的时间与内存开销。源码注释明确说明了这一设计动机:“recording every bean instantiation, configuration class parse and condition evaluation costs time and memory on every deployment, while the data it produces is only reachable through the startup actuator endpoint”。 - 大小写不敏感:
type.toLowerCase(Locale.ENGLISH)保证BUFFERING、Buffering均可识别。
在哪里被装配进 SpringApplication
ApplicationUtils.getApplicationStartup() 的结果被注入到 CAS 的所有启动入口:
- 嵌入式启动(
java -jar):CasWebApplication 的main方法中,SpringApplicationBuilder显式调用.applicationStartup(ApplicationUtils.getApplicationStartup()); - Tomcat WAR 部署:CasWebApplicationServletInitializer 在构造函数中将同一实例传给父类
AbstractCasSpringBootServletInitializer; - GraalVM 原生镜像:CasNativeWebApplication 同样通过
.applicationStartup(ApplicationUtils.getApplicationStartup())接入。
三条路径一致,意味着无论以哪种方式运行 CAS,-DCAS_APP_STARTUP 的语义都相同。
测试用例佐证行为
ApplicationUtilsTests 用四个用例锁定了上述行为,可作为理解实现时的“可验证依据”:
@Test
@ClearSystemProperty(key = ApplicationUtils.SYSTEM_PROPERTY_APP_STARTUP)
void verifyStartupIsNotInstrumentedByDefault() {
assertSame(ApplicationStartup.DEFAULT, ApplicationUtils.getApplicationStartup());
}
@Test
@SetSystemProperty(key = ApplicationUtils.SYSTEM_PROPERTY_APP_STARTUP, value = "unknown-strategy")
void verifyUnknownStartupFallsBackToDefault() {
assertSame(ApplicationStartup.DEFAULT, ApplicationUtils.getApplicationStartup());
}
@Test
@SetSystemProperty(key = ApplicationUtils.SYSTEM_PROPERTY_APP_STARTUP, value = "buffering")
void verifyBufferingStartupIsOptIn() {
assertInstanceOf(BufferingApplicationStartup.class, ApplicationUtils.getApplicationStartup());
}
@Test
@SetSystemProperty(key = ApplicationUtils.SYSTEM_PROPERTY_APP_STARTUP, value = "jfr")
void verifyFlightRecorderStartupIsOptIn() {
assertInstanceOf(FlightRecorderApplicationStartup.class, ApplicationUtils.getApplicationStartup());
}
第二步:启用并 Web 暴露 startup 端点
startup 不在 CAS 默认暴露的端点之列——按 CAS 文档的约定,默认只暴露 info、health 和 status 三个端点。要访问 /actuator/startup,需要在 CAS 配置中做两件事(YAML、Properties 与环境变量任一语法均可):
management:
endpoint:
startup:
access: "UNRESTRICTED"
endpoints:
web:
exposure:
include: "startup"
等价的 Properties 写法:
management.endpoint.startup.access=UNRESTRICTED
management.endpoints.web.exposure.include=startup
其中 management.endpoint.startup.access=UNRESTRICTED 控制端点“允许被暴露”(Spring Boot 端点访问级别),management.endpoints.web.exposure.include=startup 才真正把它挂到 Web 环境上。端点基础路径为 /cas/actuator/,最终请求地址类似 /cas/actuator/startup。如果部署了多个端点,exposure.include 可写成逗号分隔列表。
端点路径还可以重映射,例如把 health 挂到 healthcheck(此技巧对 startup 同样适用,把键名换掉即可):
management.endpoints.web.path-mapping.health=healthcheck
第三步:为端点配置安全规则
一旦端点被 Web 暴露,所有调用都会经过 Spring Security,由 CAS 按端点粒度决定放行策略;未显式配置规则的请求会落到特殊的 defaults 端点规则,其行为是拒绝访问。CAS 通过 cas.monitor.endpoints.endpoint.startup.* 属性支持四类访问规则:
1. 仅限已认证用户(authenticated)
cas:
monitor:
endpoints:
endpoint:
startup:
access: "AUTHENTICATED"
2. 按角色(role)
cas:
monitor:
endpoints:
endpoint:
startup:
access: "ROLE"
required-roles: "ADMIN"
3. 按源 IP 地址(ip)
cas:
monitor:
endpoints:
endpoint:
startup:
access: "IP_ADDRESS"
required-ip-addresses: "127.0.0.1"
4. 完全放行(permit)
cas:
monitor:
endpoints:
endpoint:
startup:
access: "PERMIT"
凭据从哪里来
规则校验通过之后,还需要凭据来源。CAS 的 actuator 端点支持五种认证方式,各自对应一组配置前缀(属性定义可在 属性文档 及 CAS 属性数据中检索):
- Basic(单用户):在 CAS 配置中定义一个用户名/口令。若名称和口令均未定义,CAS 会在启动时自动生成凭据并打印出来。属性前缀为
spring.security.user。 - JAAS:
cas.monitor.endpoints.jaas,通过 JAAS 登录模块验证。 - LDAP:
cas.monitor.endpoints.ldap,连接 LDAP 目录校验用户。 - JDBC:
cas.monitor.endpoints.jdbc,从关系数据库校验。 - JSON(静态用户列表):
cas.monitor.endpoints.json指向一个 JSON 文件,包含静态用户、口令和角色,支持{sha512}、{sha256}、{bcrypt}、{noop}、{pbkdf2}、{scrypt}、{argon2}等口令编码:
[
{
"username": "casuser",
"password": "{sha512}<hashed-password>",
"authorities": [ "ROLE_ADMIN" ]
}
]
对于只做启动剖析的场景,最简单的组合是 Basic 单用户 + access=AUTHENTICATED,用文档示例中的 Basic 用户凭据即可发起带认证的请求。
调用端点查看启动数据
启用上述配置后,可以直接请求端点:
curl -u "casuser:password" "https://sso.example.org/cas/actuator/startup"
请求 Accept 为 application/json(或 application/vnd.spring-boot.jvm.bootstrap.events.v2 等 Spring Boot 定义的事件格式)时,返回的是启动过程中按顺序缓冲的事件序列,每条记录包含事件名(对应 Bean 初始化、条件评估等步骤)、开始时间与持续时间,便于人工或工具(如 JFR 查看器、Spring Boot 的 startup 图表)进一步分析。
注意:如果启动事件没有被记录(即没有设置 -DCAS_APP_STARTUP=buffering 或 jfr),该端点将没有可用的事件数据——这是“配置了端点却查不到东西”最常见的原因。
排障与日志
按 CAS 文档对 actuator 端点的通用排障约定:
- 404:端点未启用或未暴露,检查上文“Enable & expose”一节的两条
management.*属性; - 401 / 403:访问规则或凭据拒绝了调用,检查
cas.monitor.endpoints.endpoint.startup.*安全配置与凭据来源。
需要更多细节时,可在 logback 配置中提高相关包的日志级别(以 Spring Security 与 startup 端点所在包为例):
<Logger name="org.springframework.security" level="debug" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
完整操作清单回顾
把整条链路串起来,一次典型的启动剖析流程是:
- 启动 JVM 时加
-DCAS_APP_STARTUP=buffering,让BufferingApplicationStartup(容量 10,000)开始缓冲启动事件——见 ApplicationUtils; - 配置
management.endpoint.startup.access=UNRESTRICTED与management.endpoints.web.exposure.include=startup,把端点挂到/cas/actuator/startup; - 配置
cas.monitor.endpoints.endpoint.startup.access(及required-roles/required-ip-addresses)与凭据来源(Basic/JAAS/LDAP/JDBC/JSON 之一); - 带凭据请求
/cas/actuator/startup,得到按时间排序的启动步骤数据,定位耗时的 Bean 初始化或条件评估; - 剖析完成后移除
-DCAS_APP_STARTUP参数恢复默认的 no-op 行为,避免常驻的内存与 CPU 开销。
需要强调的适用前提:该端点是 Spring Boot 第三方提供的端点(由 CAS 文档标注为 third-party endpoint),其行为、返回格式与参数以所使用 Spring Boot 版本的 startup 端点实现为准;CAS 侧的定制仅体现在启动事件的采集开关(CAS_APP_STARTUP)与端点的安全规则上。