Java/Spring Boot

[MSA] Spring Cloud로 MSA를 개발해보자 13편 [모니터링]

누리는 귀여워 2024. 9. 12. 01:44

1. 설명

 

1). Micrometer

- JVM 기반의 애플리케이션의 Metrics 제공

- Prometheus 등의 다양한 모니터링 시스템 지원

 

2). Prometheus

- Metrics를 수집하고 모니터링 및 알람에 사용되는 오픈소스

- 16년부터 CNCF에서 관리되는 2번째 공식 프로젝트 [1번째는 k8s]

  - Level DB -> Time Series Database(TSDB)

- pull 방식의 구조와 다양한 Metric Exporter 제공

- 시계열 DB Metrics 저장 -> 조회 가능(Query)

 

3). Grafana

- 데이터 시각화, 모니터링 및 분석을 위한 오픈소스

- 시계열 데이터를 시각화하기 위한 대시보다 제공

 

 

2. Micrometer 적용

1). 코드

user-service, apigate-service, order-service

application.yml

management:
  endpoints:
    web:
      exposure:
        include: info, metrics, prometheus

 

UserServiceController.java

// 해당 어노테이션이 붙은 메소드가 호출될 때마다 타이머가 실행되고, 
// 그 결과는 Prometheus, Grafana 등에 기록된다.

@GetMapping("/health_check")
    @Timed(value = "users.status", longTask = true)
    public String status() {
        return String.format("It's Working in User Service"
                + ", port(local.server.port)=" + env.getProperty("local.server.port")
                + ", port(server.port)=" + env.getProperty("server.port")
                + ", token secret=" + env.getProperty("token.secret")
                + ", token expiration time=" + env.getProperty("token.expiration_time"));
    }

    @GetMapping("/welcome")
    @Timed(value = "users.welcome", longTask = true)
    public String welcome() {
        return greeting.getMessage();
    }

 

 

2). metrics 테스트

 

 

 

 

3. Prometheus 설치

https://prometheus.io/download/

 

Download | Prometheus

An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.

prometheus.io

 

 

4. Grafana 설치

https://grafana.com/get/

 

Grafana get started | Cloud, Self-managed, Enterprise

Thank you! Your message has been received!

grafana.com

 

5. 설정 수정 및 실행

1). 설정 수정

prometheus.yml

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: 'user-service'
    scrape_interval: 15s
    metrics_path: '/user-service/actuator/prometheus'
    static_configs:
    - targets: ["localhost:8000"]
  - job_name: 'apigateway-service'
    scrape_interval: 15s
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ["localhost:8000"]
  - job_name: 'order-service'
    scrape_interval: 15s
    metrics_path: '/order-service/actuator/prometheus'
    static_configs:
    - targets: ["localhost:8000"]

 

2). 접속 테스트

(1). prometheus 불러오기

 

(2). prometheus 실행

prometheus.exe 실행

localhost:9090 접속

 

 

 

(3). Grafana 실행

bin/grafana-server.exe 실행

localhost:3030 접속 [id, pwd : admin]

 

 

6. Grafana - Prometheus 연동

 

- Grafana Dashboard 추가 목록

 - JVM(Micrometer)

 - Prometheus

 - Spring Cloud Gateway

 

1). Connection - Prometheus 등록

 

prometheus URL 등록 후 하단 save

 

2). 대쉬보드 플러그인 추가

대쉬보드 ID = 3662, 11892, 11506

https://grafana.com/grafana/dashboards/

 

Grafana dashboards | Grafana Labs

No results found. Please clear one or more filters.

grafana.com

 

대쉬보드 ID 복사

 

대쉬보드 ID 붙여넣기

 

 

 

3). 대쉬보드 데이터 설정 수정

 

 

sum은 prometheus에서 검색할 수 있는 값이어야 한다.

job은 prometheus.yml에서 설정한 job의 이름이다