Spring boot health check on existing webapp
Asked Answered
Q

3

13

I have existing Spring MVC web application. Now I just want to use the health check feature present in spring-boot-starter-actuator.

I am new to spring boot, so not sure if I need to convert my complete project to spring boot project for health check to work. Can I just include the dependency and somehow enable only the required feature?

Quintet answered 28/10, 2015 at 6:53 Comment(0)
Q
20

I figured it out myself. Instead of spring-boot-starter-actuator I am including spring-boot-actuator. And I don't need to initialize the application using @SpringBootApplication. Instead now I just import the auto-config classes that are required. So the config class now looks like this

@Configuration
@ComponentScan(basePackages = { "org.example" })
@Import({MyApplicationContext.class, EndpointWebMvcAutoConfiguration.class, 
  ManagementServerPropertiesAutoConfiguration.class, EndpointAutoConfiguration.class, 
  HealthIndicatorAutoConfiguration.class})
@PropertySource("classpath:app.properties")
@EnableWebMvc
public class MyWebApplicationContext {
...
}

EndpointWebMvcAutoConfiguration depends on ManagementServerProperties hence had to import it. This seems to be the bare minimum configuration for me. Let me know if there is any better alternative

Quintet answered 28/10, 2015 at 10:0 Comment(3)
You can also import PublicMetricsAutoConfiguration.class to get jvm/threads metrics available at /metrics endpointScheider
How to monitor such non spring-boot application using spring-boot-admin ?Cauterize
Didn't worked for me. I'm running spring webapp using gretty plugin. any extra configurations needed ??Sower
R
7

First of all I'd like tell that sidgate's answer is correct. But maybe somebody need to enable all metrics or choose different set of metrics.

You still can use spring-boot-starter-actuator instead of spring-boot-actuator

For enabling all metrics

@org.springframework.context.annotation.Configuration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration
public class MyApplicationContext {
}

where @EnableAutoConfiguration

org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.InfoContributorAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsDropwizardAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsChannelAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricExportAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration.class

and @ManagementContextConfiguration

org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration

enables Spring Boot Configurations

For enabling certain management endpoints you could import endpoint's configurations directly as sidgate's already explained

@Configuration
@Import({
org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration.class,

...

})
public class MyApplicationContext {
}

P.S. Of course Spring Application Context and Properties initialization could be configured as always via annotations or xml configuration.

Relentless answered 5/1, 2017 at 15:29 Comment(0)
P
3

If smb is looking for a configuration of Spring Boot Actuator 2.x in an existing Spring MVC project, this is a configuration which worked for me

@Configuration
@Import({
        EndpointAutoConfiguration.class,
        HealthIndicatorAutoConfiguration.class,

        InfoEndpointAutoConfiguration.class,
        HealthEndpointAutoConfiguration.class,

        WebEndpointAutoConfiguration.class,
        ServletManagementContextAutoConfiguration.class,
        ManagementContextAutoConfiguration.class,
})
@EnableConfigurationProperties(CorsEndpointProperties.class)
class ActuatorConfiguration {

    @Bean //taken from WebMvcEndpointManagementContextConfiguration.class
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                                                         ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
                                                                         EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
                                                                         WebEndpointProperties webEndpointProperties) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        EndpointMapping endpointMapping = new EndpointMapping(webEndpointProperties.getBasePath());
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
                corsProperties.toCorsConfiguration(),
                new EndpointLinksResolver(allEndpoints, webEndpointProperties.getBasePath()));
    }

    @Bean
    DispatcherServletPath dispatcherServletPath() {
        return () -> "/";
    }

}

I did include

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        <version>2.1.18.RELEASE</version>
    </dependency>

for compatibility with the baseline Spring version I've been using (5.1.19.RELEASE)

Pyroconductivity answered 18/1, 2021 at 23:45 Comment(2)
For versions > 2.2.0 you should replace HealthIndicatorAutoConfiguration.class with HealthContributorAutoConfiguration docs.spring.io/spring-boot/docs/2.2.0.RELEASE/api/…Canister
Thanks Jakub and Denis. I had a legacy application using spring webmvc 5.x and I was struggling to get actuator working. I was able to use the version 2.5.13, adding false to the latest param of WebMvcEndpointHandlerMapping constructorStyliform

© 2022 - 2024 — McMap. All rights reserved.