Java 没有 Spring Boot 的 Spring Boot 执行器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29953157/
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
Spring Boot Actuator without Spring Boot
提问by greyfox
I've been working on a Spring/Spring MVC application and I'm looking to add performance metrics. I've come across Spring Boot Actuator and it looks like a great solution. However my application is not a Spring Boot application. My application is running in a traditional container Tomcat 8.
我一直在研究 Spring/Spring MVC 应用程序,并且希望添加性能指标。我遇到了 Spring Boot Actuator,它看起来是一个很好的解决方案。但是我的应用程序不是 Spring Boot 应用程序。我的应用程序在传统容器 Tomcat 8 中运行。
I added the following dependencies
我添加了以下依赖项
// Spring Actuator
compile "org.springframework.boot:spring-boot-starter-actuator:1.2.3.RELEASE"
I created the following config class.
我创建了以下配置类。
@EnableConfigurationProperties
@Configuration
@EnableAutoConfiguration
@Profile(value = {"dev", "test"})
@Import(EndpointAutoConfiguration.class)
public class SpringActuatorConfig {
}
I even went as far as adding @EnableConfigurationProperties on every configuration class as suggested on another post on StackOverflow. However that didn't do anything. The endpoints are still not being created and return 404s.
我什至按照 StackOverflow 上另一篇文章的建议,在每个配置类上添加 @EnableConfigurationProperties。然而这并没有起到任何作用。端点仍未创建并返回 404。
采纳答案by vsingh
You can use actuator without spring boot. Add this to pom.xml
您可以使用没有弹簧套的执行器。将此添加到 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
And then in your config class
然后在你的配置类中
@Configuration
@EnableWebMvc
@Import({
EndpointAutoConfiguration.class , PublicMetricsAutoConfiguration.class , HealthIndicatorAutoConfiguration.class
})
public class MyActuatorConfig {
@Bean
@Autowired
public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
return new EndpointHandlerMapping(endpoints);
}
@Bean
@Autowired
public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
return new EndpointMvcAdapter(delegate);
}
}
And then you can see the metrics in your application
然后你可以在你的应用程序中看到指标
回答by Rob Baily
You will need to convert your application to Spring Boot if you want to use the components that plug into it. There is a section named Convert an existing application to Spring Boot under the Spring Boot docsthat you will want to review. I have not done this myself but I use Spring Boot and it is relatively simple to configure so hopefully you can get it from here.
如果要使用插入其中的组件,则需要将应用程序转换为 Spring Boot。您需要查看的 Spring Boot 文档下有一个名为 Convert an existing application to Spring Boot 的部分。我自己没有这样做,但我使用 Spring Boot,它的配置相对简单,所以希望你能从这里得到它。
回答by Pytry
First let's clarify that you cannot use Spring Boot Actuator without using Spring Boot.
首先让我们澄清一下,如果不使用 Spring Boot,就不能使用 Spring Boot Actuator。
I was wrong about not being able to it without Spring Boot. See @stefaan-neyts answer for an example of how to do it.
如果没有 Spring Boot,我就无法做到这一点,我错了。有关如何执行此操作的示例,请参阅 @stefaan-neyts 答案。
I created a sample project to show how you could convert a basic SpringMVC application using a minimal amount of Spring Boot auto-configuration.
我创建了一个示例项目来展示如何使用最少的 Spring Boot 自动配置来转换基本的 SpringMVC 应用程序。
Original source: http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example
原始来源:http: //www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example
Converted source: https://github.com/Pytry/minimal-boot-actuator
转换源:https: //github.com/Pytry/minimal-boot-actuator
I could have completely removed the dispatcher-servlet.xml and the web.xml files, but I kept them to show how to perform as minimal a change as possible and to simplify converting more complex projects.
我本可以完全删除 dispatcher-servlet.xml 和 web.xml 文件,但我保留它们是为了展示如何执行尽可能少的更改并简化更复杂项目的转换。
Here is a list of steps I took to convert.
这是我进行转换所采取的步骤列表。
Conversion Process
转换过程
- Add a Java Configuration file annotated with @SpringBootApplication
- Add the Application configuration file as a bean to the traditional xml configuration ( added it just after the context scan).
Move view resolvers into Application java configuration.
Alternatively, add the prefix and suffix to application.properties. You can then inject them with @Value in your application, or delete it entirely and just use the provided spring boot view resolver. I went with the former.
Removed Default context listener from the spring context xml.
This is important! Since spring boot will provide one you will get an "Error listener Start" exception if you do not.
Add the spring boot plugin to your build script dependencies (I was using gradle)
Add a mainClassName property to the build file, and set to an empty String (indicates not to create an executable).
Modify dependencies for spring boot actuator
- 添加一个用@SpringBootApplication注解的Java配置文件
- 将 Application 配置文件作为 bean 添加到传统的 xml 配置中(在上下文扫描之后添加)。
将视图解析器移动到应用程序 java 配置中。
或者,将前缀和后缀添加到 application.properties。然后,您可以在应用程序中使用 @Value 注入它们,或者将其完全删除并仅使用提供的 Spring Boot 视图解析器。我和前者一起去了。
从 spring 上下文 xml 中删除了默认上下文侦听器。
这个很重要!由于 spring boot 将提供一个,如果您不提供,您将收到“错误侦听器启动”异常。
将 spring boot 插件添加到您的构建脚本依赖项中(我使用的是 gradle)
将 mainClassName 属性添加到构建文件,并设置为空字符串(表示不创建可执行文件)。
修改 spring boot 执行器的依赖项
回答by sidgate
Though the answer is already accepted, I thought of updating my experience. I did not want to convert my application to spring boot using @SpringBootApplication
. Refer to another questionwhere I have mentioned the bare minimum code required.
虽然答案已经被接受,但我想更新我的经验。我不想使用@SpringBootApplication
. 请参阅另一个问题,其中我提到了所需的最低限度代码。
回答by Stefaan Neyts
Allthough it is not a good idea to use Spring Boot features without Spring Boot, it is possible!
尽管在没有 Spring Boot 的情况下使用 Spring Boot 功能不是一个好主意,但这是可能的!
For example, this Java configuration makes Spring Boot Actuator Metrics available without using Spring Boot:
例如,此 Java 配置使 Spring Boot Actuator Metrics 在不使用 Spring Boot 的情况下可用:
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ EndpointAutoConfiguration.class, PublicMetricsAutoConfiguration.class })
public class SpringBootActuatorConfig {
@Bean
@Autowired
public EndpointHandlerMapping endpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
return new EndpointHandlerMapping(endpoints);
}
@Bean
@Autowired
public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
return new EndpointMvcAdapter(delegate);
}
}
The Maven dependency:
Maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
回答by saravanan
you have made the mistake by not introducing the @springboot annotation in your code.When you add @springboot ot will consider as boot program by the compiler automatically and addd the required dependency file for it and your actuator dependency file
你犯了一个错误,没有在你的代码中引入 @springboot 注释。当你添加 @springboot 时,编译器会自动将其视为引导程序,并为其添加所需的依赖文件和执行器依赖文件