繁体   English   中英

在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

[英]Use Spring Boot Actuator without a Spring Boot Application

具有生产信息端点的Spring Boot的Actuator库对任何服务器应用程序都非常有用。 但问题是我找不到集成到传统Spring应用程序(不是Spring BOOT应用程序)的方法。

必须有一些方法来使用执行器的端点,但我无法将它们连接起来。

我有一个JavaConfig类,如下所示

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

public class AppConfig extends WebMvcConfigurerAdapter {

}

但是此配置在部署期间会引发错误。

没有Spring Boot应用程序可以完成这种连线吗?

我在此博客文章中添加了有关如何在非启动应用程序中添加spring 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 Config类中,我添加了以下内容:

 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);  
  }  
}  

如果你想摆脱一个额外的依赖,那么请参阅博客帖子。

UPDATE

此外,您需要确保为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>

我正在研究的项目使用Spring,但是Spring-boot和Spring-MVC都没有。 以下解决方案可能不像启动执行器那样自动化,但它以非常简洁的方式暴露端点。

基本上,所有执行器端点都只是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());
}

}

在我们的项目中,我们使用了一个对我们有用的小黑客。 为了启用执行器,我们在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 {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM