Monitoring Microservices with Spring Boot Admin
Tags: java, springboot, microservices, monitoring
Published: 2022-07-10 09:31:39

The microservices enable the rapid, frequent and reliable delivery of large, complex applications. Monitoring, managing or troubleshooting poor performing microservice can be tedious task sometime, especially if you have to go to multiple places to look at what is happening under the hood. Be it looking for behavior of an application through log file, threads, heap memory etc. And you would certainly like all these things at one place for quick monitoring of your spring boot applications.

Although **[Spring Boot Actuator](https://springframework.guru/actuator-in-spring-boot/#:~:text=Spring%20Boot%20Actuator%20is%20a,metrics%20from%20production%2Dready%20applications.)** can do the magic for you if you have single app to look after. When you have multiple applications to monitor, **Spring Boot Admin** is a great choice!


Spring Boot Admin Server is an application used to manage and monitor your Microservice applications. CodeCentric team had developed Spring Boot Admin UI to manage your Spring Boot application actuator endpoints.

Each microservice acts as a client and has to register to Spring boot admin server. Behind the curtain, admin server leverages application's actuator endpoints to gather all the data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1r3l9cr1xoqhfc211hmj.jpg)

Lets look at what is needed to build a Spring Boot Admin server.

- Go to https://start.spring.io/ and Generate a starter project with mentioned dependencies.

**_Maven_**

```
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
		</dependency>
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3nqvb54bx791nfehpbc.png)
 

- Add the **@EnableAdminServer** annotation to your main application class. The **@EnableAdminServer** annotation will turn your spring boot application to Admin Server and monitor all other microservices.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7p54qy9ac8kurejxb9m.png)

- Define application name and port in application.properties

```
spring.application.name=adminserver
server.port=9090
```

And that's all about it! Your Spring Boot Admin server is ready to run and serve the clients connecting to it. 

Tomcat server will be started on the port 9090 as specified. You can launch Spring Boot Admin UI by browsing **http://localhost:9090** 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7ezzavt13xfhll8mjmo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5qz8mkic0ahjumyh7jt.png)
 

---
Now that our Admin server is all setup, lets write couple of client applications to place on monitoring.

You will need to add below dependencies to pom.xml file.

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

I have created two REST API applications for demo. We need to setup these as a client to connect to admin server. 

We need to add few properties to application.properties file as given below.

_**Note:** From Spring Boot 2 and above, actuator endpoints except health and info are restricted. That why we are exposing all the endpoints by specifying the properties explicitly._

```
# Application
spring.application.name=Demo API
server.port=8080

# Spring boot admin server 
spring.boot.admin.client.url=http://localhost:9090

# Actuator endpoints properties
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# To monitor log file on admin server
logging.file.name=demo_api.log
logging.logback.rollingpolicy.max-history=5
logging.logback.rollingpolicy.max-file-size=10MB
```

Once client applications are started, they will be registered to admin server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/env1m7rk8ev5uabltdd5.png)

You can now see the applications being monitored on Spring Boot Admin UI Wallboard with their overall status.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3hsr84m1c2ey57ilm5e4.png)

Applications tab will show you the details on number of applications up/down, overall status, lets you filter the apps etc.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxfr15cipwkvx6roun28.png)

Journal tab will give you the audit details on startup/shutdown of a particular application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fwv2evvcbtcizl5pfrv.png)

Lets look at what other granular details are being provided for each of these applications.

- You can look at overall health of an application instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cthx7tiu2qm3lw7xe476.png)
 
- Add metrics as per your requirement.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19cdru16e9sgd1uhgb04.png)
 
- Details on system environment
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/602y2js8nxbkbs0u4c7f.png)

- Application beans

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njvpq1iukoaadi0h2u3o.png)

- Configuration details and scheduled tasks if any

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9amof7gkafyc93n9ip7.png)

- You can monitor and download logs of an application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77vnkpbsu6qorhoykoxw.png)

- You can set the logger level as well

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x77dfdfjm4ovg7lxbc6v.png)

- JVM related details on Thread and Heap dump.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11ulyiq6r78ot30c57k3.png)

- URI mappings of an application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9udinrb0mn8eyfc1osc.png)
       
- Cache details of an application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgebwd5gf9yvtpaowg9z.png)

As you can see, we have all the details about an application at one place now. 

Lets look at few other modules provided by Spring Boot Admin.

- We had exposed all the actuator endpoints to admin server which in turn will expose all the sensitive information about an application as well. It becomes pivotal to secure admin UI and have only authorized personnel look at it. 

You can refer https://codecentric.github.io/spring-boot-admin/current/#securing-spring-boot-admin to secure admin UI.

- Notification about a particular event in an application is also of great importance to avoid eye on the glass monitoring.
Spring boot admin notifications module provides lot of options to notify user via Email, MS teams, Slack etc.

Checkout https://codecentric.github.io/spring-boot-admin/current/#_notifications for more details.

I hope you enjoyed reading about Spring Boot Admin in this article. Leave your reactions and comments below if you found this useful. Keep Learning!