모니터링 툴
이전의 글 + 강의를 참고하여 MSA 애플리케이션에 모니터링 툴을 적용해보자.
Micrometer 추가하기
API Gatway, user-service, order-serivce 세 마이크로 서비스에
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
Java
복사
위와 같이 actuator와 micrometer의 prometheus에 대한 라이브러리 의존성을 추가해주고,
management:
endpoints:
web:
exposure:
include: refresh, health, info, metrics, beans, busrefresh, prometheus
YAML
복사
이처럼 application.yml 파일에 노출 시킬 endpoint로 metrics, prometheus를 추가하자.
그 후 해당 주소로 들어가 metrics 정보를 살펴보면,
이와 같이 actuator가 수집한 정보들을 지표 형태로 볼 수 있다.
prometheus에도 제공되는 데이터를 확인할 수 있게 된다.
@GetMapping("/health-check")
@Timed(value = "users.status", longTask = true)
public String status(HttpServletRequest request) {
return "It's working in User-service On Port " + request.getServerPort()
+ ", token secret = " + environment.getProperty("token.secret")
+ ", token expiration time = " + environment.getProperty("token.expiration_time");
}
@GetMapping("/welcome")
@Timed(value = "users.welcome", longTask = true)
public String welcome() {
return userServiceProperties.getMessage();
}
Java
복사
만약 부가적인 지표를 수집하고 싶다면, Controller에 수집하고 싶은 메서드 위에 @Timed 애노테이션을 추가하면 된다.
Prometheus 추가하기
http://prometheus.io/download에서 프로메테우스 다운로드 후 압축을 풀면,
이와 같이 설정 파일과 실행 파일이 들어있다.
- job_name: 'user-service'
scrape_interval: 15s
metrics_path: '/user-servcie/actuator/prometheus'
static_configs:
- targets: ['localhost:8000']
- job_name: 'order-service'
scrape_interval: 15s
metrics_path: '/order-servcie/actuator/prometheus'
static_configs:
- targets: ['localhost:8000']
- job_name: 'apigateway-service'
scrape_interval: 15s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8000']
YAML
복사
먼저 prometheus.yml 파일을 위와 같이 수정하여 API Gateway, order-serivce, user-service의 actuator에 대한 메트릭 수집 설정을 추가해준다.
이후 서버를 실행시키고 9090 포트에 접속하면,
이와 같이 수집한 메트릭 정보를 시간에 따라 수치와 그래프로 확인할 수 있다.
개발자 확인 문제
만약 이와 같은 에러가 발생한다면, 설정-개인정보 보호 및 보안에 들어가서 차단된 파일을 그래도 실행 버튼을 눌러 열어주면 된다.
Grafana 추가하기
http://grafana.com/grafana/download에서 그라파나 다운로드하고 압축 파일을 풀고나면,
역시 마찬가지로 설정 파일과 실행 파일들이 들어있다.
./bin/grafana-server
Shell
복사
위 명령어를 통해 서버를 실행시키고,
서버가 실행된 상태에서 3000번 포트에 접속해보자.
로그인 화면이 뜨는데, 기본적으로 따로 변경하지 않았다면 ID/PW가 admin - admin으로 되어있다.
접속하고 나면 이와 같이 대시보드를 꾸밀 수 있도록 되어있다.
http://grafana.com/grafana/dashboards에 들어가면 다른 사람들이 미리 만들어둔 대시보드를 가져다가 사용할 수 있다.
이와 같이 구성하고자 하는 대시보드를 검색하여,
해당 대시보드의 ID를 복사하여
우리의 Grafana 서버에서 Import로 넣어주면 된다.
만약 사용한 대시보드의 일부 값이 불러와지지 않거나 잘 맞지 않는다고 생각되면
값을 직접 수정하여 사용하는 것도 가능하다.