SpringBoot e-Commerce project mall (20K + STAR) address: github.com/macrozheng/…

Abstract

SpringBoot Admin can monitor various indicators of SpringBoot applications. It can be used as a monitoring center in the micro-service architecture. This article describes its usage in detail.

Spring Boot Admin Introduction

The SpringBoot application can use the Actuator to expose various indicators in the running process of the application. SpringBoot Admin monitors the SpringBoot application through these indicators and presents them through a graphical interface. The Spring Boot Admin can monitor not only individual applications, but also microservice applications in conjunction with the Spring Cloud registry.

The Spring Boot Admin provides the following monitoring information for applications:

  • Monitor the overview information during application running.
  • Metrics, such as JVM, Tomcat, and process information;
  • Environment variable information, such as system properties, system environment variables, and application configuration information;
  • View information about all created beans.
  • View all configuration information about an application.
  • View application run logs.
  • View JVM information;
  • View accessible Web endpoints;
  • View HTTP tracing information.

Create an admin-server module

Here we create an Admin-server module to act as a monitoring center to demonstrate its functionality.

  • Add dependencies to 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>
Copy the code
  • Configure in application.yml:
spring:
  application:
    name: admin-server
server:
  port: 9301
Copy the code
  • Add @enableadMinServer to the startup class to enable admin-server functionality:
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

Create an Admin-client module

Here we create an Admin-client module that registers with admin-server as a client.

  • Add dependencies to 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>
Copy the code
  • 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: The '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log Add enable log monitoring for admin
Copy the code
  • Start the admin-server and admin-client services.

Monitoring Information Demonstration

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

  • Click wallBoard button and select Admin-client to view monitoring information.

  • Monitoring information overview;

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

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

  • View information about all created beans.

  • View all configuration information about an application.

  • To view logs, you need to add the following configuration.
logging:
  file: admin-client.log Add enable log monitoring for admin
Copy the code

  • View JVM information;

  • View accessible Web endpoints;

  • View HTTP trace information.

Use in conjunction with the registry

Spring Boot Admin Is used with the Spring Cloud registry. You only need to integrate admin-server with the registry. The admin-server automatically obtains the service list from the registry and then obtains monitoring information one by one. The Eureka registry is used as an example to illustrate this feature.

Modify the admin server. –

  • Add dependencies to pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code
  • To configure in application-eureka.yml, simply 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/
Copy the code
  • Add @enableDiscoveryClient to the startup class to enable service registration:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

Modify the admin – client

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

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

Function demonstration

  • Use application-eureka. Yml to start admin-server and admin-client.

  • View registry discovery services are registered: http://localhost:8001/

  • You can view the Spring Boot Admin home page and find the service information: http://localhost:9301

Adding Login Authentication

Login authentication can be achieved by adding Spring Security support to admin-server.

Create the admin-security-server module

  • Add dependencies to 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>
Copy the code
  • Yml to configure the login user name and password, ignoring the monitoring information of admin-security-server:
spring:
  application:
    name: admin-security-server
  security: Configure the login user name and password
    user:
      name: macro
      password: 123456
  boot:  # do not display monitoring information about 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/
Copy the code
  • Configure SpringSecurity 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. Make all static resources and login pages publicly accessible
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2. Configure the login and logout paths
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3. Enable HTTP Basic, which is required for admin-client registration
                .httpBasic().and()
                .csrf()
                //4. Enable cookie-based CSRF protection
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5. Ignore CSRF protection for these paths for admin-client registration
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"); }}Copy the code
  • Modify the startup class to enable AdminServer and registration discovery:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {

    public static void main(String[] args) { SpringApplication.run(AdminSecurityServerApplication.class, args); }}Copy the code
  • Start eureka-server, admin-security-server, and access the Spring Boot admin home page. You need to log in to http://localhost:9301

The module used

Springcloud - learning ├ ─ ─ eureka - server-- Eureka Registry├ ─ ─ the admin server. --- admin Monitoring center service├ ─ ─ the admin - client- Admin Indicates the application services monitored by the monitoring center└ ─ ─ the admin ws-security - server-- Admin monitoring center service with login authentication
Copy the code

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.