Java/Spring-msa

[스프링 msa] zipkin을 이용한 msa 분산추적

Mini_96 2025. 2. 23. 21:24
  • spring 3.0 이후로는 brave를 사용해야함

  • 의존성 추가
implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'io.github.openfeign:feign-micrometer'
  • 이걸해야 요청마다 동일한 TraceId 부여
@Configuration
public class MonitorConfig {
    @Bean
    public Capability capability(final MeterRegistry registry){
        return new MicrometerCapability(registry);
    }
}
management:
  endpoints:
    web:
      exposure:
        include: refresh, health, beans, busrefresh, info, metrics, prometheus
  endpoint:
    health:
      show-details: always
  tracing:
    sampling:
      probability: 1.0
    propagation:
      consume: b3
      produce: b3_multi
  zipkin:
    tracing:
      endpoint: "http://localhost:9411/api/v2/spans"
  logging:
    level:
      org.example.orderservice: DEBUG
    pattern:
      level: "%5p [%X{traceId:-},%X{spanId:-}]"
  • order-service, user-service가 같은 traceId를 사용 -> 한 요청임을 의미

  • zipkin 검색 결과

 

* 에러가 있는경우 test

  • 조회부분에서 강제로 예외 던져보기
@GetMapping("/{userId}/orders")
public ResponseEntity getOrder(@PathVariable("userId") String userId) throws Exception {
    log.info("Before retrieve orders data");
    Iterable<OrderEntity> orderList = orderService.getOrdersByUserId(userId);
    
    try{
        Thread.sleep(1000);
        throw new Exception("장애 발생");
    } catch (InterruptedException ex){
        log.warn(ex.getMessage());
    }
  • 에러 확인가능

  • 장애의 경우 붉은 점 표시