You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring CommandLineRunner应用中启用Actuator REST端点?

解决Spring Boot非Web项目启用Actuator REST端点的问题

你遇到的核心问题是:Actuator的REST端点依赖Spring Web环境支持,而你的项目没有引入spring-boot-starter-web(或类似Web依赖),所以只能通过JMX访问Actuator数据,无法使用REST端点。下面是具体的解决步骤:

1. 添加必要的依赖

因为你的应用是TCP Socket服务器(非Web),要启用Actuator REST端点,必须补充Web环境依赖和Prometheus相关的指标导出依赖。以Maven为例,在pom.xml中添加:

<dependencies>
    <!-- 核心Actuator依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- 引入Spring MVC支持,为Actuator提供Web环境 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 支持将指标导出为Prometheus格式 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

如果用Gradle,对应的依赖配置是:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
}

2. 配置Actuator端点和Web服务

在application.yml(或application.properties)中添加以下配置,确保端点被启用并暴露,同时避免Web端口和你的TCP Socket端口冲突:

spring:
  server:
    port: 8081 # 选择一个未被你的TCP服务占用的端口
management:
  endpoints:
    web:
      exposure:
        include: prometheus, health, info # 按需暴露需要的端点,prometheus是Prometheus必需的
  endpoint:
    prometheus:
      enabled: true # 启用Prometheus端点
  metrics:
    export:
      prometheus:
        enabled: true

如果用application.properties格式:

spring.server.port=8081
management.endpoints.web.exposure.include=prometheus,health,info
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

3. 验证配置

启动你的应用后,访问http://localhost:8081/actuator/prometheus,如果能看到类似以下的Prometheus格式指标数据,说明配置成功:

# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="heap",id="Eden Space",} 3.2457472E7
jvm_memory_used_bytes{area="heap",id="Old Gen",} 0.0
...

关键注意事项

  • 添加spring-boot-starter-web后,Spring Boot会自动启动嵌入式Web容器(默认Tomcat),这不会影响你的TCP Socket服务运行,两者只是占用不同端口,完全独立。
  • 如果不想使用Tomcat,可以替换为Undertow或Jetty,只需排除Tomcat依赖并引入对应容器的starter即可,比如:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    
  • 如果你之前尝试过配置但没生效,检查是否有配置项拼写错误,比如management.endpoints.web.exposure.include是否正确设置,默认只暴露health和info,必须显式添加prometheus。

内容的提问来源于stack exchange,提问作者vivi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:56:15