Microservice monitoring spring boot admin is simple to use

Spring Boot Admin is an open source community project for managing and monitoring SpringBoot applications. The application acts as a Spring Boot Admin Client to register with the Spring Boot Admin Server (via HTTP) or use the Spring Cloud registry (such as Eureka, Consul).

This article is a simple construction step without introducing its principle, the official website https://codecentric.github.io/spring-boot-admin/current/

This article cooperates with eureka to monitor services, so no admin client is needed. Just register the admin server to eureka. The admin will automatically pull services from eureka for monitoring, and other services can be monitored without changing.

It should be noted that other monitored service yml files also need to add the following code, otherwise the service monitoring page will only default info information:

#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
  endpoints:
    web:
      exposure:
        # 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
        include: "*"
  endpoint:
    health:
    # 是否展示健康检查详情
      show-details: ALWAYS

1: Create a new maven aggregation project pom.xml

<?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">
    <parent>
        <groupId>com.xxx.xxx</groupId>
        <artifactId>xxx</artifactId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>admin-server</artifactId>
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--       admin-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.6</version>
        </dependency>
         <!--   ui 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
     <!--   spring security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
      <!--  邮箱配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${artifactId}</warName>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${artifactId}</finalName>

    </build>


</project>

 

2: admin yml 配置

server:
  port: 8079
spring:
  application:
    name: server-admin
#  mail:
#    host: smtp.qq.com
#    username: [email protected]
#    password: paudschxdxijbcja
#    properties:
#      mail.debug: false
#      mail.smtp.auth: true   #安全认证(默认是true)
#      mail.smtp.port: 465
#      mail.smtp.ssl.enable: true  #开启ssl加密 否则项目启动时报530error
#      mail.smtp.ssl.socketFactory: sf
#      mail.smtp.starttls.enable: true
#      mail.smtp.starttls.required: true
  boot:
    admin:
      ui:
        title: admin-Server
#    notify:
#      mail:
#        to: [email protected]  #收件人邮箱
#        from: [email protected]  #发件人邮箱
  security:
     user:
       name: "admin"
       password: "admin"
#eureka注册中心地址
eureka:
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://127.0.0.1:8060/eureka/
    register-with-eureka: true
    fetch-registry: true

#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
  endpoints:
    web:
      exposure:
        # 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
        include: "*"
  endpoint:
    health:
    # 是否展示健康检查详情
      show-details: ALWAYS

 

3: Entry class, 2 annotations @EnableDiscoveryClient used with eureka @EnableAdminServer represents the admin server

package com.fencer.admin.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 类描述:
 *
 * @author :carry
 * @version: 1.0  CreatedDate in  2019年12月02日
 * <p>
 * 修订历史: 日期			修订者		修订描述
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminServerApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAdminServerApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        System.out.println("######################监控服务启动完成!######################");
    }
}

 

4: Security login configuration

package com.fencer.admin.server.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

/**
 * 类描述:
 *
 * @author :carry
 * @version: 1.0  CreatedDate in  2019年12月02日
 * <p>
 * 修订历史: 日期			修订者		修订描述
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter( "redirectTo" );

        http.authorizeRequests()
                .antMatchers( adminContextPath + "/assets/**" ).permitAll()
                .antMatchers( adminContextPath + "/login" ).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
                .logout().logoutUrl( adminContextPath + "/logout" ).and()
                .httpBasic().and()
                .csrf().disable();
        // @formatter:on
    }
}

 

After the above configuration is completed, start the project, enter localhost:8079 in the browser to enter the login page, enter the account password admin, you can enter the monitoring page

Click on a service to see the status of the service, other information such as bean warehouse, requested interface, etc., which can be explained by Baidu.

 

Guess you like

Origin blog.csdn.net/CarryBest/article/details/103367388