Apereo CAS Prometheus Actuator 端点详解:暴露配置、访问安全与 Prometheus 抓取集成
Apereo CAS Prometheus Actuator 端点详解:暴露配置、访问安全与 Prometheus 抓取集成
本文围绕 CAS 文档中的 Actuator Endpoint - Prometheus 页面展开,讲清 /actuator/prometheus 端点在 CAS 监控体系中的定位、它导出的指标内容、如何启用并暴露该端点、如何用 CAS 的端点访问规则保护它,以及如何配置 Prometheus 抓取 CAS 实例。读完本文,你可以独立为一套 CAS 部署打通"指标采集 → 暴露 → 抓取"的完整 Prometheus 监控链路。
端点定位:以 Prometheus 格式导出可抓取指标
CAS 监控文档(Monitoring-Statistics)说明,用于监控和诊断 CAS 内部配置的 Actuator 端点通常暴露于 /actuator 路径下;Actuator 本身是 Spring Boot 的概念而非 CAS 术语。其中 Prometheus 端点在文档站点数据源 actuators.properties 中的官方描述为:
Exposes metrics in a format that can be scraped by a Prometheus server.
即:以可被 Prometheus 服务器抓取(scrape)的格式暴露应用指标。该端点由 Spring Boot 生态提供(属于第三方端点而非 CAS 自有端点),其底层是 Micrometer 指标体系与 Prometheus exposition 格式转换器,这一点可以从当前仓库的依赖清单得到印证(见下文"底层依赖"一节)。
文档站点的每个端点参考页(包括 Actuator-Endpoint-Prometheus)由统一的 Jekyll 模板渲染,端点列表的基准路径标注为 /cas/actuator/,模板逻辑见 actuators.html。
端点导出哪些指标
Prometheus 端点导出的是 CAS 通过 Spring Boot/Micrometer 注册的全部 meter。根据 Configuring-Metrics,CAS 在适用情况下注册以下核心指标:
- 各类内存与缓冲池(memory and buffer pools)
- 垃圾回收(GC)相关统计
- 线程利用率(threads utilization)
- 已加载/卸载的类数量
- CPU 指标
- 文件描述符指标
- Logback 指标:按日志级别记录写入 Logback 的事件数量
- Uptime 指标:应用运行时长 gauge,以及代表应用绝对启动时间的 fixed gauge
- Apache Tomcat 指标
- Spring Integration 指标
此外,自动配置会为所有可用的 DataSource 注入名为 jdbc 的指标,反映连接池中当前活跃、允许最大与最小连接数的 gauge,并按 DataSource 的 bean 名称打标签;Hikari 专属指标以 hikaricp 前缀暴露,按连接池名称打标签;所有可用缓存在启动时以 cache 前缀注入标准化指标及缓存专属指标。
指标采集的支撑模块是 cas-server-support-metrics(org.apereo.cas 组),文档建议将其加入 WAR Overlay 以启用上述 instrumentation。而 /actuator/prometheus 端点本身由 Spring Boot Actuator + Micrometer 提供,无需该模块即可存在,但指标内容的丰富程度取决于应用加载了哪些模块。
启用并暴露 Prometheus 端点
Spring Boot 默认只暴露 info、health、status 三个端点,因此 Prometheus 端点需要显式开启访问权限并加入 Web 暴露列表。文档站点的"Enable & expose"面板(由 cas_actuators.rb 中的 cas_actuator_enable_snippet 过滤器生成,规则见该文件 L134-L139)对单个端点生成如下两条配置:
properties 格式
management.endpoint.prometheus.access=UNRESTRICTED
management.endpoints.web.exposure.include=prometheus
YAML 格式
management:
endpoint:
prometheus:
access: "UNRESTRICTED"
endpoints:
web:
exposure:
include: "prometheus"
环境变量格式
MANAGEMENT_ENDPOINT_PROMETHEUS_ACCESS=UNRESTRICTED
MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=prometheus
两条配置的职责不同:management.endpoint.prometheus.access=UNRESTRICTED 负责打开该端点的访问权限(默认端点访问受限);management.endpoints.web.exposure.include=prometheus 负责把它加入 Web 暴露清单。
端点还可以映射到其他路径,文档给出的通用示例是把 health 映射到 healthcheck:
management.endpoints.web.path-mapping.health=healthcheck
同理可为 prometheus 端点配置路径映射,便于与反代或网关的路由规则对齐。端点设置面板还会列出 CAS 自身的配套属性 cas.monitor.endpoints.form-login-enabled 与 cas.monitor.endpoints.endpoint.*,用于控制端点的表单登录行为及端点级访问规则。
访问安全:谁可以调用该端点
文档站点的"Security"面板强调:端点一旦暴露,每次调用都会经过 Spring Security,而 CAS 按端点维度决定访问策略——若未配置任何规则,则命中特殊的 defaults 端点规则,该规则默认拒绝访问。
针对 prometheus 端点,可配置的访问规则(规则键由 cas_actuators.rb L12-L17 的 SECURITY_RULES 定义)如下,其中 127.0.0.1 等值为示例占位,实际部署需按环境修改:
仅认证用户
cas.monitor.endpoints.endpoint.prometheus.access=AUTHENTICATED
按角色
cas.monitor.endpoints.endpoint.prometheus.access=ROLE
cas.monitor.endpoints.endpoint.prometheus.required-roles=ADMIN
按 IP 地址
cas.monitor.endpoints.endpoint.prometheus.access=IP_ADDRESS
cas.monitor.endpoints.endpoint.prometheus.required-ip-addresses=127.0.0.1
允许任意访问(不推荐用于生产)
cas.monitor.endpoints.endpoint.prometheus.access=PERMIT
认证凭据的来源支持五种方式(Basic / JAAS / LDAP / JDBC / JSON):
- Basic:单个用户定义在 CAS 配置中(
spring.security.user.*属性);若用户名和密码均未定义,CAS 会在启动时生成。 - JAAS / LDAP / JDBC:分别对应
cas.monitor.endpoints.jaas.*、cas.monitor.endpoints.ldap.*、cas.monitor.endpoints.jdbc.*属性组,从相应认证体系校验凭据。 - JSON:静态用户清单(用户名、密码、角色)存放在 JSON 文件中,由
cas.monitor.endpoints.json.*属性指定。支持的密码编码包括{sha512}、{sha256}、{bcrypt}、{noop}、{pbkdf2}、{scrypt}与{argon2}。JSON 格式示例(来自文档模板 actuators.html):
[
{
"username": "casuser",
"password": "{sha512}<hashed-password>",
"authorities": [ "ROLE_ADMIN" ]
}
]
对于纯指标抓取这类机器对机器(M2M)场景,通常采用 IP_ADDRESS 规则限定 Prometheus 服务器地址,或使用 Basic 凭据配合 AUTHENTICATED/ROLE 规则。
配置 Prometheus 抓取 CAS
Prometheus 的工作方式是主动轮询各应用实例的指标地址。Spring Boot 提供的 /actuator/prometheus 端点即是以 Prometheus 期望的 exposition 格式呈现抓取内容。Configuring-Metrics-Storage-Prometheus 给出了 prometheus.yml 中应追加的 scrape_config 示例:
scrape_configs:
- job_name: 'spring'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:PORT']
将 HOST:PORT 替换为 CAS 实例的实际地址即可;多实例部署时为每个实例添加一条 target。如果 CAS 部署在 Servlet 容器且上下文路径为 /cas,需注意抓取路径前缀与 management.endpoints.web.base-path 的实际取值保持一致。
底层依赖佐证
从当前仓库的版本目录 libs.versions.toml 可以确认该端点的技术底座:
- Micrometer 版本为
1.18.0-M2(L178),同时声明了io.micrometer:micrometer-registry-prometheus依赖(L560),这是 Micrometer 官方 Prometheus 注册表,负责把应用内注册的 meter 转换为 Prometheus exposition 格式; - 另声明了
io.prometheus:prometheus-metrics-exposition-formats,版本1.9.0(L208、L623),提供 exposition 格式的文本渲染能力。
这两条依赖说明 /actuator/prometheus 的格式转换与 CAS 自身无关,完全由 Micrometer/Spring Boot 生态承担,CAS 侧的贡献在于注册业务与基础设施指标(JVM、JDBC、缓存、Tomcat 等)。
故障排查
文档站点的"Troubleshooting"面板给出两条最常见的排查线索:
- 返回
404:端点未启用或未暴露,检查 Enable & expose 一节的两条配置是否同时存在; - 返回
401或403:访问规则或凭据拒绝了调用,检查 Security 一节的cas.monitor.endpoints.endpoint.prometheus.*配置与凭据来源。
需要更细的日志时,可在 log4j 配置中调高端点所属包与 Spring Security 的日志级别,文档模板给出的配置形态为(包名按 prometheus 端点实际的 owner 包替换):
<Logger name="org.springframework.boot.actuate.metrics" level="debug" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
<Logger name="org.springframework.security" level="debug" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="file" />
</Logger>
小结与延伸阅读
Prometheus 端点是 CAS 指标体系对外输出的标准出口:cas-server-support-metrics 及自动配置决定"有什么指标",management.endpoint.prometheus.access 与 management.endpoints.web.exposure.include 决定"端点是否可用",cas.monitor.endpoints.endpoint.prometheus.* 决定"谁能调用",Prometheus 的 scrape_configs 决定"如何收集"。相关延伸阅读:
- Actuator Endpoint - Metrics:以 JSON 形式查询指标与 meter 明细;
- Configuring-Metrics:指标清单与自定义;
- Configuring-Metrics-Storage-Prometheus:Prometheus 抓取配置;
- Configuring-Metrics-Storage:Datadog、CloudWatch、InfluxDB 等其它指标存储后端。