22 Run Management How to Use Admin Server to Manage Spring Applications

22 Run Management - How to Use Admin Server to Manage Spring Applications #

In the previous two lessons, we added system monitoring functionality to our Spring Boot application by introducing the Actuator component. With the various HTTP endpoints exposed by Actuator, developers can obtain the runtime status of the system. However, these endpoints are a low-level monitoring technique, which requires a certain level of understanding of the HTTP protocol and the construction of Spring Boot applications.

So, is there a simpler, visual way to obtain the information behind these endpoints? The answer is yes. Therefore, in this lesson, we will introduce the Spring Boot Admin component.

Integrating the Spring Boot Admin Component #

Spring Boot Admin is an application for monitoring Spring Boot applications. Its basic principle is to provide a concise visual web UI by leveraging the various HTTP endpoints provided by Spring Boot Actuator. The following diagram illustrates the basic architecture of Spring Boot Admin:

Diagram 1.png

Basic architecture of Spring Boot Admin

From the diagram above, we can see that the overall architecture of Spring Boot Admin consists of two components: the server-side component called Admin Server and the client-side component called Admin Client. The Admin Client is actually a regular Spring Boot application, while the Admin Server is an independent service that needs to be built separately.

Next, let’s first introduce two ways to build the Admin Server: one is a simple approach that uses a standalone Admin service, and the other is a relatively complex approach that relies on the service registration and discovery mechanism of the service registry.

Building an Admin Server with a Standalone Service #

Regardless of the approach used to implement the Admin Server, we need to create a Spring Boot application first and add the following dependencies to the pom file:


<dependency>

  <groupId>de.codecentric</groupId>

  <artifactId>spring-boot-admin-server</artifactId>

</dependency>



<dependency>

  <groupId>de.codecentric</groupId>

  <artifactId>spring-boot-admin-server-ui</artifactId>

</dependency>

Please note that Spring Boot Admin is not an official component provided by the Spring family, but it is from a team called codecentric AG.

To convert a regular Spring Boot application into a Spring Boot Admin Server, we just need to add the @EnableAdminServer annotation to the Bootstrap class. Here is an example code snippet of a Bootstrap class with the @EnableAdminServer annotation:


@SpringBootApplication

@EnableAdminServer

public class AdminApplication {



    public static void main(String[] args) {

        SpringApplication.run(AdminApplication.class, args);

    }

}

With this approach, we can easily build a Spring Boot Admin Server.

Next, we start this Spring Boot application and open the web interface, where we can see the following:

Screenshot 1.png

Startup effect of Spring Boot Admin Server

From the screenshot above, we can see that there is currently no application associated with the Admin Server. If we want to associate the application with the Admin Server, we need to make some modifications to the existing Spring Boot application.

First, we add the Maven dependency for the Spring Boot Admin Client component to the Maven dependencies, as shown in the following code snippet:


<dependency>

  <groupId>de.codecentric</groupId>

  <artifactId>spring-boot-admin-starter-client</artifactId>

</dependency>

Then, in the configuration file, we add the following configuration information so that the application can be associated with the Admin Server:


spring:

  boot:

    admin:

      client:

        url: http://localhost:9000

Note: Here, 9000 is the server port of the Admin Server.

Now, when we start this application, we can see the name and address of this application appear in the Admin Server, as shown in the following screenshot:

Screenshot 2.png

Effect of adding an application to the Spring Boot Admin Server

In the screenshot, we can see that the number of APPLICATIONS and INSTANCES is both 1, which means that the Admin Server is managing one application, and this application has only one running instance. Below the interface, we can also see the name and instance address of this application. Here, you can try starting different instances of the application on different ports and observe changes in this list.

Building an Admin Server with a Service Registry #

Although building an Admin Server and Admin Client with a standalone service is very simple, it requires adding Maven dependencies for Spring Boot Admin to each application and specifying the Admin Server address. This actually results in code invasion, meaning that there is a strong coupling between the application and the Admin Server.

So, is there a better way to separate or transfer this coupling?

Considering that there needs to be a relationship similar to service registration between the Admin Server and Admin Clients, we can consider this as a manifestation of the service registration and discovery mechanism.

In the Spring family, there is a Spring Cloud framework for building microservice architectures, and within that framework, there happens to be a component specifically designed to implement service registration and discovery called the service registry Spring Cloud Netflix Eureka. Spring Boot Admin natively integrates with this registry tool.

With the service registry, the interaction between the Admin Server and the Admin Clients is as shown in the following diagram:

Diagram 2.png

Interaction between the Eureka-based Admin Server and Admin Client

Building a registry using Eureka is also simple. First, we create a standalone Spring Boot application and add the following Maven dependency for providing Eureka server functionality in the pom file:


<dependency>

  <groupId>org.springframework.cloud</groupId>

  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

</dependency>

After adding the Maven dependency, we can create the Spring Boot startup class. In the example code, we name this startup class EurekaServerApplication, as shown below:


@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

    public static void main(String[] args) {

     

        SpringApplication.run(EurekaServerApplication.class, args);

    }

}

Note: In the code snippet above, we added the @EnableEurekaServer annotation on the startup class. In Spring Cloud, a service with the @EnableEurekaServer annotation is a Eureka server component. This way, the Eureka service is built. Similarly, the Eureka service also provides us with a visual UI interface that can be used to observe the application information currently registered in Eureka, as shown in the following figure:

Drawing 4.png

Eureka Service Monitoring Page

Next, we need to make corresponding adjustments to the Admin Server. First, we add a dependency on the spring-cloud-starter-netflix-eureka-client Eureka client component to the pom file:

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

At this point, the Admin Server acts as a client of Eureka, so we need to add the @EnableEurekaClient annotation to its BootStrap class to register the Admin Server to Eureka.

The final step in refactoring the Admin Server is to adjust the configuration. At this point, we need to add the following configuration items to the configuration file to specify the Eureka server address.

eureka:

  client:

    registerWithEureka: true

    fetchRegistry: true

    serviceUrl:

      defaultZone: http://localhost:8761/eureka/

Okay, now the Admin Server has been refactored, next let’s take a look at the Admin Client.

The purpose of introducing a registry is to reduce the coupling between the Admin Client and the Admin Server. We can confirm this from the Maven dependencies. With the registry, the Admin Client no longer depends on the spring-boot-admin-starter-client component, but directly uses the following Eureka client component.

<dependency>

     <groupId>org.springframework.cloud</groupId>

     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

In the configuration file, we need to remove the reference to the Admin Server address and directly use the Eureka server address. No modifications to the Bootstrap class of the Admin Client are necessary.

With the above adjustments, each Admin Client can be associated with the Admin Server through the Eureka registry.

Using the Admin Server to monitor the system #

According to the introduction on Spring Boot Admin’s official GitHub page, the Admin Server monitoring system provides a complete visualization solution. Based on the Admin Server, core functions such as health status, JVM, memory, Micrometer metrics, threads, and HTTP tracing can all be displayed through the visual UI interface.

Key metrics during the runtime of the monitoring system #

Notice that in the Admin Server menu, there is a “Wallboard”. Clicking on this menu, we can see an application wall, as shown in the following figure:

Drawing 5.png

Admin Server Application Wall

Clicking on an application in the wall, we can enter the main monitoring page for that application. On the left side of this page, there are various levels of directories for monitoring functions, as shown in the following figure:

图片6.png

Admin Server Monitoring Main Page

In the figure, we see the most important “Health” information. Obviously, this information comes from the Health endpoint of the Spring Boot Actuator component. You can refer to the content of “Service Monitoring: How to Use the Actuator Component for System Monitoring?” for a review.

Continuing to scroll down on this page, we will see some JVM-related monitoring information, such as useful data on threads, garbage collection, and memory status, as shown in the following figure:

图片7.png JVM Monitoring Information in Admin Server

These JVM data are presented in a visual way and are updated in real time as the runtime state changes.

In Lecture 21, we discussed in detail the metrics in Spring Boot Actuator. Similarly, in the Admin Server, there is also a “Metrics” menu, which is shown in the following screenshot:

Drawing 8.png

Metrics Information in Admin Server

In the “Metrics” menu, developers can filter various conditions and add corresponding metrics. For example, in the above screenshot, we filtered the /actuator/health endpoint in HTTP requests to obtain metrics results.

Next, let’s take a look at the system environment attributes. Because there are many attributes in this area, Admin Server provides a filter as shown in the following screenshot:

Drawing 9.png

Environment Information in Admin Server

In the above screenshot, by entering the “spring.” parameter, we can obtain a series of environment properties related to that parameter.

Logs are also an important way to monitor the system. In the “Loggers” menu of Admin Server, you can see all the log information of the application, as shown in the following screenshot:

图片10.png

Loggers Information in Admin Server

By filtering these logs with the “springcss” keyword, we can obtain detailed logs for the SpringCSS example. The screenshot also shows the log level corresponding to each logger.

Finally, let’s take a look at the “JVM” menu in Admin Server, which has two sub-menus: “Thread Dump” and “Heap Dump”.

Taking “Thread Dump” as an example, although Actuator provides the /threaddump endpoint, developers can only obtain the dump information when triggering that endpoint. However, Admin Server provides a continuous visual monitoring interface, as shown in the following screenshot:

Drawing 11.png

Thread Dump Information in Admin Server

By clicking on the color bar in the screenshot, we can obtain detailed information of each thread. Here, you can try to perform some analysis.

Controlling Access Security #

At this point, we will find that the functionality of Admin Server is very powerful and should not be exposed to all developers. Therefore, we need to control the access security of Admin Server.

To achieve this, it is very simple. We just need to integrate Spring Security.

Combining the content of “User Authentication: How to Build a User Authentication System Based on Spring Security?”, we add a Maven dependency for spring-boot-starter-security to the Spring Boot application:                         org.springframework.boot                 spring-boot-starter-security                

Then, we add the following configuration in the configuration file:         spring:           security:             user:               name: “springcss_admin”               password: “springcss_password”    

After restarting Admin Server, when accessing the web interface again, we need to enter the username and password, as shown in the following screenshot:

Drawing 12.png

Security Login Interface of Admin Server

Summary and Preview #

Visual monitoring has always been the basic requirement for developers and operation personnel to manage the runtime state of applications. Spring Boot Admin is exactly such a visual tool. It is based on the monitoring information exposed by various endpoints in Spring Boot Actuator, which are integrated and combined. Today’s content first introduced the methods of building Admin Server and Admin Client, and analyzed the whole set of visual solutions available in Admin Server.