不使用Spring Boot应用程序如何使用Spring Boot Actuator

12

Spring Boot 的 Actuator 库提供了生产信息端点,对于任何服务器应用都非常有用。但问题在于我找不到一种将其集成到传统的 Spring 应用程序中(而不是 Spring BOOT 应用程序)的方法。

一定有办法可以使用 actuator 的端点,但我无法将它们连起来。

我有一个像下面这样的 JavaConfig 类

@Configuration
@ComponentScan(basePackages = { "com.company.helper", "org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })

public class AppConfig extends WebMvcConfigurerAdapter {

}

但是在部署期间,这种配置会导致错误。

这种连线是否可以在没有Spring Boot应用程序的情况下完成?


通过复制Spring Boot的配置和bean,您应该能够实现。但是为什么不直接将Spring Boot添加到您的应用程序中呢?您不会失去任何东西,只会获得更多的优势。 - M. Deinum
3个回答

12

我已经在这篇博客文章中添加了关于如何将Spring Boot Actuator添加到非Boot应用程序的信息。

http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html

在应用程序的build.gradle文件中,我添加了以下依赖项。

compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
    exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'}

在应用程序的Spring配置类中,我添加了以下内容:

 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;  
 import org.springframework.boot.actuate.endpoint.BeansEndpoint;  
 import org.springframework.boot.actuate.endpoint.HealthEndpoint;  
 import org.springframework.boot.actuate.endpoint.InfoEndpoint;  
 import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;  
 import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;  

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

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

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

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

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

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

如果你想要减少一个额外的依赖项,那么请参考博客文章。

更新

同时,您需要确保已经定义了RequestMappingHandlerAdapter的bean,如果没有,则ServletDispatcher将无法获取您的HealthMvcEndpoint处理程序的适配器。

如果您没有它,只需将其添加到bean配置文件中。

xml配置:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json" />
        <property name="prettyPrint" value="true" />
    </bean>

当我访问健康检查端点时,我总是得到一个未知状态的响应。你能从这个配置中获取有效的健康状态信息吗? - mad_fox
是的,我们确实可以获取健康状态。您需要添加 @ComponentScan("my.domain.package.of.health.indicators") 以便Spring引用您的健康指标。 - Prateek Negi
如果我使用这个,所有的执行器都会默认启用吗?还是需要在application.properties中进行配置?另外,端口是否默认为8080? - Loren

5
我正在处理的项目使用Spring框架,但不使用Spring-boot或Spring-MVC。下面的解决方案可能不像actuator与boot一样自动化,但以相当简洁的方式暴露了端点。基本上,所有actuator端点都只是bean,因此您可以创建一个新组件,并根据需要自动装配端点。在我的pom中,唯一的额外依赖项是spring-boot-actuator和spring-webmvc。
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>1.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId> 
        <version>4.1.4.RELEASE</version>
    </dependency>

接下来你只需要创建一个单一的组件类(如果需要,可以进行注册)。请确保使用 @EnableAutoConfiguration 进行注释:

@Component
@EnableAutoConfiguration
@Path("/actuator/")
public class ActuatorResource {

private ObjectMapper mapper = new ObjectMapper();


@Autowired 
private DumpEndpoint dumpEndpoint;

@GET
@Produces("application/json")
@Path("/dump")
@Transactional(readOnly = true)
public String getDump() throws JsonProcessingException { 
    return mapper.writeValueAsString(dumpEndpoint.invoke());
}

@Autowired
private EnvironmentEndpoint envEndpoint;

@GET
@Produces("application/json")
@Path("/environment")
@Transactional(readOnly = true)
public String getEnvironment() throws JsonProcessingException {
    return mapper.writeValueAsString(envEndpoint.invoke());
}

}

请查看此帖子:https://stackoverflow.com/questions/32383875/does-having-spring-boot-actuator-changes-the-context-root-of-the-spring-applicat - brain storm

2
在我们的项目中,我们使用了一个小技巧,对我们很有效。为了启用执行器,我们在POM中使用了spring-boot的依赖项。
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>1.2.3.RELEASE</version>
        <type>jar</type>
    </dependency>
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.3.2.Final</version>
    </dependency>

并且只需使用以下附加配置类:
@EnableConfigurationProperties
@Configuration
@EnableAutoConfiguration
@Import(EndpointAutoConfiguration.class)
public class SpringBootActuatorConfig {

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接