Manage Spring boot applications using Spring boot admin

Manage and monitor your spring boot applications using nice UI with spring boot admin. There are lots of options out there for APM (Application process monitoring) spring boot applications such as MicrometerPrometheus and Grafana.I was looking for something from spring ecosystem itself, although we have lots of others options with respect to APM (Application process monitoring) .

I wanted to monitor our spring applications; In this search, I have come across ‘Spring boot admin‘.

Spring Boot Actuator

Actuator is a Spring Boot module, which adds REST/JMX endpoints to your application, so you can easily monitor and manage it in production.It offers health-check, metrics monitoring, access to logs, thread dumps, heap dumps, environmental info and more through end points.

Spring Boot Admin

Spring Boot Admin is a web application, used for managing and monitoring Spring Boot applications. Each application is considered as a client and registers to the admin server. Behind the scenes, the magic is given by the Spring Boot Actuator endpoints.Spring boot admin is a yet another spring boot application to monitor your other spring boot applications.

The official Spring website gives some useful insights on Spring boot admin. (! some of the information are not updated, in spring generally things will be little different based on which version you use).Please check official spring framework website here for more details. 24. Monitoring and Management.

Spring Boot Admin is not a core module provided by the Spring team, it was created by a company called Codecentric. Still, the code is publicly available on Github and it is free.

Overview of Spring boot admin:

What you can monitor using spring boot admin:

Spring boot admin provides the following features for registered application:

  • Show health status
  • Show details, like
    • JVM & memory metrics
    • micrometer.io metrics
    • Datasource metrics
    • Cache metrics
  • Show build-info number
  • Follow and download logfile
  • View jvm system- & environment-properties
  • View Spring Boot Configuration Properties
  • Support for Spring Cloud’s postable /env- &/refresh-endpoint
  • Easy log level management
  • Interact with JMX-beans
  • View thread dump
  • View http-traces
  • View audit events
  • View http-endpoints
  • View scheduled tasks
  • View and delete active sessions (using spring-session)
  • View Flyway / Liquibase database migrations
  • Download heapdump
  • Notification on status change (via e-mail, Slack, Hipchat , …)
  • Event journal of status changes (non persistent)

What I am going to do?

  1. Develop our spring boot admin application
  2. Develop two spring boot client applications and register to the admin application
  3. Show what are the basic features can be seen from the admin application.

Technologies used:

Tool
version
Spring boot 2.1.0.RELEASE
Spring boot admin 2.1.0
Java 1.8
Gradle 5.2.1

1. Develop our spring boot admin application

A simple spring boot application which will have spring boot admin added as dependency. This is going to be our monitoring application which is nothing but a spring boot admin application.

I have used Spring Initializr to create spring boot applications. I have created application with the following details:

build.gradle

Simple Spring boot application ‘Spring boot admin starter server'(de.codecentric:spring-boot-admin-starter-server)  is added as a dependency.

application.properties

spring.security.user.name=admin
spring.security.user.password=admin

In application properties, I have very minimal configuration, administrator credentials for our monitoring admin application. i.e my admin boot server will run on 8080 (default port).

Now in your Client you need to add these credentials as well, otherwise it will not be able to register with the server:

WebSecurityConfig.java

If you look at the build.gradle we have added ‘org.springframework.boot:spring-boot-starter-security‘ in the dependencies.

We need is to add Spring Security configuration to protect the Admin user interface:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler
= new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl("/");

http.authorizeRequests()
.antMatchers("/assets/**").permitAll()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").successHandler(successHandler).and()
.logout().logoutUrl("/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/instances",
"/actuator/**"
);
}
}

The above configuration is to restrict the admin UI only to authenticated users using HTTP basic authentication and form login. Otherwise, you could not log in. Then there is a cookie-based Cross-Site Request Forgery protection.

SpringBootAdminApplication.java

This is the main application class of our app, we need to add ‘@EnableAdminServer’ in order to make this application as a spring boot admin server.

Running the admin server:

We are done with the spring boot admin server development and necessary configurations. Let us try to start the server.

In gradle : gradle clean bootRun Upon opening http://localhost:8080 in browser, You will be asked to login:

Now you can use user name and password you have configured in the application.properties file.

In our case, it is ‘admin/admin’.

After successful login, you will see:

Now there are no client applications to monitor. No clients are registered in admin. So instances are zero.

2. Develop and configure two spring boot client applications

We have successfully started a spring boot admin server. Now we will develop two client boot applications for demonstration purpose.The client applications will be simple spring boot applications, which will have actuators and spring boot admin client modules included as dependency.

We need to do the following in our client applications in order to be registered to the admin application.

  • Adding actuator and spring boot admin client modules as dependencies in build.gradle:
    • org.springframework.boot:spring-boot-starter-actuator
    • de.codecentric:spring-boot-admin-starter-client
  • Adding  spring boot admin credentials and admin url  to register as client in application.properties
  • adding ‘management.endpoints.web.exposure.include=*’ — To expose all the Actuator endpoints in application.properties
  • adding  ‘management.endpoint.health.show-details=always’ — Always show details in Health check section in application.properties

Client Setup

Same as with server setup, the first step is to add a proper dependency to an existing application:

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-actuator'
   implementation 'de.codecentric:spring-boot-admin-starter-client'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'org.springframework.boot:spring-boot-starter-security'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

You need to define URL where is your Admin Server running. Add this line to your application.properties:

spring.boot.admin.client.url=http://localhost:8080

However, most of the endpoints are not exposed by Actuator by default. You need to change your configuration in application.properties to expose them:

management.endpoints.web.exposure.include=*


build.gradle

If you look at the build.gradle: we have added actuator module, spring boot admin client module and security( to make our app secure).

application.properties

# servier port
server.port=8081

# Url where Boot Admin Server is running
spring.boot.admin.client.url=http://localhost:8080
# Credentials to authenticate with the Boot Admin Server
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

# To expose all the Actuator endpoints
management.endpoints.web.exposure.include=*

# Always show details in Health check section
management.endpoint.health.show-details=always

# If you don't set this, username 'user' will be used by default
# And a password will be auto-generated each time your app starts
# Such password is visible in the console during app startup
spring.security.user.name=user
spring.security.user.password=user

# To connect to the client
spring.boot.admin.client.instance.metadata.user.name=user
spring.boot.admin.client.instance.metadata.user.password=user

info.app.name=Spring Boot client application 1
info.app.description=This is a demo spring boot client application
info.app.version=1.0.0

Start Monitoring

When you open Admin Server UI, you should see your application. When you click the app name, a page with application details should show up.

After exposing your Actuator endpoints, you should see much more information in your Admin interface:

Monitoring:

Audit Logs:

Threads:

Notifications

Once you have monitoring in place, you want to get notified when something goes wrong. The good news is that Spring Admin provides a wide variety of notification options.

If you visit the Admin Server page for the first time, it asks you for permission to display push notifications on your computer. Whenever there is an issue, you’ll get a popup message.

There are some other notifications and it need a simple configuration. Just providing a few entries in your application.properties will suffice.

The following services are supported:

    • Mail
    • Slack
    • HipChat
    • PagerDuty
    • OpsGenie
    • Let’s Chat
    • Telegram
    • Microsoft Team

Email Notification

you need to add Spring email dependency to your Server part:

dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-mail'
}

Then you need to define your SMTP server, which will be used to send email notifications and credentials. Update your application.properties of your Admin Server.

spring.mail.host=smtp.foo.com
spring.mail.username=smtp-server-user
spring.mail.password=smtp-server-password

Then you need to define recipients and sender.

# Sender email address
spring.boot.admin.notify.mail.from="Spring Boot Admin <noreply@mks.com>"
# Comma-delimited list of recipient email addresses
spring.boot.admin.notify.mail.to=ram@mks.com,bob@mks.com
# Comma-delimited list of carbon copy recipient email addresses
spring.boot.admin.notify.mail.cc=joe@mks.com

Code

You can find the source code of this article in Source github.

Conclusion

Spring Boot Admin offers a nice and useful UI layer on top of Actuator Endpoints. And it allows you to centrally monitor multiple applications with multiple instances, which is very useful when working in the cloud and with microservices. Make sure though, that you sufficiently protect both your Client and Server.

References:

 

You Might Also Like
Leave a Reply