반응형
본 내용은 인프런의 이도원 님의 강의 "Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)" 내용을 바탕으로 정리한 내용입니다.
TurbineServer란?
- 넷플릭스의 오픈 소스 라이브러리 중 하나로, 마이크로서비스 아키텍처에서 각 서비스의 Hystrix 스트림을 집계하고 모니터링하는데 사용된다.
- 마이크로서비스 시스템에서 장애 격리와 복구를 위한 회로 차단기 패턴을 구현한 라이브러리로 각 서비스는 자체적인 Hystrix 스트림을 생성하며, 이러한 개별 스트림을 하나의 집합 스트림으로 통합하여 대시보드에서 실시간으로 모니터링할 수 있게 도와준다.
주요 기능
- 스트림 집계: 여러 마이크로서비스에서 발생하는 Hystrix 스트림을 하나의 스트림으로 통합
- 실시간 모니터링: 통합된 스트림을 사용하여 전체 시스템의 상태를 실시간으로 모니터링
- 확장성: 마이크로서비스 아키텍처의 특성상 서비스의 수가 많아질 수 있으며, TurbineServer는 이러한 상황에서 효율적으로 작동할 수 있도록 설계되어 있음
Micrometer란?
- Java 애플리케이션의 성능 모니터링을 위한 라이브러리로, 다양한 모니터링 시스템과 통합하여 메트릭을 수집하고 보고하는 데 사용된다.
- Spring Boot 2.x부터 기본으로 통합되어 있으며, 애플리케이션의 성능 지표를 쉽게 수집하고 시각화할 수 있다.
주요 기능
- 성능 메트릭 수집: 애플리케이션의 실행 시간, 메서드 호출 수, 메모리 사용량 등
- 다양한 모니터링 도구 통합: Prometheus, Datadog, New Relic 등과의 통합
- Spring Boot 통합: Spring Boot 2.x 이상에 기본적으로 포함됨
Timer
- Micrometer에서 제공하는 메트릭 유형 중 하나로, 코드 블록의 실행 시간을 측정하고 집계하는 데 사용된다.
- Timer를 사용하면 애플리케이션의 특정 작업이나 메서드의 성능을 모니터링할 수 있는데 주로 응답 시간, 처리 시간 등의 성능 지표를 수집하는 데 활용된다.
주요 기능
- 응답 시간 측정: 메서드의 처리 시간을 측정하고 기록
- 성능 지표 수집: 사용자 요청의 응답 시간, 메서드 호출 시간, 메서드 호출 횟수 등
- 간단한 통합: @Timed 어노테이션을 추가하기만 하면 자동 수집 가능
Micrometer 실습
UserService
pom.xml
<!-- micrometer -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
- micrometer dpendency 추가
application.yml
management:
endpoints:
web:
exposure:
include: refresh,health,beans,busrefresh, info, prometheus, metrics
- metrics, prometheus, health 엔드포인트 노출: /actuator/metrics, /actuator/prometheus 경로에서 메트릭 데이터를 확인할 수 있음.
UserController
/**
* 프로퍼티 정보 확인
* @return
*/
@GetMapping("/health_check")
@Timed(value="user.status", longTask = true)
public String status(){
// 랜덤으로 설정된 서버 포트를 알려준다.
return String.format("It's Working in User Service"
+ ", port(local.sever.port)=" + env.getProperty("local.server.port")
+ ", port(sever.port)=" + env.getProperty("server.port")
+ ", with token secret " + env.getProperty("token.secret")
+ ", with token time " + env.getProperty("token.expiration_time"));
}
/**
* http://localhost:랜덤포트/welcome 호출시 프로퍼티 greeting.message 값이 출력된다.
* @return
*/
@GetMapping("/welcome")
@Timed(value="user.welcome", longTask = true)
public String welcome(){
return greeting.getMessage();
}
- @Timed 어노테이션: user.status, user.welcome라는 이름으로 메트릭을 수집.
- 측정 대상: /health_check, /welcome 메서드의 실행 시간 및 호출 횟수.
GatewayServer
pom.xml
<!-- micrometer -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
- micrometer dpendency 추가
application.yml
management:
endpoints:
web:
exposure:
include: refresh,health,beans,busrefresh, info, prometheus, metrics
- endpoints에 프로퍼티 값 추가
OrderService
pom.xml
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- micrometer -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
- actuator, micrometer dpendency 추가
application.yml
management:
endpoints:
web:
exposure:
include: health, httptrace, info, prometheus, metrics
- endpoints 프로퍼티 값 추가
결과 확인
1. Postman 테스트
- Postman에서 http://127.0.0.1:8080/userService/health_check, http://127.0.0.1:8080/userService/welcome 두 URL 을 호출
- http://localhost:8761/ 접속 후 UserService의 랜덤 포트 확인
- http://127.0.0.1:${랜덤포트}/actuator/metrics URL 호출
"user.status",
"user.welcome",
- UserController에 설정한 user.status, user.welcome 메트릭 정보가 출력되는 것을 확인할 수 있다.
- http://127.0.0.1:${랜덤포트}/actuator/prometheus Prometheus 텍스트 형식으로 Metric이 제공되는 것을 확인할 수 있다.
반응형
'Cloud > MSA' 카테고리의 다른 글
[MSA] Prometheus와 Grafana (0) | 2024.12.07 |
---|---|
[MSA] MSA 분산 추적 (0) | 2024.12.02 |
[MSA] MSA 통신 간 오류 처리 (0) | 2024.12.02 |
[MSA] Kafka Connect (0) | 2024.12.02 |
[MSA] Apache Kafka (0) | 2024.12.02 |