Spring Boot Admin Server and Client Example

Spring boot admin server is a community project that makes monitoring the Spring boot applications a lot easier. An admin client application can be registered with the admin server via HTTP or discovered using the spring cloud. Let’s create an example implementation of the spring boot admin server and client applications.

In this post, we will learn how to set up the Spring boot admin server and also register a client application into it.

We will set up the Spring boot admin server with basic security(with a username and password to access the admin console) and then create a client application and register it into the admin server.

Following are the version details:

  • Java version 1.8
  • Spring boot version 2.2.4.RELEASE
  • Spring boot admin version 2.2.1

Table of Contents

Create the Spring Boot Admin Server example app

Create a new Spring boot application with required dependencies. Add spring-boot-starter-web, spring-boot-starter-security and spring-boot-admin-starter-server dependencies.

The spring-boot-admin-starter-server dependency configures this application as a Spring boot admin server.

<?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.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.asbnotebook</groupId>
    <artifactId>spring-boot-admin-server-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-admin-server-example</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.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-security</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

To enable the admin server capabilities for the created application, we need to annotate the application with the @EnableAdminServer annotation.

package com.asbnotebook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.server.config.EnableAdminServer;

@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminServerExampleApplication {

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

Set up the Admin server

We have changed the admin server application’s port to 9090, and we have also set a default username and password.

server.port=9090
spring.security.user.name=admin
spring.security.user.password=admin

Create a security configuration class and add the following spring security configuration details.

package com.asbnotebook;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl("/");
        http.authorizeRequests(authorizeRequests -> authorizeRequests.antMatchers("/assets/**").permitAll()
                .antMatchers("/login").permitAll().anyRequest().authenticated())
                .formLogin().loginPage("/login").and()
                .logout().logoutUrl("/logout").and()
                .httpBasic(Customizer.withDefaults())
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers("/instances", "/actuator/**");
    }
}

This class configures a basic Spring security configuration for the Spring boot admin server.

Admin Client application

Create another spring boot application with the required dependencies. This application will be our admin client application, that will be registered with the admin server.

We have added spring-boot-starter-web, spring-boot-starter-actuator and spring-boot-admin-starter-client dependencies.

<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.asbnotebook</groupId>
    <artifactId>spring-boot-admin-client-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-admin-client-example</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
    <dependencies>
        <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>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

Set up the client application

To register the client application with the admin server, we have to add the following configurations.

spring.boot.admin.client.url=http://localhost:9090
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
management.endpoints.web.exposure.include=*
spring.jmx.enabled=true

We have used following configuration properties:

  • spring.boot.admin.client.url: The Admin server URL.
  • spring.boot.admin.client.username: User name of the admin server.
  • spring.boot.admin.client.password: Password of the admin server.
  • management.endpoints.web.exposure.include: To expose all of the actuator endpoints.
  • spring.jmx.enabled: Expose the JMX management beans.

Create a simple GET endpoint by creating a RestController class, as shown below. We will use this to demonstrate how the metrics can be monitored using the Spring boot admin server.

We have created a GET endpoint called /test-admin in the following example.

package com.asbnotebook.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/test-admin")
    public String testMethod() {
        return "test";
    }
}

Testing the application

Run both of the applications(server and the client). We can access the Spring boot admin server console at http://localhost:9090.

Since we have enabled Spring security configuration, we will get a default admin server login page.

Spring boot admin server

After logging in to the admin server console, we can observe the registered client applications and instances.

Notice the registered application’s status on the admin console panel.

Spring boot admin example

The Wallboard section displays the different options available related to the application instances.

We can also monitor the threads, metrics, logging configurations, etc.

admin 2.0 console

Access the client application’s /test-admin endpoint.

Spring boot admin 2.0 example

The metrics section displays the registered endpoint details.

admin server -client

Conclusion

In this post, we learned the basics of the Spring boot admin server.
Also, we created the Spring boot admin server, enabled security using the spring security.

We also created an admin client and registered the application with the admin server.

Example source code is available on GitHub.

Spring Boot Admin Server and Client Example
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading