How To Enable Actuator In Spring Boot | Spring Boot Examples



 Hi Guys,

Welcome to Java Inspires.

In this post, we will see how to enable/add spring boot actuator to the spring boot application.

To add the actuator to a Maven based project, add the following ‘Starter’ dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

Project Structure:
How To Enable Actuator In Spring Boot | Spring Boot Examples

pom.xml
<?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.4.2</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainspires</groupId>
	<artifactId>actuator-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>actuator-example</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

ActuatorExampleApplication.java
package com.javainspires.actuatorexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActuatorExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(ActuatorExampleApplication.class, args);
	}

}

application.properties
info.app.name = Spring Boot Actuator Example



Startup Log:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-02-06 10:03:01.464  INFO 6516 --- [           main] c.j.a.ActuatorExampleApplication         : Starting ActuatorExampleApplication using Java 15.0.1 on DESKTOP-U1NBKT7 with PID 6516 (C:\Users\developer\Desktop\actuator-example\target\classes started by developer in C:\Users\developer\Desktop\actuator-example)
2021-02-06 10:03:01.470  INFO 6516 --- [           main] c.j.a.ActuatorExampleApplication         : No active profile set, falling back to default profiles: default
2021-02-06 10:03:03.537  INFO 6516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-02-06 10:03:03.572  INFO 6516 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-02-06 10:03:03.572  INFO 6516 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-06 10:03:03.769  INFO 6516 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-02-06 10:03:03.769  INFO 6516 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2195 ms
2021-02-06 10:03:04.457  INFO 6516 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-06 10:03:05.141  INFO 6516 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-02-06 10:03:05.342  INFO 6516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-06 10:03:05.369  INFO 6516 --- [           main] c.j.a.ActuatorExampleApplication         : Started ActuatorExampleApplication in 4.614 seconds (JVM running for 6.094)
2021-02-06 10:03:07.850  INFO 6516 --- [on(3)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-02-06 10:03:07.851  INFO 6516 --- [on(3)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-02-06 10:03:07.852  INFO 6516 --- [on(3)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

Output:


THANK YOU



Post a Comment

Previous Post Next Post