Web/환경설정 관련
스프링 부트 모니터링(2.2) - Info 엔드포인트 탐구
now0204
2024. 5. 14. 16:49
- info엔드포인트를 통해 애플리케이션 관련 정보를 확인할 수 있다.
- info 엔드포인트는 기본적으로 아무것도 제공하지 않는다. 하지만 적절히 커스텀해서 원하는 정보를 노출할 수 있다.
1. 다양한 정보 표시해보기
- application.properties에 다음과 같은 내용을 추가해보자
info.app.name= Spring Boot Actuator Info Application
info.app.description=Spring Boot application that explores the /info endpoint
info.app.version=1.0.0
management.info.env.enabled=true
- http://localhost:8080/actuator/info에 접근하면, 위와 같이 미리 적어둔 정보를 읽어올 수 있다.
- 또한, 빌드 정보를 info에 추가할 수 있다.
- 먼저, gradle에 아래 내용을 추가하자
springBoot {
buildInfo()
}
- 다음으로 application.properties 다음 내용을 추가하면 읽어올 수 있다.
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@
info.build.properties.java.version=@java.version@
- @정보@를 통해 프로젝트에 빌드 정보를 읽어올 수 있다.
- 또한, git이나, build-info에 대한 정보를 읽어올 수 있는데,
- 클래스패스에 git.properties, 클래스패스 META-INF폴더안에 build-info.properties가 있으면 스프링 부트가 자동으로 info 엔드포인트에 추가해준다.
plugins {
id "com.gorylenko.gradle-git-properties" version "2.4.2"
}
- 위 플러그인을 등록해주자 다음으로 git.properties를 작성해보자 (임의로 작성했다)
git.branch = main
git.build.host = DESKTOP-VBHX979
git.build.time = 2021-03-27T19:30:23+0530
git.build.user.email = abc@gmail.com
git.build.user.name = abc
git.build.version = 0.0.1-SNAPSHOT
- application.properties에 다음을 등록하고 info를 호출하면
management.info.git.mode=full
- git과 관련된 정보를 읽어올 수 있다. 더 자세한 사항은 아래 두 블로그를 참고하자\
- https://github.com/n0mer/gradle-git-properties https://kdev.ing/spring-boot-build-info/
- https://github.com/n0mer/gradle-git-properties
2. 커스텀 InfoContributor
- 애플리케이션의 상세정보를 위 클래스를 구현해서 추가해보도록 하자 !
- 아래와 같은 CourseService와 Course엔티티가 있을 때
@Service
public class CourseService {
private CourseRepository courseRepository;
@Autowired
public CourseService(CourseRepository courseRepository) {
this.courseRepository = courseRepository;
}
public Iterable<Course> getAvailableCourses() {
return courseRepository.findAll();
}
}
- getAvaliableCourses()를 호출하면, 데이터베이스에서 관련 정보를 싹 긁어온다.
@Entity
@Table(name = "COURSES")
@Data
public class Course {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "CATEGORY")
private String category;
@Column(name = "RATING")
private int rating;
@Column(name = "DESCRIPTION")
private String description;
}
- Course엔터티는 위와 같은 정보로 이루어져 있다.
- 이를 활용하여, InfoContributor을 작성하면, 아래와 같다
//@Component
public class CourseInfoContributor implements InfoContributor {
private CourseService courseService;
@Autowired
public CourseInfoContributor(CourseService courseService) {
this.courseService = courseService;
}
@Override
public void contribute(Info.Builder builder) {
List<CourseNameRating> courseNameRatingList = new ArrayList<>();
for(Course course : courseService.getAvailableCourses()) {
courseNameRatingList.add(CourseNameRating.builder().name(course.getName()).rating(course.getRating()).build());
}
builder.withDetail("courses", courseNameRatingList);
}
@Builder
@Data
private static class CourseNameRating {
String name;
int rating;
}
}
- public void contribute(Info.Builder builder)메서드를 오버라이드하면 된다.
- courseService로 DB조회해서 값 가져온 다음에, CourseNameRating 클래스로 적절히 맵핑한 뒤
- courseNameRatingList에 담고, 이를 Info.Builder에 추가한 것 뿐이다.
- 중요한 점은 간단하게, API 호출 정보를 담아서 보여줄 수 있다는 점이다.
* 굳이 저런 정보를 보여줄 필요는 없다!! 그냥 다양한 정보를 보여주고 싶을 때 InfoContributor를 사용하면 된다는 것!