Search
📒

7-4. Spring Boot Actuator

여태까지 만든 개발 산출물을 상용 서비스로 제공하는 단계에서는, 현재 실행중인 서비스 프로세스가 어떤 상태로 운영되고 있는지 알아야 할 필요가 있습니다. 정상적으로 실행되고 있는지, 사용하고 있는 디스크 공간이 충분한지, 어느정도의 요청이 들어왔는지 같은 부분들을 실시간으로 받아오는 기능들이 필요합니다. 이런 기능들을 Production-Ready Feature라고 부르게 되는데, 이런 기능들이 Spring Boot에서는 Spring Boot Actuator에 구현이 되어 있습니다. build.gradle 에 추가하여 그 기능을 알아봅시다.
(* Production-Ready 라는 의미는 서비스 제공자의 역할에 따라 많이 다르게 해석되는 부분이 있습니다.)
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Java
복사
이후 별다른 수정없이 Application을 실행하면 로그에 다음과 같은 내용이 출력됩니다.
... EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' ...
Plain Text
복사
이제 Postman 또는 Browser에서 http://localhost:8080/actuator 로 요청을 보내봅시다.
{ "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:8080/actuator/health/{*path}", "templated": true } } }
JSON
복사
이제 Actuator를 간단히 구성해 봅시다.

Actuator 기본 설정

/actuator 경로로 갔을 때 나타난 JSON 에 담긴 데이터는 현재 Actuator에서 사용할 수 있는 기능들이 나열됩니다. 초기에 사용 가능하도록 설정된 기본 기능들은 현재 Application의 상태를 확인할 수 있는 /actuator/health/actuator/health/{*path} 입니다. 우선 사용할 수 있는 기능들을 보기 위한 설정을 해봅시다. 현재 사용중인 profile에 맞는 설정 파일(YAML)에,
management: endpoints: enabled-by-default: true web: exposure: include: health,info,loggers endpoint: health: show-details: always show-components: always
YAML
복사
를 작성하고 다시 /actuator 결과를 살펴봅시다.
{ "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:8080/actuator/health/{*path}", "templated": true }, "info": { "href": "http://localhost:8080/actuator/info", "templated": false }, "loggers-name": { "href": "http://localhost:8080/actuator/loggers/{name}", "templated": true }, "loggers": { "href": "http://localhost:8080/actuator/loggers", "templated": false } } }
JSON
복사
사용 가능한 링크가 더 늘어난 모습을 확인할 수 있습니다.
management.endpoints.enabled-by-default 설정은 기본적으로 Actuator에서 제공하는 기능들이 사용가능한 상태인지에 대한 기본값을 설정하게 됩니다. 만약 이 값이 false 일 경우, Actuator의 기능들 대부분이 정지가 되며 사용할 수 없습니다. 반면 Actuator의 기능을 반드시 다 활용할 필요는 없기 때문에, 해당 값을 false 로 유지하고 사용할 기능만 설정하는 것도 가능합니다.
예시

개별 기능 설정

management.endpoint.<id> 와 그 하위 설정들은 개별 기능들의 설정을 담당합니다. 예시로, 위의 설정에서 management.endpoint.health.show-details=alwaysmanagement.endpoint.health.show-components=always 의 경우 /actuator/health 로 요청을 보냈을때 그 결과를 달리 할 수 있습니다.
예시

Loggers 기능 써보기

이 설정을 살펴보며 추가한 loggers 는 현재 설정된 logger 정보(LoggerFactory.getLogger()를 통해서 생성한 Logger들)를 반환합니다.
GET /actuator/loggers
그리고 /actuator/loggers/{name} 으로 요청을 보내면, 해당 Logger의 현재 로그 레벨과, Actuator에서 설정된 로그 레벨을 확인할 수 있습니다.
GET /actuator/loggers/{name}
동일한 경로(Path)로 JSON 요청을 보내게 될 경우, configuredLevel 이 변경됩니다.
POST /actuator/loggers/{name}
이러면 현재 실행중인 Application의 로그 레벨을 바로 바꿀 수 있습니다.

Actuator와 Prometheus / Grafana

Prometheus와 Grafana는 세계적으로 많이 사용하는 모니터링 도구 입니다. 보통은 두가지를 같이 사용하게 됩니다.
Prometheus는 Web Application의 정보를 HTTP 요청을 통해서 받아와 정리하고, 실시간 계측(Metric) 정보를 제공하며, 알림을 주는 도구입니다.
Grafana는 Prometheus를 비롯한 도구에서 나온 계측 데이터를 시각화 하고, 분석하는 도구입니다.
여기서 HTTP 요청을 통해서 받아온다는 부분은, 저희가 Postman이나 브라우저에 주소를 입력하는 것과 동일한 의미입니다. 그리고 Spring Boot Actuator는, 이때 Prometheus가 사용할 데이터를 종합하여 보내주기 위한 endpoint를 제공합니다.