4

我们开发了一个基于 Spring (4.1.1) 的 Web 应用程序,我正在尝试集成 Actuator 以获得一些不错的信息。

我们的堆栈:Spring 4.1.1

它是一个非引导应用程序,因此在阅读了很多问题和文章后,我将以下代码放在一起:

pom.xml

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

ActuatorConfig.java

@Configuration
@Import(EndpointAutoConfiguration.class)
public class ActuatorConfig {


    @Bean
    @Autowired
    public ManagementServerProperties myManagementServerProperties() {
        ManagementServerProperties manag = new ManagementServerProperties();
        manag.setContextPath("/actuator");

        return manag;
    }

    @Bean
    @Autowired
    // Define the HandlerMapping similar to RequestHandlerMapping to expose the
    @DependsOn("myManagementServerProperties")
    public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
        return new EndpointHandlerMapping(endpoints);
    }

    @Bean
    @Autowired
    // define the HealthPoint endpoint
    @DependsOn("myManagementServerProperties")
    public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate) {
        return new HealthMvcEndpoint(delegate);
    }

    @Bean
    @Autowired
    // define the Info endpoint
    @DependsOn("myManagementServerProperties")
    public EndpointMvcAdapter infoMvcEndPoint(InfoEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }

    @Bean
    @Autowired
    // define the beans endpoint
    @DependsOn("myManagementServerProperties")
    public EndpointMvcAdapter beansEndPoint(BeansEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }

    @Bean
    @Autowired
    // define the mappings endpoint
    @DependsOn("myManagementServerProperties")
    public EndpointMvcAdapter requestMappingEndPoint(RequestMappingEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }

    @Bean
    @Autowired
    // define the HealthPoint endpoint
    @DependsOn("myManagementServerProperties")
    public EndpointMvcAdapter dumpMvcEndpoint(DumpEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }

    @Bean
    @Autowired
    // define the MetricsEndpoint endpoint
    @DependsOn("myManagementServerProperties")
    public MetricsMvcEndpoint metricsMvcEndpoint(MetricsEndpoint delegate) {
        return new MetricsMvcEndpoint(delegate);
    }

    @Bean
    @Autowired
    @DependsOn("myManagementServerProperties")
    public EnvironmentMvcEndpoint environmentMvcEndpoint(EnvironmentEndpoint delegate) {
        return new EnvironmentMvcEndpoint(delegate);
    }

使用此配置,我实际上可以调用我声明的那些端点:

2018-02-09 12:33:47,083 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/beans],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/health],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke()
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-02-09 12:33:47,084 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-02-09 12:33:47,085 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2018-02-09 12:33:47,085 [localhost-startStop-1] INFO  org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Mapped "{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()

我想要实现的是将所有这些端点的基本路径更改为\actuator之类的东西,以便我可以调用:

https://mydomain:8443/myapp/api/actuator/dump
https://mydomain:8443/myapp/api/actuator/metrics

鉴于它是一个非启动应用程序,我在网上查看后尝试的是将此配置添加到我的 ActuatorConfig 类中:

@Bean
        @Autowired
        public ManagementServerProperties myManagementServerProperties() {
            ManagementServerProperties manag = new ManagementServerProperties();
            manag.setContextPath("/actuator");

            return manag;
        }

我尝试使用@DependsOn注释来确保 myManagementServerProperties 在 Actuator 端点之前被实例化,但没有任何改变。

我究竟做错了什么?

4

0 回答 0