반응형

환경: springboot2.7.6, spring cloud2021.0.8, java17

 

zipkin

  • 분산 환경의 데이터 수집, 추적 시스템(오픈소스, 트위터 시작, google drapper에서 발전)
  • 분산 환경에서의 시스템 병목 현상 파악
  • collector, query service, database, webui 로 구성
  • span : 하나의 요청에 사용되는 작업 단위; 64bit unique ID(in msa component)
  • trace: 트리 구조로 이뤄진 span set; 하나의 요청에 같은 trace ID 발급(in total flow)
  • spring cloud sleuth; zipkin 서버와 연동, trace/span id를 로그에 추가 가능 

 

다운로드

 

로컬 터미널로 실행을 하고 http://127.0.0.1:9411/zipkin/ 들어가면 웹화면이 나온다.

 

서비스에서 사용하기(양쪽 모두에 세팅)

1. dependency 추가

<!-- zipkin -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

2. application.yml 추가

spring:
  zipkin:
    base-url: http://localhost:9411
    enabled: true
  sleuth:
    sampler:
      probability: 1.0

3. 재시작하고 연동되어있는 api를 날려보면

첫번째 서버

2024-02-08 15:54:27.101  INFO [user-service,89bf1666da761e42,89bf1666da761e42] 27456 --- [o-auto-1-exec-5] c.e.userservice.service.UserServiceImpl  : before call orders msa
2024-02-08 15:54:27.210 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] ---> GET http://order-service/order-service/fcb59ec4-b2d6-4fd2-aefd-c1004542c801/orders HTTP/1.1
2024-02-08 15:54:27.211 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] ---> END HTTP (0-byte body)
2024-02-08 15:54:28.095 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] <--- HTTP/1.1 200 (883ms)
2024-02-08 15:54:28.095 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] connection: keep-alive
2024-02-08 15:54:28.095 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] content-type: application/json
2024-02-08 15:54:28.096 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] date: Thu, 08 Feb 2024 06:54:28 GMT
2024-02-08 15:54:28.096 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] keep-alive: timeout=60
2024-02-08 15:54:28.096 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] transfer-encoding: chunked
2024-02-08 15:54:28.096 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] 
2024-02-08 15:54:28.097 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] [{"productId":"Catalog3","qty":1,"unitPrice":2000,"totalPrice":2000,"createdAt":"2024-02-08T15:51:49","orderId":"17af8280-b028-42d6-b590-594578c75d4c"},{"productId":"Catalog3","qty":2,"unitPrice":2000,"totalPrice":4000,"createdAt":"2024-02-08T15:52:28","orderId":"765e3a28-c6ae-4e48-a4a3-dd2cc73d3caa"}]
2024-02-08 15:54:28.097 DEBUG [user-service,89bf1666da761e42,cc4d64c75759fa06] 27456 --- [pool-4-thread-1] c.e.u.client.OrderServiceClient          : [OrderServiceClient#getOrders] <--- END HTTP (303-byte body)
2024-02-08 15:54:28.138  INFO [user-service,89bf1666da761e42,89bf1666da761e42] 27456 --- [o-auto-1-exec-5] c.e.userservice.service.UserServiceImpl  : after call orders msa

user-service,89bf1666da761e42,89bf1666da761e42

user-service,89bf1666da761e42,cc4d64c75759fa06

  • traceId: 89bf1666da761e42
  • spanId: cc4d64c75759fa06
  • 처음엔 tId랑 같은걸로 시작했다가 새로운 커낵션이 맺어지면 새로운 spanId를 딴다.

 

두번째 서버

2024-02-08 15:54:27.567  INFO [order-service,89bf1666da761e42,730648c3fe5dad8a] 4968 --- [o-auto-1-exec-4] c.e.o.controller.OrderController         : before get orders
Hibernate: select order0_.id as id1_0_, order0_.created_at as created_2_0_, order0_.order_id as order_id3_0_, order0_.product_id as product_4_0_, order0_.qty as qty5_0_, order0_.total_price as total_pr6_0_, order0_.unit_price as unit_pri7_0_, order0_.user_id as user_id8_0_ from orders order0_ where order0_.user_id=?
2024-02-08 15:54:28.069  INFO [order-service,89bf1666da761e42,730648c3fe5dad8a] 4968 --- [o-auto-1-exec-4] c.e.o.controller.OrderController         : after call orders msa

order-service,89bf1666da761e42,730648c3fe5dad8a

  • traceId: 89bf1666da761e42
  • spanId: 730648c3fe5dad8a

4. 해당 값을 가지로 웹으로 들어가서 검색

http://localhost:9411/zipkin/traces/89bf1666da761e42

msa component, 시간 등등 해당 요청으로 연결된 모든 정보를 알 수 있다.

어떤 서비스에서 에러가 발생하면 아래처럼 에러를 표시해준다.

728x90
반응형

+ Recent posts