🍃 Spring/🌱 Spring Boot를 이용한 RESTful Web Service

[Spring Boot를 이용한 RESTful Web Services 개발] 31강

락꿈사 2022. 3. 3. 17:46

REST API Monitoring을 위한 Actuator 설정

  • 스프링 부트 프로젝트에 모니터를 하는 기능 제공
  • Spring boot starter Component의 Acutator 라이브러리 추가 (이것만으로도 간단하게 어플리케이션의 상태를 쉽게 파악할 수 있음)
  • pom.xml에 의존성 추가
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.6.2</version>
</dependency>
  • application.yml에 내용 추가
  • 더 많은 정보를 확인할 수 있음
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • SwaggerConfig 클래스에 추가 (이 내용을 추가하지 않으면 에러남. 링크 참고)
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
    List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
    Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
    allEndpoints.addAll(webEndpoints);
    allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
    allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
    String basePath = webEndpointProperties.getBasePath();
    EndpointMapping endpointMapping = new EndpointMapping(basePath);
    boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
    return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}

private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
    return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}

다양한 정보의 HATEOAS를 확인할 수 있음
health 링크에 들어가자 현재 서버가 작동중인 것을 확인할 수 있음