Tích hợp Spring Boot Admin để giám sát hiệu năng ứng dụng

1. Cài đặt Admin Server

Tạo dự án Spring Boot phiên bản 2.3.7.RELEASE với các dependency:



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


<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>

Thêm annotation vào class chính:


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

Cấu hình application.yml:


server:
  port: 9999

spring:
  application:
    name: admin-server
  boot:
    admin:
      notify:
        mail:
          to: admin@example.com
      monitor:
        info-interval: 5s
        status-interval: 5s
  security:
    user:
      name: admin
      password: secure123
  mail:
    host: smtp.example.com
    username: admin@example.com
    password: ********

Cấu hình bảo mật:


@Configuration
class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
    private final AdminServerProperties adminServer;
    private final SecurityProperties security;

    public AdminSecurityConfig(AdminServerProperties adminServer, SecurityProperties security) {
        this.adminServer = adminServer;
        this.security = security;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = 
            new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminServer.path("/"));

        http.authorizeRequests()
            .antMatchers(adminServer.path("/assets/**")).permitAll()
            .antMatchers(adminServer.path("/actuator/**")).permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage(adminServer.path("/login"))
            .successHandler(successHandler)
            .and()
            .logout()
            .logoutUrl(adminServer.path("/logout"))
            .and()
            .httpBasic()
            .and()
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .ignoringRequestMatchers(
                new AntPathRequestMatcher(adminServer.path("/instances"), "POST"),
                new AntPathRequestMatcher(adminServer.path("/instances/*"), "DELETE"),
                new AntPathRequestMatcher(adminServer.path("/actuator/**"))
            )
            .and()
            .rememberMe()
            .key(UUID.randomUUID().toString())
            .tokenValiditySeconds(1209600);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser(security.getUser().getName())
            .password("{noop}" + security.getUser().getPassword())
            .roles("USER");
    }
}

2. Cấu hình ứng dụng client

Thêm dependency vào ứng dụng cần giám sát:



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

Cấu hình client application.yml:


spring:
  boot:
    admin:
      client:
        url: http://localhost:9999
        username: admin
        password: secure123
        instance:
          prefer-ip: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

3. Kết nối qua Eureka Discovery

Cài đặt Eureka Server:


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

Cấu hình Eureka Server:


spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false
    fetch-registry: false

Cấu hình Admin Server kết nối Eureka:


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

eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    health-check-url-path: /actuator/health
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

management:
  endpoints:
    web:
      exposure:
        include: "*"

Thêm annotation vào class chính:


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

Thẻ: spring-boot-admin monitoring performance-monitoring eureka Actuator

Đăng vào ngày 9 tháng 7 lúc 00:32