스프링 부트 액추에이터는 다양한 정보를 가공해서 제공하는 강력한 도구이다. 기본 엔드포인트 외에도, 개발자의 요구사항에 맞춘 커스텀 기능을 설정할 수 있다. 커스텀 기능을 설정하는 방법에는 크게 두 가지가 있다.
액추에이터의 기본 제공 엔드포인트에 추가 정보를 포함하거나 기능을 확장할 수 있다. InfoContributor
인터페이스를 구현하여 info
엔드포인트에 커스텀 정보를 추가할 수 있다.
InfoContributor
인터페이스 구현InfoContributor
인터페이스를 구현하는 클래스를 생성하여 정보를 추가한다.
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class CustomInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
Map<String, Object> content = new HashMap<>();
content.put("code-info", "InfoContributor 구현체에서 정의한 정보입니다.");
builder.withDetail("custom-info-contributor", content);
}
}
위의 CustomInfoContributor
클래스는 InfoContributor
인터페이스를 구현하며, info
엔드포인트에 커스텀 정보를 추가한다. contribute
메서드에서 Builder
객체를 사용하여 추가할 정보를 설정한다.
이 클래스를 추가한 후 애플리케이션을 재가동하고 /actuator/info
엔드포인트를 호출하면, 기존 application.properties
에서 정의한 속성값 외에 커스텀 정보도 포함된 것을 확인할 수 있다.
{
"organization": {
"name": "wikibooks"
},
"contact": {
"email": "thinkground.flature@email.com",
"phoneNumber": "010-1234-5678"
},
"custom-info-contributor": {
"code-info": "InfoContributor 구현체에서 정의한 정보입니다."
}
}
새로운 엔드포인트를 정의하여 커스텀 기능을 구현할 수 있다. 이를 위해 @Endpoint
어노테이션을 사용한다.
아래 예시는 애플리케이션에 메모를 기록할 수 있는 기능을 제공하는 커스텀 엔드포인트이다.
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@Endpoint(id = "note")
public class NoteEndpoint {
private Map<String, Object> noteContent = new HashMap<>();
@ReadOperation
public Map<String, Object> getNote(){
return noteContent;
}
@WriteOperation
public Map<String, Object> writeNote(String key, Object value){
noteContent.put(key, value);
return noteContent;
}
@DeleteOperation
public Map<String, Object> deleteNote(String key){
noteContent.remove(key);
return noteContent;
}
}
@Endpoint
: 이 클래스가 엔드포인트임을 선언하며, id
속성으로 엔드포인트의 경로를 정의한다.@ReadOperation
: GET 요청을 처리한다.@WriteOperation
: POST 요청을 처리한다.@DeleteOperation
: DELETE 요청을 처리한다.애플리케이션을 재가동하고, GET
, POST
, DELETE
요청을 통해 엔드포인트를 호출하여 동작을 확인할 수 있다.
GET http://localhost:8080/actuator/note
빈 JSON 객체를 반환한다.
{}
Talend API Tester 등을 사용하여 POST
요청을 보낸다.
POST http://localhost:8080/actuator/note
Content-Type: application/json
{
"key": "description",
"value": "설명 부분을 기입한다."
}
다시 GET
요청을 보내면 추가한 메모가 포함된 결과를 확인할 수 있습니다.
{
"description": "설명 부분을 기입한다."
}
DELETE http://localhost:8080/actuator/note
Query Parameters: key=description
키 값을 삭제하고 다시 GET
요청을 보내면 빈 JSON 객체가 반환된다.
이와 같이 커스텀 엔드포인트를 생성하면 더욱 확장성 있는 기능을 개발할 수 있다.
💡 Tip.
스프링 부트 액추에이터의 자세한 내용은 공식 페이지에서 확인할 수 있다.
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html