티스토리 뷰
> Production Monitoring and Management with Spring Boot Actuator
> pom.xml 에 dependency 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
> application 실행 다음 URL들로 spring 환경설정, context설정 등 확인 가능
http://localhost:8080/autoconfig
http://localhost:8080/configprops
http://localhost:8080/mappings
http://localhost:8080/info : applicaton정보. default 빈값. propretis 파일에 변수 추가해야됨
http://localhost:8080/metrics : 실시간 서버상태(메모리, gc 등등)
http://localhost:8080/shutdown : application shutdown 시킴 (POST, default: disabled).
> auctuator 환경설정. application.properties 파일에 추가
# Actuator Configuration
# /health endpoints 바꿈
endpoints.health.id=status
endpoints.health.sensitive=false
# /shutdown endpoints 활성화
endpoints.shutdown.enabled=true
endpoints.shutdown.sensitive=false
# autuator endpooinse 기본 path 지정
management.context-path=/actuators
> custom status 추가
> HealthIndicatro class 생성
@Component
public class GreetingHealthIndicator implements HealthIndicator {
@Autowired
GreetingService greetingService;
@Override
public Health health() {
Collection<Greeting> greetings = greetingService.findAll();
if (greetings == null || greetings.size() == 0){
return Health.down().withDetail("count", 0).build();
}
return Health.up().withDetail("count", greetings.size()).build();
}
}
> http://localhost:8080/status 실행하면 추가된 항목표시됨
{
"status": "UP",
"greeting": {
"status": "UP",
"count": 2
},
"diskSpace": {
"status": "UP",
"total": 100705234944,
"free": 20383506432,
"threshold": 10485760
},
"db": {
"status": "UP",
"database": "HSQL Database Engine",
"hello": 1
}
}
> custom info 추가
> application properties 다음 항목 추가
# /info endpoint 결과에 보일 내용.
info.app.name=Web Services
info.app.description=A RESTful web services project using Spring Boot.
info.build.group=@project.groupId@
info.build.artifact=@project.artifactId@
info.build.version=@project.version@
> http://localhost:8080/actuators/info 결과
{
"app": {
"description": "A RESTful web services project using Spring Boot.",
"name": "Web Services"
},
"build": {
"group": "lyj",
"artifact": "sample-springBoot",
"version": "0.0.1-SNAPSHOT"
}
}
> count metrics customizing
> count metrics를 추가하고자 하는 bean에 아래와 같이 추가
import org.springframework.boot.actuate.metrics.CounterService;
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class GreetingService {
@Autowired
CounterService counterService;
@Autowired
GreetingRepository greetingRepository;
public Collection<Greeting> findAll(){
counterService.increment("method.invoked.greetingServiceBean.findAll");
Collection<Greeting> greetings = greetingRepository.findAll();
return greetings;
}
> http://localhost:8080/actuators/metrics endpoint 호출결과
"counter.status.200.api.greetings": 1,
"counter.status.200.actuators.metrics": 2,
"counter.method.invoked.greetingServiceBean.findOne": 1,
"counter.method.invoked.greetingServiceBean.findAll": 1,
"counter.status.200.api.greetings.id": 1
- Total
- Today
- Yesterday