본문 바로가기
프레임워크/스프링

[Spring] actuator

by so5663 2024. 2. 15.

액츄에이터?

시스템을 움직이거나 제어하는데 쓰이는 기계 장치라는 뜻입니다.

 

 

build.gradle - 추가

implementation 'org.springframework.boot:spring-boot-starter-actuator' //actuator 추가

 

액츄에이터는 /actuator 경로를 통해서 기능을 제공한다.

{
     "_links": {
         "self": {
             "href": "http://localhost:8080/actuator",
             "templated": false
         },
         "health-path": {
             "href": "http://localhost:8080/actuator/health/{*path}",
             "templated": true
         },
         "health": {
             "href": "http://localhost:8080/actuator/health",
             "templated": false
         }
     }
}

 

http://localhost:8080/actuator/health는 서버가 잘 작동되는지 알려준다 

 

application.yml

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

를 추가하면 액츄에이터가 제공하는 모든 기능을 확인이 가능하다.

 

엔드포인트 설정

1. 엔드포인트 활성

2. 엔드포인트 노출

대부분 엔드포인트는 활성화 되어있는데 노출만 되어있지 않는다고 한다.

 

엔드포인트 활성화 application.yml - shutdown 엔드포인트 활성화

management:
	endpoint:
        shutdown:
             enabled: true
        endpoints:
        	web:
            	exposure:
                	include: "*"

 

 

전체 엔드포인트

https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints

 

헬스정보

헬스 정보를 사용하면 애플리케이션에 문제가 발생했을 때 문제를 빠르게 인지할 수 있다. http://localhost:8080/actuator/health

https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.health.auto-configured-health-indicators

 

Production-ready Features

You can enable recording of HTTP exchanges by providing a bean of type HttpExchangeRepository in your application’s configuration. For convenience, Spring Boot offers InMemoryHttpExchangeRepository, which, by default, stores the last 100 request-response

docs.spring.io

애플리케이션 정보

info 엔드포인트는 애플리케이션의 기본 정보를 노출한다.

 

실행 http://localhost:8080/actuator/info

처음에 실행하면 정보들이 보이지 않을 것이다.

java , os 기능을 활성화해보자. java, os JAVA, OS 정보를 확인해보자.

 

application.yml - 내용 추가

management: 
  info: 
    java: 
      enabled: true 
    os: 
      enabled: true

 

management.info..enabled 의 값을 true 로 지정하면 활성화 된다

 

로거

loggers 엔드포인트를 사용하면 로깅과 관련된 정보를 확인하고, 또 실시간으로 변경할 수도 있다.

 

application.yml - 내용 추가

logging:
  level:
    hello.controller: debug

 

실행

http://localhost:8080/actuator/loggers/hello.controller

 

결과

{ "configuredLevel": "DEBUG", "effectiveLevel": "DEBUG" }

 

 

실시간 로그 레벨 변경 개발 서버는 보통 DEBUG 로그를 사용하지만, 운영 서버는 보통 요청이 아주 많다. 따라서 로그도 너무 많이 남기 때문에 DEBUG 로그까지 모두 출력하게 되면 성능이나 디스크에 영향을 주게 된다. 그래서 운영 서버는 중요하다고 판단되는 INFO 로그 레벨을 사용한다.

그런데 서비스 운영중에 문제가 있어서 급하게 DEBUG 나 TRACE 로그를 남겨서 확인해야 확인하고 싶다면 어떻게 해야할까? 일반적으로는 로깅 설정을 변경하고, 서버를 다시 시작해야 한다.

 

loggers 엔드포인트를 사용하면 애플리케이션을 다시 시작하지 않고, 실시간으로 로그 레벨을 변경할 수 있다

 

postman을 사용했다.

POST http://localhost:8080/actuator/loggers/hello.controller

 

결과

{ "configuredLevel": "TRACE" }

 

HTTP 요청 응답 기록

HTTP 요청과 응답의 과거 기록을 확인하고 싶다면 httpexchanges 엔드포인트를 사용하면 된다.

HttpExchangeRepository 인터페이스의 구현체를 빈으로 등록하면 httpexchanges 엔드포인트를 사용할 수 있다.

스프링 부트는 기본으로 InMemoryHttpExchangeRepository 구현체를 제공한다.

 

InMemoryHttpExchangeRepository 추가

@SpringBootApplication
public class ActuatorApplication {
 public static void main(String[] args) {
 SpringApplication.run(ActuatorApplication.class, args);
 }
 @Bean
 public InMemoryHttpExchangeRepository httpExchangeRepository() {
 return new InMemoryHttpExchangeRepository();
 }
}

 

실행하면 지금까지 실행한 HTTP 요청과 응답정보를 확인할 수 있습니다.

실제운영에서는 모니터링 툴이나 핀포인트, Zipkin 같은 다른 기술을 사용하는 것이 좋다고 합니다.

 

출처

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-%ED%95%B5%EC%8B%AC%EC%9B%90%EB%A6%AC-%ED%99%9C%EC%9A%A9/dashboard

 

스프링 부트 - 핵심 원리와 활용 강의 - 인프런

실무에 필요한 스프링 부트는 이 강의 하나로 모두 정리해드립니다., 백엔드 개발자를 위한 스프링 부트 끝판왕! 실무에 필요한 내용을 모두 담았습니다.  [임베딩 영상] 김영한의 스프링 완전

www.inflearn.com