问题描述
带有生产信息端点的 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
在应用的build.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!