Spring Boot Admin: microservice application monitoring

Spring boot actual e-commerce project mall (20k+star) address: https://github.com/macrozheng/mall

abstract

Spring Boot Admin can monitor various indicators of spring boot application and can be used as the monitoring center in the microservice architecture. This article will introduce its usage in detail.

Introduction to Spring Boot Admin

Spring boot application can expose various indicators during application running through activator. Spring Boot Admin monitors spring boot application through these indicators, and then presents them through graphical interface. Spring Boot Admin can not only monitor single application, but also combine with Spring Cloud registry to monitor microservice application.

Spring Boot Admin can provide the following monitoring information of the application:

  • Monitor the overview information during application operation;
  • Metrics information, such as JVM, Tomcat, and process information;
  • Environment variable information, such as system attribute, system environment variable and application configuration information;
  • View all created Bean information;
  • View all configuration information in the application;
  • View application operation log information;
  • View JVM information;
  • View the accessible Web endpoint;
  • View HTTP trace information.

Create admin server module

Here we create an admin server module to demonstrate its functions as a monitoring center.

  • Add dependency in pom.xml:
<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>
  • Configure in application.yml:
spring:
  application:
    name: admin-server
server:
  port: 9301
  • Add @ EnableAdminServer on the startup class to enable the admin server feature:
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }

}

Create admin client module

Here we create an admin client module to register to the admin server as a client.

  • Add dependency in pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
  • Configure in application.yml:
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:9301 configure admin server address
server:
  port: 9305
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log #Add log monitoring to enable admin
  • Start the admin server and admin client services.

Demonstration of monitoring information

  • Visit the following address to open the Spring Boot Admin home page: http://localhost:9301

  • Click the wallboard button and select admin client to view the monitoring information;
  • Overview of monitoring information;

  • Metrics information, such as JVM, Tomcat, and process information;

  • Environment variable information, such as system attribute, system environment variable and application configuration information;

  • View all created Bean information;

  • View all configuration information in the application;

  • To view log information, you need to add the following configuration to enable it;
logging:
  file: admin-client.log #Add log monitoring to enable admin

  • View JVM information;

  • View the accessible Web endpoint;

  • View HTTP trace information;

Use in combination with the registration center

Spring Boot Admin is used in combination with the Spring Cloud registration center. Just integrate the admin server and the registration center. The admin server will automatically obtain the service list from the registration center, and then obtain the monitoring information one by one. Here we take Eureka registry as an example to introduce this function.

Modify admin server

  • Add dependency in pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • To configure in application-eureka.yml, just add the registry configuration:
spring:
  application:
    name: admin-server
server:
  port: 9301
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • Add @ EnableDiscoveryClient on the startup class to enable service registration:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }

}

Modify admin client

  • Add dependency in pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • Configure in application-eureka.yml, delete the original admin server address configuration, and add the registry configuration
spring:
  application:
    name: admin-client
server:
  port: 9305
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log #Add log monitoring to enable admin
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • Add @ EnableDiscoveryClient on the startup class to enable service registration:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminClientApplication.class, args);
    }

}

Function demonstration

  • Start Eureka server, use application-eureka.yml configuration to start admin server, admin client;
  • View registry discovery services are registered: http://localhost:8001/

  • Check the Spring Boot Admin homepage to see the service information: http://localhost:9301

Add login authentication

We can get login authentication function by adding Spring Security support to admin server.

Create admin security server module

  • Add dependency in pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • Configure in application.yml, configure login user name and password, and ignore the monitoring information of admin security server:
spring:
  application:
    name: admin-security-server
  security: # Configure login user name and password
    user:
      name: macro
      password: 123456
  boot:  # Do not display monitoring information of admin security server
    admin:
      discovery:
        ignored-services: ${spring.application.name}
server:
  port: 9301
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  • Configure spring security so that admin client can register:
/**
 * Created by macro on 2019/9/30.
 */
@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 {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //1. Configure all static resources and login pages to be publicly accessible
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2. Configure login and logout paths
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3. Enable http basic support, which is required for admin client registration
                .httpBasic().and()
                .csrf()
                //4. Enable cookie based csrf protection
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5. Ignore the csrf protection of these paths for admin client registration
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}
  • Modify the startup class to enable the AdminServer and registration discovery function:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminSecurityServerApplication.class, args);
    }

}
  • Start Eureka server, admin security server, and visit the Spring Boot Admin homepage. You need to log in to access: http://localhost:9301

Modules used

springcloud-learning
├── eureka-server -- eureka Registry Center
├── admin-server -- admin Monitoring center service
├── admin-client -- admin Application services monitored by monitoring center
└── admin-security-server -- With login authentication admin Monitoring center service

Project source address

https://github.com/macrozheng/springcloud-learning

official account

mall project In a series of learning guides, we should pay attention to the first time the public number is obtained.

Keywords: Java Spring xml jvm github

Added by natronp on Thu, 07 Nov 2019 04:17:43 +0200