왜 필요하게 되었을까?

  • SpringBoot 가 나오기 이전에는 애플리케이션의 상태를 직접 추적하고 모니터링을 하기 위해 많은 노력이 필요하였음.
  • 개발 시간의 낭비와 애플리케이션 문제를 진단하고 해결하는데 어려움이 초래됨.
  • 지속적인 모니터링을 통한 DevOps 문화 지원 가능.

Spring Boot Actuator 실습

Dependencies 추가

Spring Boot Actuator를 추가해준다.

WebSecurityConfig 파일 수정

@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class WebSecurity {
    private final BCryptPasswordEncoder passwordEncoder;
    private final UserService userService;
    private final Environment env;
    @Bean
    public SecurityFilterChain configure(HttpSecurity http) throws Exception{
        AuthenticationManagerBuilder authenticationManagerBuilder =
                http.getSharedObject(AuthenticationManagerBuilder.class);
        authenticationManagerBuilder.userDetailsService(userService).passwordEncoder(passwordEncoder);
        AuthenticationManager authenticationManager= authenticationManagerBuilder.build();
        http.csrf(AbstractHttpConfigurer::disable);
        http.formLogin(form->form
                .loginPage("/login")
                .permitAll());
        http.authorizeHttpRequests(request -> request
                .requestMatchers("/actuator/**").permitAll()
                .requestMatchers("/h2-console/**").permitAll()
                .requestMatchers("/**").permitAll()
        );
        http.authenticationManager(authenticationManager);
        http.addFilter(getAuthenticationFilter(authenticationManager));

        http.headers(header -> header
                .frameOptions(HeadersConfigurer.FrameOptionsConfig::disable));
        return http.build();
    }
    private AuthenticationFilter getAuthenticationFilter(AuthenticationManager authenticationManager) throws  Exception{
        return new AuthenticationFilter(userService,authenticationManager,env);
    }

}

requestMatchers에 /actuator/** 를 추가하여 /actuator 하위 경로로 오는 요청을 허용해준다. (실제 운영단계에서는 admin Role을 가진 사람만 허용되도록 해야 할 것 같다.)

application.yml 설정 추가

management:
  endpoints:
    web:
      exposure:
        include: info, refresh, health, beans

management.endpoints.web.exposure.include 에 actuator 에서 활성화할 기능을 명시해준다.

  • refresh(POST): 빌드된 파일은 그대로인데 설정 정보가 변경되었을 때 변경사항을 반영해주기 위해 사용.

  • health(GET): 현재 서비스의 상태를 표시해줌.

  • beans(GET): 현재 스프링 컨텍스트에 등록된 모든 빈에 대한 정보를 제공한다. 주로 디버깅과 모니터링 목적으로 사용한다.

  • httpexchanges(GET): 요청이 어떻게 처리되었는지에 대한 정보가 표시된다.
    기본적으로는 컨텍스트에 등록되어있지 않기 때문에 직접 스프링 빈으로 등록시켜주어야한다.

profile
백엔드 주니어 개발자

0개의 댓글

Powered by GraphCDN, the GraphQL CDN