Spring Boot Admin Server Example (2024) | CodeUsingJava




















Spring Boot Admin Server Example (2024)

This will be the standard directory layout for maven project structure-
Spring Boot Maven Project
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
	<?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.8</version>
			<relativePath/> <!-- lookup parent from repository -->
		</parent>
		<groupId>com.codeusingjava</groupId>
		<artifactId>AdminServer</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<name>AdminServer</name>
		<description>Demo project for Spring Boot</description>
		<properties>
			<java.version>1.8</java.version>
			<spring-boot-admin.version>2.3.1</spring-boot-admin.version> 
		</properties>
		<dependencies>
			<dependency>
				<groupId>de.codecentric</groupId>
				<artifactId>spring-boot-admin-starter-server</artifactId>
			</dependency>  
	
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>
		</dependencies>
		<dependencyManagement>
			<dependencies>
				<dependency>
					<groupId>de.codecentric</groupId>
					<artifactId>spring-boot-admin-dependencies</artifactId>
					<version>0</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement> 
	
		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
	</project>
	
	
Under src/main/resources, we define the port number for the application
	server.port=8888

The main class for the AdminServerApplication is as follows:
The @EnableAdminServer annotation specifies that the class is an admin server.
	package com.codeusingjava;
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	
	import de.codecentric.boot.admin.server.config.EnableAdminServer;
	
	@SpringBootApplication 
	@EnableAdminServer
	public class AdminServerApplication {
	
		public static void main(String[] args) {
			SpringApplication.run(AdminServerApplication.class, args);
		}
	
	}
	
	
When we run the application, the output will be as follows-
We can see that the application has started successfully and no instances are registered here.
Spring Boot output

Spring boot Admin (Client)

The project structure for the Spring boot Client will be as follows-
Here we use the same application i.e Actuator with Spring boot which we have discussed above and modify the project in the following manner-
Spring Boot Maven Project
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
	<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>
	
		<groupId>com.codeusingjava.actuator</groupId>
		<artifactId>ActuatorSpringBoot</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<packaging>jar</packaging>
	
		<name>ActuatorSpringBoot</name>
		<description>Demo project for Spring Boot Actuator</description>
	
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>2.4.8</version>
			<relativePath /> <!-- lookup parent from repository -->
		</parent>
	
		<properties>
			<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
			<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
			<java.version>1.8</java.version>
			<spring-boot-admin.version>2.3.1</spring-boot-admin.version>  
		</properties>
	
		<dependencies>
	
			<dependency>
				<groupId>de.codecentric</groupId>
				<artifactId>spring-boot-admin-starter-client</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-web</artifactId>
			</dependency>
			
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>
		</dependencies>  
		<dependencyManagement>
			<dependencies>
				<dependency>
					<groupId>de.codecentric</groupId>
					<artifactId>spring-boot-admin-dependencies</artifactId>
					<version>0</version>
					<type>pom</type>
					<scope>import</scope>
				</dependency>
			</dependencies>
		</dependencyManagement>   
	
		<build>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
				</plugin>
			</plugins>
		</build>
	
	
	</project>
	
Under src/main/resources, we define the following properties-
We register the Spring Admin Client to the url of the Spring Admin Server. Several other properties regarding the actuator are also defined here.
server.port=8080
management.security.enabled=false

spring.profiles.active=local
spring.boot.admin.client.url=http://localhost:8888
spring.application.name= My Microservice


management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS

The main class for the Spring Admin Client will be as follows-
 
	package com.codeusingjava.actuator;

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

@SpringBootApplication
public class ActuatorSpringBoot {

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

Now when we run the Client application as Spring Boot Application, the output is as follows-
When we refresh the above url, http://localhost:8888/wallboard, we can see that the client instance is registered here.
Spring Admin Server instance 1

Spring Admin Server instance 2
The instance of the service is registered here-
We can check other details by clicking on the highlighted instance here-
Spring Admin Server instance 3
Now we can check all the other endpoints by clicking on the particular instance.
Spring Admin Server instance 4

Download the code-

Spring Boot Admin Actuator Example
Spring Boot Admin Server Example