2017년 7월 2일 일요일

spring cloud slueth setting

You can trace the request in micro soft architecture by using spring cloud slueth.
By that dependency setting, slueth issue the traceId, spanId per request,
and you can trace the request by that.

All the flow, the request from the user has the same traceId per the request,
and the same spandId per the module.

Just adding spring-cloud-starter-sleuth library to your project, traceId and spanId are issued to the request.

In this example, we are using the interceptor, and then all the request would have traceId and spanId.

package org.blog.test.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class TraceInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private Tracer tracer;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("[{}] {} spanId : [{}], traceId : [{}]", request.getMethod(), request.getRequestURI(), tracer.getCurrentSpan().getTraceId(), tracer.getCurrentSpan().getSpanId());
        return true;
    }
}

By the interceptor, when the request is called, the log is added as below.

2017-06-30 00:20:11.745  INFO [slueth-application,5c330db5e121700a,5c330db5e121700a,false] 11200 --- [nio-8080-exec-1] org.blog.test.config.TraceInterceptor    : [GET] /test/call spanId : [6643668950118920202], traceId : [6643668950118920202]
2017-06-30 00:20:11.835  INFO [slueth-application,5c330db5e121700a,18462ed55b34706c,false] 11200 --- [nio-8080-exec-2] org.blog.test.config.TraceInterceptor    : [GET] /test spanId : [6643668950118920202], traceId : [1749136999173091436]

All the example can be downloaded as below link.

https://gitlab.com/shashaka/slueth-project

댓글 없음 :

댓글 쓰기