Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Look at Spring Boot Admin

DZone's Guide to

A Look at Spring Boot Admin

Particularly useful for microservices, Spring Boot Admin allows you to administer and monitor multiple Spring Boot applications using a single dashboard.

Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

As part of microservices development, many of us are using Spring Boot along with Spring Cloud features. In the microservices world, we will have many Spring Boot applications running on both the same and different hosts. If we add Spring Actuator to the Spring Boot applications, we will get many out-of-the-box endpoints to monitor and interact with Spring Boot applications. The list is given below.

ID Description Sensitive Default
actuator Provides a hypermedia-based “discovery page” for the other endpoints. Requires Spring HATEOAS to be on the classpath. true
auditevents Exposes audit events information for the current application. true
autoconfig Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied. true
beans Displays a complete list of all the Spring beans in your application. true
configprops Displays a collated list of all @ConfigurationProperties. true
dump Performs a thread dump. true
env Exposes properties from Spring’s ConfigurableEnvironment. true
flyway Shows any Flyway database migrations that have been applied. true
health Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated). false
info Displays arbitrary application info. false
loggers Shows and modifies the configuration of loggers in the application. true
liquibase Shows any Liquibase database migrations that have been applied. true
metrics Shows ‘metrics’ information for the current application. true
mappings Displays a collated list of all @RequestMapping paths. true
shutdown Allows the application to be gracefully shutdown (not enabled by default). true
trace Displays trace information (by default the last 100 HTTP requests). true


The above endpoints provide a lot of insights about a Spring Boot application. But if you have many applications running, then monitoring each application by hitting the endpoints and inspecting the JSON response is a tedious process. To avoid this hassle, the Code Centric team came up with the Spring Boot Admin module, which will provide us an admin UI dashboard to administer Spring Boot applications. This module crunches the data from Actuator endpoints and provides insights about all the registered applications in a single dashboard.

As a first step, create a Spring Boot application, which we will make as a Spring Boot Admin server module by adding the below maven dependencies.

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>1.5.1</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>1.5.1</version>
</dependency>


Add the Spring Boot Admin Server configuration by adding @EnableAdminServer to your configuration.

package org.samrttechie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import de.codecentric.boot.admin.config.EnableAdminServer;

@EnableAdminServer
@Configuration
@SpringBootApplication
public class SpringBootAdminApplication {

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

    @Configuration
    public static class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // Page with login form is served as /login.html and does a POST on /login
            http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll
                // The UI does a POST on /logout on logout
            http.logout().logoutUrl("/logout");
            // The ui currently doesn't support csrf
            http.csrf().disable();

            // Requests for the login page and the static assets are allowed
            http.authorizeRequests()
                .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
                .permitAll();
            // ... and any other request needs to be authorized
            http.authorizeRequests().antMatchers("/**").authenticated();

            // Enable so that the clients can authenticate via HTTP basic for registering
            http.httpBasic();
        }
    }
    // end::configuration-spring-security[]

}


Let us create more Spring Boot applications to monitor via the Spring Boot Admin server created in the above steps. All the Spring Boot applications created now will act as Spring Boot Admin clients. To make an application as an Admin client, add the following dependency along with the Actuator dependency. In this demo, I have created three applications — Eureka Server, Customer Service, and Order Service.

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>1.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


Add the following property to the application.properties file. This property contains where the Spring Boot Admin server is running. Hence the clients will register with the server.

spring.boot.admin.url=http://localhost:1111


Now, if we start the Admin Server and other Spring Boot applications, we can able to see all the admin clients information in the Admin server dashboard. As we started our admin server on port 1111 in this example, we can see the dashboard at http ://<host_name>:1111. Below is the screenshot of the Admin Server UI.

A more detailed view of an application is given below. In this view, we can see the tail of the log file, metrics, environment variables, and log configuration, and we can dynamically switch the log levels at the component level, root level, or package level.

Now we will see another feature called notifications from Spring Boot Admin. This will notify the administrators when the application status is DOWN or the application status is coming UP. Spring Boot Admin supports the following channels to notify the user.

  • Email
  • Pagerduty
  • Hipchat
  • Slack
  • Let’s Chat

In this article, we will configure Slack notifications. Add the following properties to the Spring Boot Admin Server’s application.properties file.

spring.boot.admin.notify.slack.webhook-url=https://hooks.slack.com/services/T8787879tttr/B5UM0989988L/0000990999VD1hVt7Go1eL //Slack Webhook URL of a channel
spring.boot.admin.notify.slack.message="*#{application.names *#{to.status}*" //Message to appear in the channel


With Spring Boot Admin, we are managing all the applications, so we need to secure the Spring Boot Admin UI with a login feature. Let's enable the login feature for the Spring Boot Admin server. Here, I am going with basic authentication. Add the following maven dependencies to the Admin Server module.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui-login</artifactId>
    <version>1.5.1</version>
</dependency>


Add the follow properties to the application.properties file.

security.user.name=admin //user name to authenticate
security.user.password=admin123 //Password to authenticate


As we added security to the Admin Server, Admin clients should be able to connect to the server by authenticating. So, add the following properties to the Admin client’s application.properties files.

spring.boot.admin.username=admin
spring.boot.admin.password=admin123


There are additional UI features like Hystrix and Turbine UI that we can enable for the dashboard. You can find more details here. The sample code created for this demonstration is available on GitHub.

Spring Boot Admin Server Demonstration

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
spring boot ,spring actuator ,spring boot admin ,java ,tutorial

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}