可以在zuul中使用hystrix电路制动器吗?

时间:2016-12-07 19:31:17

标签: java netflix-zuul hystrix

我知道使用application.yml我可以修改调用微服务的url,但我怀疑的是如何用hystrix电路制动器实现zuul?我有一个扩展ZuulFilter的类,并且在我的run方法中我可以使用它。我试图像这样执行hystrixCommand:

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(HystrixCommandGroupKey.Factory.asKey(request.getRequestURL().toString())) {
        @Override
        protected String run() throws Exception {
            RestTemplate restTemplate = new RestTemplate();
            String responseBody = restTemplate.getForObject(request.getRequestURL().toString(), String.class); 
            return responseBody;
        }

        @Override
        protected String getFallback() {
            return "No response from server";
        }
    };

    String response = hystrixCommand.execute();

    RequestContext.getCurrentContext().setResponseBody(response);
    return null;
}

但是如果实际的URL失败了怎么能告诉hystrixCommand使用getFallback方法?我想调用相同的URL,但我想如果我这样做它会做一个无限循环或者我不理解?

提前致谢。

更新

这是我的整个过滤器类

package com.filter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import javax.servlet.http.HttpServletRequest;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.web.client.RestTemplate;

public class ZuulHttpFilter extends ZuulFilter{

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 10000;
    }

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

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(HystrixCommandGroupKey.Factory.asKey(request.getRequestURL().toString())) {
            @Override
            protected String run() throws Exception {
                  RestTemplate restTemplate = new RestTemplate();
                  String responseBody = restTemplate.getForObject(request.getRequestURL().toString(), String.class); 
                  return responseBody;
            }

            @Override
            protected String getFallback() {
                return "No response from server";
            }
        };

        String response = hystrixCommand.execute();

        RequestContext.getCurrentContext().setResponseBody(response);
        return null;
    }
    }

1 个答案:

答案 0 :(得分:0)

您看到this question了吗?实际上,Hystrix javadoc表示它应该自动执行回退:

  

返回:R run()执行结果或getFallback()回退结果   如果命令因任何原因失败。