상태 점검 및 메트릭
상태 엔드포인트를 제공하고 애플리케이션 메트릭을 수집하도록 Spring Boot Actuator를 구현합니다.
상태 점검 및 메트릭은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 3개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Spring Boot 4 Microservices & REST APIs 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 3개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What is Spring Boot Actuator?
When running microservices, monitoring their health and performance is super important. Spring Boot Actuator provides a set of production-ready features to help you do just that!
It exposes operational information about your running application, making it easier to observe and manage.
Add Actuator Dependency
To start using Actuator, you just need to add its dependency to your Spring Boot project. This is typically done in your pom.xml for Maven or build.gradle for Gradle.
Here's how to add it with Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Explore Default Endpoints
Once the Actuator dependency is added, Spring Boot automatically configures several useful endpoints. These endpoints provide valuable insights into your application's state.
/actuator/health: Shows the application's health status./actuator/info: Displays general application information./actuator/metrics: Provides detailed metrics data.
Checking Application Health
The /actuator/health endpoint is a fundamental part of monitoring. It tells you if your application and its integrated components (like databases or disk space) are functioning correctly.
A simple "UP" status means everything is good! Run this basic app and check its health endpoint.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActuatorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorDemoApplication.class, args);
}
}Accessing Health Endpoint
After running the previous application, open your browser and navigate to http://localhost:8080/actuator/health.
You should see a JSON response indicating the application's overall status, typically {"status":"UP"}.
This is a quick way to verify if your service is alive!
Create Custom Health Checks
You can extend Actuator's health checks to include your own custom logic. For example, checking the status of an external API, a message queue, or a specific business process.
To do this, you implement the HealthIndicator interface:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomServiceHealthIndicator implements HealthIndicator {
private final String SERVICE_NAME = "MyExternalService";
@Override
public Health health() {
if (isServiceUp()) {
return Health.up().withDetail(SERVICE_NAME, "Available").build();
}
return Health.down().withDetail(SERVICE_NAME, "Not Available").build();
}
private boolean isServiceUp() {
// Simulate checking an external service
return Math.random() > 0.3; // 70% chance of being up
}
}Understanding Application Metrics
Metrics are numerical data points that describe your application's behavior over time. They are crucial for understanding performance, resource usage, and identifying potential bottlenecks.
Spring Boot Actuator, powered by Micrometer, automatically collects various metrics:
- CPU and memory usage
- HTTP request counts and latencies
- Database connection pool statistics
Querying Metrics Data
The /actuator/metrics endpoint provides a list of all available metrics. You can then query specific metrics for detailed information.
- Visit
http://localhost:8080/actuator/metricsto see available metrics. - Then, try
http://localhost:8080/actuator/metrics/jvm.memory.usedto see the specific memory usage metric.
This allows you to gather precise data for monitoring dashboards.
Implement Custom Metrics
Beyond the default metrics, you can track application-specific events using custom metrics. Micrometer provides simple APIs for different metric types like Counters, Gauges, and Timers.
Here's an example of a custom Counter:
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final Counter customCounter;
public MyService(MeterRegistry meterRegistry) {
this.customCounter = Counter.builder("my_app.processed.items")
.description("Number of items processed by MyService")
.register(meterRegistry);
}
public void processItem() {
System.out.println("Processing an item...");
customCounter.increment(); // Increment the counter
}
}Customize Endpoint Exposure
By default, only /health and /info are typically exposed over HTTP. You can configure which other endpoints Actuator makes available using application.properties or application.yml.
To expose more endpoints like metrics, beans, and env, add this line:
management.endpoints.web.exposure.include=health,info,metrics,beans,envActuator Knowledge Check
Let's quickly check your understanding of Spring Boot Actuator's benefits.
Lesson Recap
Great job! In this lesson, you learned about:
- Spring Boot Actuator for monitoring and managing microservices.
- Implementing health checks using the
/actuator/healthendpoint. - Creating custom health indicators for specific services.
- Understanding and collecting application metrics via
/actuator/metrics. - Implementing custom metrics with Micrometer.
- The importance of securing Actuator endpoints in production.
Next, we'll dive into centralized logging and dashboarding!
AI 튜터와 함께 Java을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 24
- 레슨
- 93
자주 묻는 질문
“상태 점검 및 메트릭” 강의는 무료인가요?
네 — “상태 점검 및 메트릭” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 3개의 강의가 포함되어 있습니다.
“상태 점검 및 메트릭”에서 뭘 배우나요?
상태 엔드포인트를 제공하고 애플리케이션 메트릭을 수집하도록 Spring Boot Actuator를 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 2번째 강의입니다.
“상태 점검 및 메트릭” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- ELK 스택을 사용한 중앙 집중식 로그 기록
- 상태 점검 및 메트릭
- 경고 및 대시보드 만들기