1. 개요 

Actuator는 JMX를 이용하여 내부 상태를 노출하는 기능이다.


2. Spring Boot 프로젝트 생성

starter.spring.io 사이트에서 신규 프로젝트를 생성하고 Dependency에 Web Starter와 Actuator를 추가한다.

이러한 평범한 프로젝트가 생성된다.

package io.sarc.springboot.BootDemo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class BootDemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(BootDemoApplication.class, args);
  }
}

3. Actuator 접속

mvn spring-boot:run으로 실행한 후 http://localhost:8080/actuator로 접속한다.

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-component":{"href":"http://localhost:8080/actuator/health/{component}","templated":true},"health-component-instance":{"href":"http://localhost:8080/actuator/health/{component}/{instance}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}

4. /actuator/health

http://localhost:8080/actuator/health

{"status":"UP"}

5. /actuator/metrics

기본적으로는 비활성화되어 있다.

http://localhost:8080/actuator/metrics

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Sep 01 14:18:53 KST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

에러가 날 것이다.

application.yml에 다음 내용을 추가한다.

management:
  endpoints:
    web:
      exposure:
        include: info, health, metrics

다시 metrics를 호출한다.

{"names":["jvm.memory.max","jvm.threads.states","process.files.max","jvm.gc.memory.promoted","system.load.average.1m","jvm.memory.used","jvm.gc.max.data.size","jvm.memory.committed","system.cpu.count","logback.events","tomcat.global.sent","jvm.buffer.memory.used","tomcat.sessions.created","jvm.threads.daemon","system.cpu.usage","jvm.gc.memory.allocated","tomcat.global.request.max","tomcat.global.request","tomcat.sessions.expired","jvm.threads.live","jvm.threads.peak","tomcat.global.received","process.uptime","tomcat.sessions.rejected","process.cpu.usage","tomcat.threads.config.max","jvm.classes.loaded","jvm.classes.unloaded","tomcat.global.error","tomcat.sessions.active.current","tomcat.sessions.alive.max","jvm.gc.live.data.size","tomcat.threads.current","process.files.open","jvm.buffer.count","jvm.buffer.total.capacity","tomcat.sessions.active.max","tomcat.threads.busy","process.start.time"]}

이제 각 상세 항목을 조회할 수 있다.

http://localhost:8080/actuator/metrics/jvm.classes.loaded

{"name":"jvm.classes.loaded","description":"The number of classes that are currently loaded in the Java virtual machine","baseUnit":"classes","measurements":[{"statistic":"VALUE","value":10684.0}],"availableTags":[]}

http://localhost:8080/actuator/metrics/jvm.threads.states

{"name":"jvm.threads.states","description":"The current number of threads having TERMINATED state","baseUnit":"threads","measurements":[{"statistic":"VALUE","value":25.0}],"availableTags":[{"tag":"state","values":["timed-waiting","new","runnable","blocked","waiting","terminated"]}]}

6. Enable & Disable

application.yml에 management.endpoints.enabled-by-default: false를 추가하면 모든 Actuator가 disable된다.

만약 특정 Actuator만 허용하고 싶다면 이렇게 할 수 있다.

managementendpoints.enabled-by-default: false
management.endpoints.info.enabled: true
management.endpoints.health.enabled: true