没有Spring Boot的Spring Boot Actuator

gre*_*fox 52 java spring spring-mvc spring-boot

我一直在研究Spring/Spring MVC应用程序,我正在寻找性能指标.我遇到过Spring Boot Actuator,它看起来像是一个很好的解决方案.但是我的应用程序不是Spring Boot应用程序.我的应用程序运行在传统容器Tomcat 8中.

我添加了以下依赖项

// Spring Actuator
compile "org.springframework.boot:spring-boot-starter-actuator:1.2.3.RELEASE"
Run Code Online (Sandbox Code Playgroud)

我创建了以下配置类.

@EnableConfigurationProperties
@Configuration
@EnableAutoConfiguration
@Profile(value = {"dev", "test"})
@Import(EndpointAutoConfiguration.class)
public class SpringActuatorConfig {

}
Run Code Online (Sandbox Code Playgroud)

我甚至在每个配置类上添加@EnableConfigurationProperties,如StackOverflow上另一篇文章所建议的那样.然而,这没有做任何事情.端点仍未创建并返回404.

Pyt*_*try 32

首先让我们澄清一下,如果不使用Spring Boot,就不能使用Spring Boot Actuator.

如果没有Spring Boot,我错了.有关如何操作的示例,请参阅@ stefaan-neyts的答案.

我创建了一个示例项目,展示如何使用最少量的Spring Boot自动配置转换基本的SpringMVC应用程序.

原始资料来源:http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example

转换来源:https://github.com/Pytry/minimal-boot-actuator

我可以完全删除dispatcher-servlet.xml和web.xml文件,但我保留它们以显示如何尽可能少地执行更改并简化转换更复杂的项目.

以下是我转换的步骤列表.

转换过程

  • 添加使用@SpringBootApplication注释的Java配置文件
  • 将应用程序配置文件作为bean添加到传统的xml配置(在上下文扫描之后添加它).
  • 将视图解析器移动到Application java配置中.

    或者,将前缀和后缀添加到application.properties.然后,您可以在应用程序中使用@Value注入它们,或者完全删除它,只需使用提供的弹簧启动视图解析程序.我和前者一起去了.

  • 从spring上下文xml中删除了Default上下文侦听器.

    这个很重要!由于spring boot将提供一个,如果你没有,你会得到一个"Error listener Start"异常.

  • 将spring boot插件添加到构建脚本依赖项中(我使用的是gradle)

  • 将mainClassName属性添加到构建文件,并设置为空String(表示不创建可执行文件).

  • 修改弹簧启动执行器的依赖关系

  • 非常有趣的讨论,但通过这种方式,您可以从经典的 Spring MVC 应用程序移植到使用 War 作为包装的 Spring Boot 应用程序 (2认同)

vsi*_*ngh 26

您可以使用没有弹簧靴的执行器.将其添加到pom.xml

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

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后在你的配置类中

@Configuration
@EnableWebMvc
@Import({
        EndpointAutoConfiguration.class , PublicMetricsAutoConfiguration.class , HealthIndicatorAutoConfiguration.class
})
public class MyActuatorConfig {

    @Bean
    @Autowired
    public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
        return new EndpointHandlerMapping(endpoints);
    }

    @Bean
    @Autowired
    public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在应用程序中查看指标

HTTP://本地主机:8085 /指标

Actutaor终点

  • 你好@vsingh,我已经添加了如上所述的配置,但是当我点击“http://localhost:port/metrics”时,它返回“404”。我们是否需要在 application.properties 或 yml 文件中添加其他属性 (3认同)

Ste*_*yts 14

尽管在没有Spring Boot的情况下使用Spring Boot功能并不是一个好主意,但这是可能的!

例如,这种Java配置可以在不使用Spring Boot的情况下使用Spring Boot Actuator Metrics:

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ EndpointAutoConfiguration.class, PublicMetricsAutoConfiguration.class })
public class SpringBootActuatorConfig {

    @Bean
    @Autowired
    public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
        return new EndpointHandlerMapping(endpoints);
    }

    @Bean
    @Autowired
    public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
}
Run Code Online (Sandbox Code Playgroud)

Maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Jak*_*cki 8

由于我们已经有了 Spring Boot Actuator 2.x,因此将执行器包含到现有 Spring MVC 项目的方法可能如下所示:

@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 () -> "/";
    }

}
Run Code Online (Sandbox Code Playgroud)

我确实包括了

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator-autoconfigure</artifactId>
        <version>2.1.18.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

为了与我一直使用的基线 Spring 版本(5.1.19.RELEASE)兼容

  • @Poli 如果您使用的是 2.7 及以上版本,请使用 &gt; HealthContributorAutoConfiguration.class 代替。 (2认同)

sid*_*ate 5

虽然答案已被接受,但我想到了更新我的经验.我不想将我的应用程序转换为弹簧启动使用@SpringBootApplication.请参阅另一个问题,其中我提到了所需的最低代码.