我有现有的Spring MVC Web应用程序。现在我只想使用spring-boot-starter-actuator中的健康检查功能。
我是Spring Boot的新手,所以不确定我是否需要将我的完整项目转换为spring boot项目以进行运行状况检查。我可以只包含依赖项,并以某种方式仅启用所需的功能吗?
答案 0 :(得分:14)
我自己想通了。而不是spring-boot-starter-actuator
我包括spring-boot-actuator
。我不需要使用@SpringBootApplication
初始化应用程序。相反,现在我只导入所需的auto-config类。所以config类现在看起来像这个
@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
取决于ManagementServerProperties
因此必须导入它。这似乎是我的最低配置。如果有更好的选择,请告诉我
答案 1 :(得分:3)
首先,我想告诉sidgate's answer是正确的。 但也许有人需要启用所有指标或选择不同的指标集。
您仍然可以使用spring-boot-starter-actuator
代替spring-boot-actuator
启用所有指标
@org.springframework.context.annotation.Configuration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration
public class MyApplicationContext {
}
其中@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
和@ManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration,
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration
启用Spring Boot配置
启用某些管理端点,您可以直接导入端点的配置,因为sidgate已经解释过
@Configuration
@Import({
org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration.class,
...
})
public class MyApplicationContext {
}
P.S。当然,可以通过注释或xml配置来配置Spring Application Context和Properties初始化。
答案 2 :(得分:0)
如果 smb 在现有的 Spring MVC 项目中寻找 Spring Boot Actuator 2.x 的配置,这是一个对我有用的配置
@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 () -> "/";
}
}
我确实包括了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<version>2.1.18.RELEASE</version>
</dependency>
为了与我一直使用的基线 Spring 版本 (5.1.19.RELEASE) 兼容