【问题标题】:Actuator 2.X without Spring Boot没有 Spring Boot 的执行器 2.X
【发布时间】:2019-04-24 08:57:25
【问题描述】:

按照一些链接,我尝试在没有 Spring Boot 的情况下设置执行器 2.X,但没有帮助。

尝试使用/health/application/health/actuator/health,但没有一个成功。我之前使用了 Actuator 1.X,只需将 EndpointWebMvcManagementContextConfigurationEndpointAutoConfigurationPublicMetricsAutoConfigurationHealthIndicatorAutoConfiguration 添加到我的 xml 上下文和 pom 依赖项中,它就可以顺利运行。

现在我的要求是动态添加/删除健康指标,因此需要继续使用 Actuator 2.X。

【问题讨论】:

    标签: java spring-boot spring-boot-actuator


    【解决方案1】:

    我最近一直在尝试将 Spring 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 () -> "/";
        }
    
    }
    

    我确实包括了

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

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

    执行器端点以/actuator/* 公开

    【讨论】:

      【解决方案2】:

      在 Spring Boot 2.x 中,端点已更改。你可以参考这个 Exposing Endpoints 详细的迁移指南在这里detailed migration guide

      要更改公开的端点,请使用以下特定于技术的包含和排除属性:

      application.properties

      management.endpoints.web.exposure.include=*
      

      输出

            {"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"auditevents":{"href":"http://localhost:8080/actuator/auditevents","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},
      ..................
      ...........
      ...................
      }}}
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2019-05-02
        • 2018-11-29
        • 2018-08-27
        • 2022-08-20
        • 1970-01-01
        • 2018-05-26
        • 2014-04-07
        • 2018-07-23
        相关资源
        最近更新 更多