如何使用Zuul覆盖HTTP请求

时间:2019-05-08 10:20:44

标签: java spring caching netflix-zuul

我正在设置Zuul网关。如何避免Zuul对端点进行HTTP调用?相反,我将从缓存中检索值(如果可用),否则我将进行HTTP调用以获取响应。

如果它们在缓存中可用,我将检索这些值,否则我将进行HTTP调用以获取这些值。在Zuul“路由”过滤器中实现了此逻辑。

// This will always make the HTTP request
if(CacheManager.cachedValues.containsKey(key)) {
    String body = "hey";
    context.setResponseBody(body);
}

@Override

    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        String method = request.getMethod();
        String uri = request.getRequestURL().toString();

        log.info("log from the route filter");
        log.info(String.format("%s request to %s", method, uri));

        String key = uri.substring(uri.lastIndexOf('/') + 1);

        //retrieve values from cache if they are available
        if(CacheManager.cachedValues.containsKey(key)) {
            String body = "hey";
            context.setResponseBody(body);
        }else {
            //call the http endpoint  
        }
        return null;
    }

    if(available in the cache ) {
       //get the values and return them
    } else {
       //call the http end point and get the values
       //cache the values
    }

0 个答案:

没有答案