java 在没有 Spring Boot 应用程序的情况下使用 Spring Boot Actuator
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26913087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Spring Boot Actuator without a Spring Boot Application
提问by LostMohican
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).
Spring Boot 的带有生产信息端点的 Actuator 库对于任何服务器应用程序都非常有用。但问题是我找不到集成到传统 Spring 应用程序(不是 Spring BOOT 应用程序)的方法。
There must be some way to use the endpoints of actuator but I could not wire them up.
必须有某种方法可以使用执行器的端点,但我无法将它们连接起来。
I have a JavaConfig class like below
我有一个像下面这样的 JavaConfig 类
@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.
但是这种配置在部署过程中会引发错误。
Can this wiring be done without the Spring Boot application?
这种接线可以在没有 Spring Boot 应用程序的情况下完成吗?
采纳答案by Prateek Negi
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
http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html
In the application's build.gradle, I added the following dependency
在应用程序的 build.gradle 中,我添加了以下依赖项
compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'}
In the application's Spring Config class, I added the following things:
在应用程序的 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);
}
}
If you want to get rid of one additional dependency then please refer to the blogpost.
如果你想摆脱一个额外的依赖,那么请参考博文。
UPDATE
更新
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.
此外,您还需要确保为 RequestMappingHandlerAdapter 定义了一个 bean,如果没有它,ServletDispatcher 将无法为 HealthMvcEndpoint 的处理程序获取适配器。
if you dont have it just add it to your bean configuration file
如果你没有它,只需将它添加到你的 bean 配置文件中
xml configurations:
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.MappingHymanson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="prettyPrint" value="true" />
</bean>
回答by dneronique
The project I'm working on uses Spring, but neither Spring-boot nor Spring-MVC. The following solution may not be as automagic as the actuator with boot, but it exposes the endpoints in a pretty succinct way.
我正在处理的项目使用 Spring,但既不是 Spring-boot 也不是 Spring-MVC。以下解决方案可能不像带引导的执行器那样自动,但它以非常简洁的方式公开端点。
Basically, all actuator endpoints are just beans, so you can create a new component and autowire in the endpoints however you see fit.
基本上,所有执行器端点都只是 bean,因此您可以在端点中创建一个新组件并自动装配,但您认为合适。
The only additional dependencies in my pom are spring-boot-actuator and spring-webmvc:
我的 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>
Then all you need to do is create a single component class (maybe register it if you need to). Make sure to annotate with @EnableAutoConfiguration:
然后您需要做的就是创建一个单一的组件类(如果需要,可以注册它)。确保使用@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());
}
}
回答by RadekSohlich
In our project we used a little hack, that worked for us. To enable actuator we used dependencies from spring-boot in POM.
在我们的项目中,我们使用了一个小技巧,这对我们有用。为了启用执行器,我们使用了 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>
and just used additional config class as follows:
并仅使用了额外的配置类,如下所示:
@EnableConfigurationProperties
@Configuration
@EnableAutoConfiguration
@Import(EndpointAutoConfiguration.class)
public class SpringBootActuatorConfig {
}