Spring Boot Admin Introduction

SpringBoot Admin is an open source community project for managing and monitoring SpringBoot applications. Applications are discovered as Spring Boot Admin Clients to either register for Spring Boot Admin Server (over HTTP) or use a SpringCloud registry (e.g. Eureka, Consul). UI is an AngularJs application that shows some monitoring on endpoints of Spring Boot Admin Client. Common functions or monitoring are as follows:

  • Show health
  • Displays detailed information, for example
    • JVM and memory metrics
    • Micrometer. IO indicators
    • Data source index
    • Cache indicators
  • Display the build information number
  • Watch and download log files
  • View JVM system and environment properties
  • View Spring Boot configuration properties
  • Spring Cloud postable/env- and/refresh-endpoint are supported
  • Easy log level management
  • Interact with JMX – beans
  • Viewing a thread dump
  • Viewing HTTP Tracing
  • Check the auditevents
  • See HTTP endpoints
  • Viewing scheduled Tasks
  • View and Delete active sessions (using spring-Session)
  • Check out the Flyway/Liquibase database migration
  • Download heapdumps
  • Notification of status changes (via email, Slack, Hipchat,……)
  • Event logs of state changes (non-persistent)

Quick start

Example Create the Spring Boot Admin Server

All projects in this paper have Spring Boot version 2.1.0 and Spring Cloud version finchley.sr2. The example uses Maven multi-module format. The parent POM file introduces the following dependencies (see source code for full dependencies) :


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0. RELEASE</version>
        <relativePath/>
    </parent>
    
    
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <spring-cloud.version>Finchley.SR2</spring-cloud.version>


Copy the code

Introduction of admin-server dependency and Web dependency in the project admin-server, the code is as follows:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

Then add @enabLeadMinServer to the AdminServerApplication class of the project to enable the function of AdminServer.


@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

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

In the project configuration file application.yml, configure the program name and the program port as follows:

spring:
  application:
    name: admin-server
server:
  port: 8769
Copy the code

The Admin Server is now created.

Example Create a Spring Boot Admin Client

Import admin-Client startup dependencies and Web startup dependencies in the POM file of the Admin-Client project. The code is as follows:


        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


Copy the code

In the application. Yml configuration file, configure the application name and port information, and the address registered with the admin-server is http://localhost:8769. In the end, all the ports of the actuator are exposed as follows:

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8769
server:
  port: 8768

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

Copy the code

The startup files in the project are as follows:


@SpringBootApplication
public class AdminClientApplication {

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

Start two projects at a time and type localhost:8769 in the browser. The browser displays the following interface:

Check the wallboard:

Click wallboard to view admin-client specific information, such as memory status information:

You can also look at spring beans:

More monitoring information, experience for yourself.

Spring Boot Admin is used with the SC registry

As in the previous case, Spring Boot version 2.1.0 and Spring Cloud version finchley.sr2 are used in this case. The example uses Maven multi-module format. The parent POM file introduces the following dependencies (full dependencies are in the source code), which are omitted here.

Setting up a registry

It is also possible for the registry to use Eureka and Consul, which are introduced in the POM file in the Eureka-Server project:

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Copy the code

Configure eureka-server port information, defaultZone, and prevent self-registration. Finally, the system exposes all the ports of the EUreka-Server’s actuator.

spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false
    fetch-registry: false
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
Copy the code

Start EurekaServer with @enableEurekaserver annotation in the project startup file EurekaServerApplication.


@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

The Eureka-server is set up.

Set up the admin server. –

In the POM file of the Admin-server project, the startup dependencies of admin-server, Web, and Eureka-client are introduced as follows:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
        
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

Then configure admin-server, application name, and port information. It registers with the registry at http://localhost:8761. Finally, all the ports of the actuator are exposed. The configuration is as follows:


spring:
  application:
    name: admin-server
server:
  port: 8769
eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
Copy the code

Add @enableadMinServer annotation to the project startup class AdminServerApplication to enable the admin server function, add @enableDiscoveryClient annotation to enable the Eurke Client function.


@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {

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

Set up the admin – client

In the POM file of admin-client, the following dependencies are introduced. Because 2.1.0 uses WebFlux, the starting dependencies of WebFlux and Eureka-client are introduced, and the starting dependencies of the actuator are used as follows:


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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
Copy the code

In the project configuration file, configure the application name, ports, addresses registered with the registry, and all ports exposed.


spring:
  application:
    name: admin-client
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
server:
  port: 8762

Copy the code

Add @enableDiscoveryClie to the startup class to enable DiscoveryClient.

@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {

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

Start three projects at a time and access localhost:8769 in a browser, which will display the same interface as in the previous section.

Integrated spring security

In version 2.1.0, the Hystrix Dashboard was removed, and the login interface was integrated into the Spring Security module by default. Once spring Security was added, the login module was integrated.

You only need to change the Admin-server project by introducing the following dependencies into the ADMIN-server project POM file:

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

The spring Security user name and password should be configured in the admin-server configuration file application.yml, and the metadata-map information should be included in the service registration.

spring:
  security:
    user:
      name: "admin"
      password: "admin"
      
eureka:
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

Copy the code

Write a configuration class SecuritySecureConfig inheritance WebSecurityConfigurerAdapter, configuration is as follows:


@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}}Copy the code

Restart the project and access http://localhost:8769/ in the browser. You are redirected to the login page. The user name and password for login are admin and admin respectively configured in the configuration file.

Integrated mailbox alarm function

Spring Boot Admin can also integrate mailbox alarm function, such as service is not healthy, offline, can send an email to the specified mailbox. Integration is very simple, just need to modify admin-server:

In the admin-server project Pom file, add the starting dependency of mail, the code is as follows:

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

Copy the code

In the configuration file application.yml, you need to configure mail-related configurations as follows:

spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: [email protected]

Copy the code

After the above configuration, when our registered client status changes from UP to OFFLINE or any other state, the server will automatically send email to the address configured above.

Download the source code

Quick start: github.com/forezp/Spri…

With Spring Cloud: github.com/forezp/Spri…

The resources

Codecentric. Making. IO/spring – the boot…

Github.com/codecentric…



Scan and support the author

(Please indicate the author and source of the article reproduced from the blog of Fang Zhipeng)