#35 springboot: custom metric - timer with aop
This commit is contained in:
@@ -4,6 +4,7 @@ import hello.order.v0.OrderConfigV0;
|
||||
import hello.order.v1.OrderConfigV1;
|
||||
import hello.order.v2.OrderConfigV2;
|
||||
import hello.order.v3.OrderConfigV3;
|
||||
import hello.order.v4.OrderConfigV4;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -13,7 +14,8 @@ import org.springframework.context.annotation.Import;
|
||||
//@Import(OrderConfigV0.class)
|
||||
//@Import(OrderConfigV1.class)
|
||||
//@Import(OrderConfigV2.class)
|
||||
@Import(OrderConfigV3.class)
|
||||
//@Import(OrderConfigV3.class)
|
||||
@Import(OrderConfigV4.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.controller")
|
||||
public class ActuatorApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package hello.order.v4;
|
||||
|
||||
import hello.order.OrderService;
|
||||
import io.micrometer.core.aop.TimedAspect;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class OrderConfigV4 {
|
||||
|
||||
@Bean
|
||||
public OrderService orderService() {
|
||||
return new OrderServiceV4();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TimedAspect timedAspect(MeterRegistry registry) {
|
||||
return new TimedAspect(registry);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package hello.order.v4;
|
||||
|
||||
import hello.order.OrderService;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Timed(value = "my.order")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class OrderServiceV4 implements OrderService {
|
||||
private final AtomicInteger stock = new AtomicInteger(100);
|
||||
|
||||
@Override
|
||||
public void order() {
|
||||
log.info("주문");
|
||||
stock.decrementAndGet();
|
||||
sleep(500);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
log.info("취소");
|
||||
stock.incrementAndGet();
|
||||
sleep(200);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AtomicInteger getStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
private void sleep(int l) {
|
||||
try {
|
||||
Thread.sleep(l + new Random().nextInt(200));
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user