测量发送请求到 zuul 代理的时间

Measure the time in which a request is sent to zuul proxy

我在 spring 引导中创建了多个微服务,它与 zuul 通信 proxy.My 问题是请求花费了太多时间,我想测量从请求开始所花费的时间到达代理以及请求从代理转发到我的 microservices.Are 的时间有什么方法可以做到这一点? 谢谢!

您可以尝试写的内容 here 并添加自定义 RequestInterceptor:

@Component
public class CustomRequestInterceptor extends HandlerInterceptorAdapter {

 private static final Logger logger = LoggerFactory.getLogger(CustomRequestInterceptor.class);

 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

  long startTime = Instant.now().toEpochMilli();
  logger.info("Request URL::" + request.getRequestURL().toString() +
   ":: Start Time=" + Instant.now());
  request.setAttribute("startTime", startTime);
  return true;
 }

 @Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

  long startTime = (Long) request.getAttribute("startTime");

  logger.info("Request URL::" + request.getRequestURL().toString() +
   ":: Time Taken=" + (Instant.now().toEpochMilli() - startTime));
 }
}

要在日志中得到这样的东西:

2018-09-30 12:02:09.704  INFO 51707 --- [nio-8080-exec-2] com.example.CustomRequestInterceptor     : Request URL::http://localhost:8080/log-incoming-request:: Start Time=2018-09-30T06:32:08.861Z
2018-09-30 12:02:16.820  INFO 51707 --- [nio-8080-exec-2] com.example.CustomRequestInterceptor     : Request URL::http://localhost:8080/log-incoming-request:: Time Taken=9942

配置中:

@Configuration
public class RequestAppConfig implements WebMvcConfigurer {

 @Autowired
 private CustomRequestInterceptor customRequestInterceptor;

 @Override
 public void addInterceptors(InterceptorRegistry registry) {
  registry.addInterceptor(customRequestInterceptor)
   .addPathPatterns("/**/path_to_zull_request/**/");
 }
}

其他类似于的方法可以用ZuulFilter完成。 下面的代码是在没有检查的情况下编写的,因此可能会有一些错误,但它应该能说明问题。 首先添加前置过滤器

public class PreFilter extends ZuulFilter {

    private static final Logger logger = LoggerFactory.getLogger(PreFilter.class);

    @Override
    public int filterOrder() {
        return PRE_DECORATION_FILTER_ORDER - 1; // run before PreDecoration
    }

    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        long startTime = Instant.now().toEpochMilli();
        logger.info("Request URL::" + request.getRequestURL().toString() +
        ":: Start Time=" + Instant.now());
        ctx.put("startTime", startTime);
        return null;
    }
}

然后在Post过滤器

public class PostFilter extends ZuulFilter {

    private static final Logger logger = LoggerFactory.getLogger(PostFilter.class);
    @Override
    public String filterType() {
        return POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return SEND_RESPONSE_FILTER_ORDER - 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        long startTime = (Long) ctx.get("startTime");

        logger.info("Request URL::" + request.getRequestURL().toString() +
   ":: Time Taken=" + (Instant.now().toEpochMilli() - startTime));
        return null;
    }
}