Add a SpringBootAdmin monitoring

0. Preface

In the previous chapters, we first built a project framework, and then built a gateway project using nacos. There are not too many things configured in the gateway project. Now we will build another important project in Spring Cloud microservices spring boot admin

1. Introduction to spring boot admin

Spring Boot Admin is used to monitor applications based on Spring Boot. It provides a simple and Visual Web UI based on Spring Boot activator. Spring Boot Admin provides the following functions:

  • Show health status of app
  • Display application details: 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 the Spring Boot project configuration
  • Display thread dump
  • Show HTTP traces

......

And so on.

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 monitor directory pom.xml File, add the following:

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

In manager/pom.xml Register our new project module:

<modules>
    <module>gateway</module>
    <module>monitor</module>
</modules>

Create the following directory in monitor:

.
├── pom.xml
└── src
    └── main
        ├── java
        └── resources

At the root pom.xml To add a Spring Boot Admin dependency:

First add the spring boot admin version number variable:

<spring-boot-admin.version>2.2.3</spring-boot-admin.version>

And under dependency Management > dependencies, add:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>${spring-boot-admin.version}</version>
</dependency>

In monitor/pom.xml Add to 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>

function

mvn clean install

Check and brush 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 interworking with gateway service

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 build a connection between the two. In other words, introduce the gateway project into Spring Admin Boot listening.

In the manager/gateway pom.xml The following references are included in the document:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Then modify the startup port of the gateway project, in resources/bootstrap.yml add to:

server:
  port: 8070

Add nacos reference to 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 :

spring:
  application:
    name: monitor
  
  cloud:
  	nacos:
      discovery:
        server-addr: 127.0.0.1:8848

There is an error about the configuration here in the previous article. It should be discovery > server addr, not config > server addr. There is a difference between the two. Discovery indicates that nacos is set as the service discovery center, and config indicates that nacos is set as the configuration center.

Start the gateway project and the monitor project to see the effect. Visit port 8080:

It can be seen that two applications can be found. If nacos is not set as the service discovery center in the monitor project, the specific online applications will not be obtained. Click gateway to view:

4. Summary

We have built a Spring Boot Admin project as a monitoring system, and will add more content here later.

Please pay attention to more My blog Mr. Gao's Cabin

Tags: C# Spring xml Maven Apache

Posted on Wed, 10 Jun 2020 21:43:28 -0400 by ptolomea