Spring Boot Admin UI for building production-grade applications

0
10883

Using Spring Boot makes it easy for developers to create standalone, production-grade Spring-based applications that can be ‘just run’. Spring Boot is a part of microservices development.

As part of developing microservices, many of us use the features of Spring Boot along with Spring Cloud. In the microservices world, we may have many Spring Boot applications running on the same or different hosts. If we add SpringActuator (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready) to the Spring Boot applications, we get a lot of out-of-the-box end points to monitor and interact with applications. The list is given in Table 1.

The end points given in Table 1 provide a lot of insights about the Spring Boot application. But if you have many applications running, then monitoring each application by hitting the end points and inspecting the JSON response is a tedious process. To avoid this hassle, the Code Centric team came up with the Spring Boot Admin (https://github.com/codecentric/spring-boot-admin) module, which provides us an Admin UI dashboard to administer Spring Boot applications. This module crunches the data from Actuator end points, and provides insights about all the registered applications in a single dashboard.

We will demonstrate the Spring Boot admin features in the following sections.

Figure 1: Spring Boot logo

As a first step, create a Spring Boot application that will be a Spring Boot Admin Server module by adding the Maven dependencies given below:

<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>

&lt;/dependency&gt;<a href="https://www.opensourceforu.com/wp-content/uploads/2017/08/Table-1-2.jpg" target="_blank" rel="noopener"><img class="aligncenter wp-image-31539 size-medium" src="https://www.opensourceforu.com/wp-content/uploads/2017/08/Table-1-2-350x203.jpg" alt="" width="350" height="203" /></a>Add the Spring Boot Admin Server configuration by adding<em> @EnableAdminServer</em> to your configuration, as follows:
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[]

}
Figure 2: Admin server UI

Let us create more Spring Boot applications to monitor through the Spring Boot Admin Server created in the above steps. All Spring Boot applications that we now create will act as Spring Boot Admin clients. To make the application an admin client, add the dependency given below 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 property given below to the application.properties file. This property tells us 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 will be 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. Figure 2 shows the Admin Server UI.

A detailed view of the application is given in Figure 3. In this view, we can see the tail end of the log file, the metrics, environment variables, the log configuration where we can dynamically switch the log levels at the component level, the root level or package level, and other information.

Let’s now look at another feature called notifications from the Spring Boot admin. This notifies the administrators when the application status is DOWN or when the application status is coming UP. Spring Boot admin supports the following channels to notify the user.

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

In this article, we will configure Slack notifications. Add the properties given below 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.name}* is *#{to.status}*” //Message to appear in the channel

Figure 3: Detailed view of Spring Boot Admin

Since we are managing all the applications with the Spring Boot Admin, we need to secure its UI with a login feature. Let us enable the login feature to the Spring Boot Admin Server. I am going with basic authentication here. Add the Maven dependencies give below to the Admin Server module, as follows:

<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 properties given below to the application.properties file.

security.user.name=admin //user name to authenticate

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

As we have added security to the Admin Server, admin clients should be able to connect to the server by authenticating. Hence, add the properties given below 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, which we can enable in the dashboard. The sample code created for this demonstration is available in a GitHub repository.

LEAVE A REPLY

Please enter your comment!
Please enter your name here