0

I know that with application.yml I can modify the url that call a microservice but my doubt is how can I implement zuul with hystrix circuit braker?, I have a class that extends ZuulFilter and in my run method I'm trying to execute the hystrixCommand like this:

@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;
}

But how can I tell hystrixCommand to use the getFallback method if the actual URL failed?, I thought to call the same URL but I think if I do that it will do an infinite cycle or am I not understanding?

Thanks in advance.

UPDATE

This is my whole filter class

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;
    }
    }
Alan Gaytan
  • 852
  • 4
  • 14
  • 33

1 Answers1

0

Did you see this question? In fact, the Hystrix javadoc says that it is supposed to execute the fallback automatically:

Returns: R Result of run() execution or a fallback from getFallback() if the command fails for any reason.

Community
  • 1
  • 1
Igor Deruga
  • 1,504
  • 1
  • 10
  • 18
  • Yes I have, in fact my example is like the one in the link, but when It executes the run method inside the hystrixCommand it freeze, let me update question to see the whole class – Alan Gaytan Dec 07 '16 at 19:59
  • Can you try using the HystrixCommand constructor with the timeout parameter? HystrixCommand(HystrixCommandGroupKey group, int executionIsolationThreadTimeoutInMilliseconds) – Igor Deruga Dec 08 '16 at 13:54
  • it is not working :(, I changed my code to use a method that calls my url and in that method add the hystrixCommand, I don't know if is the right thing to do but for me is working – Alan Gaytan Dec 09 '16 at 14:21