13、 Spring boot elegant integration of spring boot admin program monitoring

Time:2020-4-3

Preface

We know that the project monitoring is particularly important, but if we use the jconsole and JVM of JDK, it will be very tedious, and the interface is not very friendly. We used the spring boot project before, but we didn’t have a good monitoring of the project. In the spring family, spring boot admin can help us monitor the microservice project.

Spring boot admin is a monitoring tool for UI beautification encapsulation of spring boot’s activator interface. It can browse the basic information, detailed health information, memory information and JVM of all monitored spring boot projects in the list Information, garbage collection information, various configuration information (such as data source, cache list and hit rate), etc., can also directly modify the level of the logger.

Spring boot admin is divided into server and client. The server is a separate microservice, which is used to check the operation of the monitored projects. The client is our microservice projects one by one. So if we want our project to be monitored by the server, we need to register our service to the server.

OK, let’s try it.

admin-server

First, let’s build the server of spring boot admin, which says that the server is a separate project. So we create a new springboot project. After creation, let’s make some changes.

pom.xml

In the POM file, we introduce the following dependencies

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.quellanan</groupId>
    <artifactId>springbootadmin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootadmin</name>
    <description>springbootadmin project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
    </properties>

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

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

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Above is my whole POM file. You can see that I have introduced web, admin starter server and security.
If you think about other things, you can only reference admin starter server to achieve the effect.

Startup class

Add @ enableadminserver annotation to our startup class. If not, the project can start normally, but nothing can be seen. @The role of the enableadminserver annotation is to start monitoring.

@SpringBootApplication
@EnableAdminServer
public class SpringbootadminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootadminApplication.class, args);
    }
}

Configure security

After this configuration, you can start the project, but we won’t start it here, because we learned in the previous section, spring boot security. Here we use it.
We have introduced security before. Next, we will add configuration in application

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

Indicates that this user can access it. In addition, we create a securityconfig class to inherit the websecurityconfigureradapter and override the configure (httpsecurity HTTP) method. The code is as follows:

@Configuration
public class SecurityConfig 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/**"
                );
    }
}

Now let’s start the project. Enter after starting project

http://localhost:8080

Will jump to the login interface, enter the home page is nothing now.
13、 Spring boot elegant integration of spring boot admin program monitoring

admin-client

Now we can configure the server. Now let’s configure the client. We can either find a spring boot project or create a new one ourselves.

pom.xml

First, we add admin client dependency to POM file. Note that the version here needs to be the same as that of server.

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.2.1</version>
        </dependency>

application.properties

Then we add the following configuration to the application.properties file.

spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
spring.application.name=sdwlzlapp-file
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

Spring.boot.admin.client.url points to the project interface path of our server.
Management.endpoints.web.exposure.include means that all ports are exposed and can be monitored.
Spring.application.name indicates the display name of the project on spring boot admin.
Spring.boot.admin.client.username and password are the set user name and password. It should be noted that if there is no integrated security in admin server, you can register without configuring the user name and password, which can be monitored in the server, but if the security is integrated in admin server, you need to ensure the user name and server configured in the client The user name and password configured in are consistent.

test

With the above configuration, you can register the project to admin server. Let’s start the project.

Now there is another problem. If our project itself is an integrated security framework, such as security, without login, we can’t access the interface. How can such a project be monitored by admin server? For example, we registered the security demo in the last section. Although it is monitored, it is a failed state.
13、 Spring boot elegant integration of spring boot admin program monitoring
It can be seen that it is not difficult to find problems, that is, the monitoring interface is also blocked by the project itself, so it leads to a failure state. How to modify it? In fact, it’s better to deal with it. Just let these interfaces go. We add configure (httpsecurity HTTP) in the securityconfig class of the project
13、 Spring boot elegant integration of spring boot admin program monitoring
The code is as follows:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/", "/hello").permitAll()
            .antMatchers( "/actuator/**").permitAll()
            .antMatchers( "/instances").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            //.loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }

In this way, we restart the project and find that the monitoring is successful.
13、 Spring boot elegant integration of spring boot admin program monitoring

Off the coast

So far, we have also demonstrated the general functions of spring boot admin.

Upload the code to GitHub:
https://github.com/QuellanAn/…

Follow up

Welcome to the public’s official account of “programmers love yogurt”.

Share all kinds of learning materials, including Java, Linux, big data, etc. The materials include video documents and source code, and share the high-quality technical blog articles delivered by myself and others.

If you like, remember to pay attention and share Mei

13、 Spring boot elegant integration of spring boot admin program monitoring