728x90
1.Spring Boot Actuator
애플리케이션의 운영 및 모니터링을 위한 기능을 제공하는 라이브러리다.
엔드포인트를 통해 확인이 가능하다.
1-1.의존성추가
Actuator 사용을 위해 pom.xml에 의존성을 추가해줬다.
<!-- Spring Boot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1-2. 엔드포인트 접근 권한설정
actuator엔드포인트를 테스트할 목적이므로 우선 인증권한 없이 접근 가능하게 SecurityConfig에서 설정한다.
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.authorizeHttpRequests(auth -> auth
.requestMatchers(
"/actuator/**", //이건 임시임
....
"/test/public")
.permitAll()
1-3.application.properties 설정
Spring Boot Actuator의 모든 엔드포인트를 활성화하고, Prometheus 메트릭스를 사용하도록 설정한다.
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
1-4. 엔드포인트 테스트
애플리케이션의 상태를 표시하는 health 엔드포인트가 정상 접근가능한것을 확인했고
Prometheus를 위한 엔드포인트 역시 정상적으로 접근 가능한것을 확인하였다.
이제 Prometheus를 설정해보자
2.Prometheus
오픈 소스 모니터링 및 경보시스템으로, 시계열 데이터를 수집 및 저장하는 데 특화된 도구다.
2-1.docker에 추가
기존 redis만 있던 docker-compose.yml 파일에 Prometheus를 위한 설정을 작성한다.
로컬에 prometheus.yml 로 설정파일을 만들어서 컨테이너 내부로 마운트하게 설정했다.
version: "3.8"
services:
redis:
image: redis:7.2
container_name: redis
restart: always
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: ["redis-server", "--appendonly", "yes"]
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: always
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- "--config.file=/etc/prometheus/prometheus.yml"
volumes:
redis_data:
driver: local
2-2.prometheus.yml
15초마다 로컬에서 실행 중인 Spring Boot 애플리케이션의 /actuator/prometheus엔드포인트에 접근해서
데이터를 저장하는 설정을 만들었다.
global:
scrape_interval: 15s # 15초마다 데이터 수집
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['host.docker.internal:8080'] # 로컬에서 실행 중인 Spring Boot 접근
2-3. 확인해보기
http://localhost:9090/ 에 접속하여 Prometheus가 정상적으로 실행됬는지 확인한다.
Status의 Runtime Information에서 확인해보니 정상적으로 작동하고 있는것을 확인하였다.
한번 Graph 쪽에도 접근해봐서 jvm_memory_used_bytes를 테스트겸 확인하였다.
이제 grafana을 추가할 차례다.
3.grafana
오픈 소스 데이터 시각화 및 모니터링 플랫폼으로, 다양한 데이터 소스에서 수집한 데이터를 시각적으로 표현하는 데 특화된 도구다.
3-1.Docker 설정 추가
아까 prometheus를 추가한것처럼 grafana 도 docker 설정에 추가해준다.
grafana는 prometheus가 실행된 이후로 실행되고 관리자 비밀번호는 테스트이므로 admin으로 설정해둔다.
version: "3.8"
services:
redis:
image: redis:7.2
container_name: redis
restart: always
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: ["redis-server", "--appendonly", "yes"]
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: always
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- "--config.file=/etc/prometheus/prometheus.yml"
grafana:
image: grafana/grafana:latest
container_name: grafana
restart: always
ports:
- "3000:3000" # Grafana 기본 포트
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin # Grafana 기본 관리자 비밀번호 설정
depends_on:
- prometheus # Grafana가 Prometheus 서비스가 시작된 후 실행
volumes:
redis_data:
driver: local
3-2. Grafana 사용
http://localhost:3000/ 로 접근하여 grafana에 접근하고 docker 에 설정한대로 admin으로 로그인하자
이제 prometheus와 연결하기 위해 좌측 메뉴에서 Add new connection을 누르자
검색창에 prometheus를 검색한뒤 prometheus를 선택한다.
우상단의 Add new data source를 고른다.
나의 경우 docker 환경에서 실행되고있기 때문에 http://prometheus:9090 으로 url 을 입력하여 연결하였다.
이제 연결이 완료되었으면 좌측 메뉴에서 Dashboards를 누르고 우상단의 New -> import 를 누른다.
이제 여기에 마음에 드는 데시보드 번호를 입력하면 되는데 나는 다음과같은 번호를 입력했고 다른 버전을 찾는사람은 아래 url에서 원하는 데시보드를 찾자
https://grafana.com/grafana/dashboards
Grafana dashboards | Grafana Labs
No results found. Please clear one or more filters.
grafana.com
대시보드를 입력하고 아래에서 prometheus 데이터를 연결해준다.
이제 새로 생겨서 prometheus와 연결된 grafana의 데시보드를 시각화로 확인할수있었다.
728x90
'BackEnd > SpringBoot' 카테고리의 다른 글
[SpringBoot] WebSocket 사용하기 (0) | 2025.04.05 |
---|---|
[SpringBoot] log aspect, API 표준 Response 제작 (0) | 2025.03.23 |
[SpringBoot] Swagger 인증 유지 설정 (0) | 2025.03.23 |