Spring Boot Admin: Micro Application Monitoring Service

SpringBoot actual electricity supplier item mall (20k + star) Address: github.com/macrozheng/...

Summary

Spring Boot Admin can be applied to the indicators SpringBoot monitoring, it can be used as a micro-service architecture of the monitoring center, the paper will detail its usage.

Spring Boot Admin Profile

SpringBoot application can be exposed through various indicators Actuator process applications running, Spring Boot Admin application to monitor SpringBoot by these indicators, which are then presented through a graphical interface. Spring Boot Admin application can not only monitor each cell, and also registry Spring Cloud combined to monitor micro-service applications.

Spring Boot Admin can provide the following information to monitor the application:

  • Overview information process monitoring applications running;
  • Metrics information, such as JVM, Tomcat and process information;
  • Environment variable information, such as system properties, system environment variables and application configuration information;
  • View all Bean information created;
  • See all the configuration information applications;
  • View application and logs;
  • View JVM information;
  • View Web endpoints can access;
  • View HTTP tracking information.

Create admin-server module

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

  • Add its dependencies 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>
复制代码
  • Configured in application.yml in:
spring:
  application:
    name: admin-server
server:
  port: 9301
复制代码
  • Add @EnableAdminServer on startup class to enable the admin-server functions:
@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 as a client admin-server.

  • Add its dependencies 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>
复制代码
  • Configured in application.yml in:
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:9301 #配置admin-server地址
server:
  port: 9305
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log #添加开启admin的日志监控
复制代码
  • Start admin-server and admin-client services.

Monitoring information demo

  • Click wallboard button, select the admin-client view monitoring information;

  • Overview of monitoring information;

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

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

  • View all Bean information created;

  • See all the configuration information applications;

  • Viewing log information, you need to add the following configuration can be opened;
logging:
  file: admin-client.log #添加开启admin的日志监控
复制代码

  • View JVM information;

  • View Web endpoints can access;

  • View HTTP tracking information;

In conjunction with registration centers

Spring Boot Admin combined Spring Cloud registration centers, simply admin-server and registration center consolidation can, admin-server will automatically obtain the list of services from the registry, and then one by one to obtain monitoring information. Sign up here to Eureka Center as an example to introduce this feature.

Modify admin-server

  • Add its dependencies in pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
  • Configure the application-eureka.yml, simply add the registry configuration to:
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 startup class to enable the Service registration:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}
复制代码

Modify the admin-client

  • Add its dependencies in pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
  • Configure the application-eureka.yml, delete the original admin-server address configuration, you can 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 #添加开启admin的日志监控
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
复制代码
  • Add @EnableDiscoveryClient on startup class to enable the Service registration:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {

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

}
复制代码

Demo

  • Start eureka-server, using the boot application-eureka.yml configuration admin-server, admin-client;

  • View registry found that services have been registered: HTTP: // localhost: 8001 /

Adding login authentication

We can obtain login authentication functionality by adding support for Spring Security to admin-server.

Create admin-security-server module

  • Add its dependencies 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>
复制代码
  • In application.yml configured, configure the login user name and password, ignore monitoring information admin-security-server of:
spring:
  application:
    name: admin-security-server
  security: # 配置登录用户名和密码
    user:
      name: macro
      password: 123456
  boot:  # 不显示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/
复制代码
  • For SpringSecurity be configured so that you can register admin-client:
/**
 * 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.配置所有静态资源和登录页可以公开访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2.配置登录和登出路径
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3.开启http basic支持,admin-client注册时需要使用
                .httpBasic().and()
                .csrf()
                //4.开启基于cookie的csrf保护
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5.忽略这些路径的csrf保护以便admin-client注册
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}
复制代码
  • Modify the startup class, open AdminServer discovery and registration:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {

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

}
复制代码
  • Start eureka-server, admin-security- server, access Spring Boot Admin Login required to access the home page found: HTTP: // localhost: 9301

To use the module

springcloud-learning
├── eureka-server -- eureka注册中心
├── admin-server -- admin监控中心服务
├── admin-client -- admin监控中心监控的应用服务
└── admin-security-server -- 带登录认证的admin监控中心服务
复制代码

Project Source Address

github.com/macrozheng/…

No public

mall project a full tutorial serialized in public concern number the first time to obtain.

No public picture

Guess you like

Origin juejin.im/post/5db98a2d518825649c730f81