Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

java - Spring Boot Actuator without Spring Boot

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First let's clarify that you cannot use Spring Boot Actuator without using Spring Boot.

I was wrong about not being able to it without Spring Boot. See @stefaan-neyts answer for an example of how to do it.

I created a sample project to show how you could convert a basic SpringMVC application using a minimal amount of Spring Boot auto-configuration.

Original source: http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example

Converted source: 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...