티스토리 뷰

Dev/Spring

Spring Boot III -Actuator

마이스토리 2016. 6. 2. 15:06

> 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설정 등 확인 가능


> 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
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31