Thirteen, springboot elegant integration springboot-admin procedures for monitoring

Foreword

We know that surveillance project is particularly important, but if we use jdk own jvisualvm jconsole and then will be very complicated, and the interface is not very friendly. Earlier we used the spring boot project, but did not have a good monitoring of the project. There are spring-boot-admin can be very good to help us play a role in monitoring micro-services project in spring family.

spring-boot-admin is one of UI beautification monitoring tools package for Actuator Interface Spring Boot, it can browse all the basic information to monitor spring-boot item in the list, detailed Health, Memory, JVM, spam recycling information, various configuration information (such as the data source, and the cache hit ratio list) and the like, may also be directly modified level logger.

spring-boot-admin divided into server and client. Operation, Client Server is a single Micro service, you can view the monitoring project is one of our micro-services. So to get our project is to monitor the server, you need to register for our services to the server to go.

Well, let's try it hands.

admin-server

We first set up the service end of the spring-boot-admin, it says the server is a separate project. So we created a new springboot project. Once you've created, we do some modifications.

pom.xml

In the pom file, we introduce the following dependence

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

The above is my whole pom file, you can see that I introduced the web, admin-starter-server, security.
If you consider other, you can refer only admin-starter-server can be achieved results.

Startup class

Join @EnableAdminServer comment on our class started, if not, then the project can be started normally, but can not see anything. @EnableAdminServer role annotation is to start monitoring.

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

Configuring security

After so configured, you can start the project, but we are here to not start because the one we learned, spring-boot-security. Here we will use it up.
We have already introduced security, Next, we increase the configuration application

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

It indicates that the user can access. In addition, we create a class that inherits WebSecurityConfigurerAdapter SecurityConfig rewrite configure (HttpSecurity http) method. code show as below:

@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 we start to see about the project. Enter to start the project

http://localhost:8080

Will jump to the login screen, is now entering the home page of nothing.
Here Insert Picture Description

admin-client

Our server this configuration would be able to, and now we look to configure the client, we just find a Springboot project, or create your own a new project can be.

pom.xml

Let's join in the pom file admin-client dependent, note the version here needs and server versions of the same.

        <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 in 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 interface path to the project end of our service.
management.endpoints.web.exposure.include represents all ports are exposed, can be monitored.
spring.application.name project represents the change in the spring-boot-admin display name.
spring.boot.admin.client.username and password is to set the user name and password, to note here is that if there is no integrated security admin-server in, you do not configure the user name and password can also sign in, you can monitor the server to, but if the admin-server integrated security, you need to ensure that the user name and password in the configuration of client and server user names configured consistent.

test

Configured to the above, it can be registered to the project admin-server in again, and we started about the project.
Here Insert Picture Description

Now there is a problem, if we project itself is an integrated security framework, such as security, are not logged in, then can not access interface, that such projects be monitored admin-server how to do? For example, on our last section of the security of the demo, we registered in, although monitored, but it is a failed state.
Here Insert Picture Description
We can see, the problem is not difficult to find, and that is monitored interface is also the project itself blocked, so that led to the failure of the state, it is up to how to modify it, in fact, Ye Hao handle, you can let go of these few interfaces a. We configure (HttpSecurity http) plus SecurityConfig class project
Here Insert Picture Description
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();
    }

We restart the project, we found that can monitor success.
Here Insert Picture Description

Extra

So far, we generally will be considered functional spring-boot-admin demonstrates next.

Code is uploaded to GitHub:
https://github.com/QuellanAn/springbootadmin

Come follow ♡

Welcome to the personal public concern number "Programmers love yogurt"

Share a variety of learning materials including java, linux, big data. Information includes video documentation and source code, while sharing themselves and delivery of high-quality technology blog.

If you like attention and remember to share yo ❤

file

Guess you like

Origin www.cnblogs.com/quellanan/p/12141925.html