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

如何在非Spring Boot的Spring MVC 5应用中集成Spring Boot Actuator 2.x?

Integrating Spring Boot Actuator 2.x with Plain Spring MVC 5

Absolutely! I’ve successfully integrated Spring Boot Actuator 2.x into a legacy Spring MVC 5 application (no full Spring Boot conversion needed). Since Actuator 2.x relies heavily on auto-configuration that’s missing in plain Spring MVC, we just need to manually replicate the critical setup pieces. Here’s a step-by-step guide:

Step 1: Add Required Dependencies

First, add the Actuator core and auto-configuration libraries to your build file. We’ll exclude Spring Boot starters to avoid pulling in unnecessary Boot-specific dependencies (since you’re sticking with plain Spring MVC).

Maven (pom.xml)

<dependencies>
    <!-- Spring Boot Actuator Core -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>2.7.14</version> <!-- Use the latest 2.x version compatible with Spring 5.2+ -->
    </dependency>
    <!-- Actuator Auto-configuration (we'll use selective parts) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        <version>2.7.14</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- For JSON serialization of endpoint responses (e.g., /actuator/health) -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>

Gradle (build.gradle)

dependencies {
    implementation 'org.springframework.boot:spring-boot-actuator:2.7.14'
    implementation('org.springframework.boot:spring-boot-actuator-autoconfigure:2.7.14') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web'
    }
    implementation 'com.fasterxml.jackson.core:jackson-databind'
}

Step 2: Create an Actuator Configuration Class

We need to import the auto-configuration classes for the endpoints you want to use, and manually wire up any required beans. This replicates what Spring Boot’s auto-config would do.

import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({
    HealthEndpointAutoConfiguration.class, // Enables /actuator/health
    InfoEndpointAutoConfiguration.class,   // Enables /actuator/info
    MetricsEndpointAutoConfiguration.class // Optional: Enables /actuator/metrics
    // Add other endpoint auto-config classes here as needed (e.g., LoggersEndpointAutoConfiguration)
})
public class ActuatorConfig {
    // You can customize endpoint behavior here if needed
    // Example: Override the InfoEndpoint to return custom app metadata
    /*
    @Bean
    public InfoEndpoint infoEndpoint() {
        return new InfoEndpoint(() -> Map.of(
            "application", "My Spring MVC App",
            "version", "1.0.0",
            "environment", System.getenv("ENVIRONMENT")
        ));
    }
    */
}

Step 3: Map Actuator Endpoints to Spring MVC

Actuator 2.x uses a dedicated handler mapping to route requests to its endpoints. We need to register this mapping with Spring MVC so it recognizes the /actuator/* paths.

import org.springframework.boot.actuate.endpoint.web.PathMapper;
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class ActuatorWebMvcConfig {

    @Bean
    public WebMvcEndpointHandlerMapping webMvcEndpointHandlerMapping(
            WebEndpointsSupplier webEndpointsSupplier,
            Environment environment,
            PathMapper pathMapper) {
        WebMvcEndpointHandlerMapping mapping = new WebMvcEndpointHandlerMapping(
                webEndpointsSupplier, environment, pathMapper);
        mapping.setPrefix("/actuator"); // Sets the base path for all Actuator endpoints
        return mapping;
    }
}

Step 4: Configure Endpoint Exposure

Use a properties file (e.g., application.properties) to control which endpoints are exposed over HTTP, and customize their behavior:

# Expose all Actuator endpoints (or list specific ones, e.g., health,info,metrics)
management.endpoints.web.exposure.include=*

# Show full details in the health endpoint (useful for debugging)
management.endpoint.health.show-details=always

# Optional: Customize the base path if you don't want to use /actuator
# management.endpoints.web.base-path=/admin/metrics

Key Notes & Troubleshooting

  • Version Compatibility: Ensure your Spring Framework version is 5.2 or higher (Spring Boot 2.x Actuator requires this).
  • Metrics Support: If you added MetricsEndpointAutoConfiguration, you’ll need to add Micrometer dependencies (io.micrometer:micrometer-core) to collect metrics. You can also add a registry like Prometheus if needed.
  • Security: If your app uses Spring Security, you’ll need to allow access to /actuator/* endpoints (add rules to your SecurityConfig to permit these paths).

Once you’ve completed these steps, start your Spring MVC app and test the endpoints:

  • GET /actuator/health - Returns application health status
  • GET /actuator/info - Returns custom app info (if configured)
  • GET /actuator/metrics - Returns collected metrics (if enabled)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:06:05