0. Preface
In the previous chapters, we first built a project skeleton, and then built a gateway project using nacos. There were not too many things configured in the gateway project. Now we proceed to build another important project in Spring Cloud microservices-Spring boot admin.
1. Introduction to Spring Boot Admin
Spring Boot Admin is used to monitor Spring Boot-based applications, and provides a concise visual Web UI based on the Spring Boot Actuator. Spring Boot Admin provides the following functions:
Display the health status of the application
Display the details of the application: JVM and memory information, micrometer information, data source information, cache information, etc.
Show compiled version
View and download logs
View jvm parameters and environment variable values
View Spring Boot project configuration
Show thread dump
Show http-traces
……
Wait for a series of content.
2. Create a Spring Boot Admin project
So, let's create a Spring Boot Admin project.
2.1 Create Spring Boot Admin server
In the manager directory, create a monitor directory, and create a pom.xml file in the monitor directory, add the following content:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>monitor</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>
<parent>
<artifactId>manager</artifactId>
<groupId>club.attachie</groupId>
<version>${revision}</version>
</parent>
</project>
Register our new project module in manager/pom.xml:
<modules>
<module>gateway</module>
<module>monitor</module>
</modules>
Create the following directory in the monitor:
.
├── pom.xml
└── src
└── main
├── java
└── resources
Add Spring Boot Admin dependency to pom.xml in the root directory:
First add the spring-boot-admin version number variable:
<spring-boot-admin.version>2.2.3</spring-boot-admin.version>
And add under dependencyManagement> dependencies:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
Add in the monitor/pom.xml file:
<dependencies>
<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>
</dependencies>
run
mvn clean install
Check and refresh the mvn reference cache.
Create the MonitorApplication class:
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
After startup, you can see the following interface:
3 Intercommunication with gateway services
In the previous article, we added the Spring Cloud Gateway project. So far, the two projects are completely separated and unrelated. In this section, we establish a connection between the two. In other words, introduce the gateway project into Spring Admin Boot to monitor.
Add the following reference in the pom.xml file of manager/gateway:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Then modify the startup port of the gateway project and add in resources/bootstrap.yml:
server:
port: 8070
Add nacos reference in monitor:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Modify MonitorApplication to:
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableAdminServer
@RefreshScope
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
Create the bootsrap.yml of the monitor project:
spring:
application:
name: monitor
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
Regarding the configuration here, there was an error in the previous article. It should be discovery> server-addr, not config> server-addr. There is a difference between the two. Discovery means setting nacos as the service discovery center, and config means nacos as the configuration center.
Start the gateway project and monitor project to check the effect, and visit port 8080:
It can be seen that the two applications can be discovered. If the monitor project is not set to use nacos as the service discovery center, specific online applications will not be available. After you click on the gateway to enter, you can see:
4. Summary
We have built a Spring Boot Admin project as a monitoring system, and we will add more content here.