Tutorial describing how to integrate JavaMelody with Spring Boot 2 application to monitor application health.
Please note that this configuration isn’t compatible with Spring Boot 1 nor plain Spring project. For those project we cannot use the javamelody-spring-boot-starter
library with preconfigured JavaMelody beans but should use the javamelody-core
dependency and do the configuration ourself like desbribed here.
Technologies used:
- Spring Boot 2.2.1.RELEASE
- JDK 1.8
- Maven 3
- JavaMelody
Project setup
Project dependencies managed by the Maven’s pom.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <groupId>com.devcases.springboot</groupId> <artifactId>springboot-javamelody</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>net.bull.javamelody</groupId> <artifactId>javamelody-spring-boot-starter</artifactId> <version>1.80.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The explanation of above dependencies:
spring-boot-starter-web
– packages all the necessary dependencies together with the auto configuration for running simple web applicationjavamelody-spring-boot-starter
– provides auto configuration for JavaMelodyspring-boot-maven-plugin
– plugin prividing commands to work with a Spring Boot application (we will be usine thespring-boot:run
command)
Application Initializer
@SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
We don’t have to configure anything for enabling JavaMelody. The javamelody-spring-boot-starter
does all the job.
Controller
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}
Running
The project can be started by the running the following command:
mvn clean spring-boot:run
By visiting the localhost:8080/hello
url, we should see the page with “Hello World” text.
By visiting the localhost:8080/monitoring
url, we can see JavaMelody reports.
The example code you can download from here