Enable Actuator in Spring Boot with Examples
Purpose: In this post, we will learn how we can enable Actuator in spring boot and test API. The steps are given below.
What is Actuator: Actuator is mainly used to expose operational information about the running application — health, metrics, info, dump, env, etc.
After adding actuator dependency in the classpath, several endpoints are available for us. As with most Spring modules.
1. Setup and Create Project: Visit here. Spring Boot Rest API Hello World Examples.
Next Step. Spring Boot Parent Dependencies.
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.M1</version><relativePath /> <!-- lookup parent from repository --></parent>
Next Step. Swagger Dependencies.
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
Next Step. Spring Boot main class.
Class: Application.java
Note: @SpringBootApplication=@Configuration+ @EnableAutoConfiguration+ @ComponentScan.
package com.bce;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
Next Step. Create Rest Controller.
package com.bce.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloWorldRestController {@GetMappingpublic String hello() {return "Hello World";}}
Next Step. Server Port: The default server port is 8080. Wish you want to change open application.properties and add the below line.
server.port=8081
Next Step: Run and Open have given below URL on the browser.
URL: http://localhost:8080/actuator
Download Code from GitHub. Download
No comments:
Post a Comment