在没有Spring Boot应用程序的情况下使用Spring Boot Actuator [英] Use Spring Boot Actuator without a Spring Boot Application
问题描述
带有生产信息端点的Spring Boot的Actuator库对任何服务器应用程序都非常有用。但问题是我找不到集成到传统Spring应用程序(不是Spring BOOT应用程序)的方法。
Spring Boot's Actuator library with production information endpoints is really useful for any server application. But the problem is I could not find a way to integrate into a traditional Spring Application (which is not a Spring BOOT application).
必须有某种方法来使用执行器的端点,但我无法将它们连接起来。
There must be some way to use the endpoints of actuator but I could not wire them up.
我有一个类似下面的JavaConfig类
I have a JavaConfig class like below
@Configuration
@ComponentScan(basePackages = { "com.company.helper", "org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })
public class AppConfig extends WebMvcConfigurerAdapter {
}
但此配置抛出部署期间出错。
But this configuration throws an error during deployment.
可以在没有Spring Boot应用程序的情况下完成此布线吗?
Can this wiring be done without the Spring Boot application?
推荐答案
我已在此博客文章中添加了有关如何在非启动应用程序中添加弹簧启动执行器的信息
I have added information on how to add spring boot actuator in a non boot application in this blog post
http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html
在应用程序的构建中.gradle,我添加了以下依赖项
In the application's build.gradle, I added the following dependency
compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'}
在应用程序的Spring Config类中,我添加了以下内容:
In the application's Spring Config class, I added the following things:
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);
}
}
如果你想摆脱一个额外的依赖关系然后请参阅博文。
If you want to get rid of one additional dependency then please refer to the blogpost.
更新
此外,您还需要确保你有一个为RequestMappingHandlerAdapter定义的bean,如果你没有它,ServletDispatcher将无法为你的HealthMvcEndpoint的处理程序获取适配器。
Also you need to make sure you have a bean defined for RequestMappingHandlerAdapter, if you do not have it the ServletDispatcher will not be able to fetch the adapter for the handler of your HealthMvcEndpoint.
如果你没有它只是将它添加到你的bean配置文件中
if you dont have it just add it to your bean configuration file
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 Boot应用程序的情况下使用Spring Boot Actuator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!