Search code examples
springspring-bootnetflix-zuulnetflixspring-cloud-netflix

Zuul Implementing Multiple ZuulFallbackProvider for multiple zuul routes


How can i implement multiple zuulFallbackProvider for multiple zuul routing. I cant see an answer how to do it using a properties only other than exposing a restcontroller and implement a method with a hystrixcommand.

Can i make each of my service with its own zuulFallBackProvider bean?

application.yml

    server:
      port: 8080

    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 20000

    ribbon:
      ReadTimeout: 20000
      ConnectTimeout: 20000

    zuul:
      prefix: /api
      ignoredServices: '*'
      host:
        connect-timeout-millis: 20000
        socket-timeout-millis: 20000
      routes:
        kicks-service:
          path: /kicks/**
          serviceId: kicks-service
          stripPrefix: false
          sensitiveHeaders:
        kicks-inventory:
          path: /inventory/**
          serviceId: kicks-inventory
          stripPrefix: false
          sensitiveHeaders: 

This is my sample app

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableZuulProxy
    public class GatewayApplication {

        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }

        @Bean
        public Prefilter prefilter(){
            return new Prefilter();
        }

        @Bean
        public ZuulFallbackProvider zuulFallbackProvider() {
            return new ZuulFallbackProvider() {
                @Override
                public String getRoute() {
                    return "kicks-inventory";
                }

                @Override
                public ClientHttpResponse fallbackResponse() {
                    return new ClientHttpResponse() {
                        @Override
                        public HttpStatus getStatusCode() throws IOException {
                            return HttpStatus.OK;
                        }

                        @Override
                        public int getRawStatusCode() throws IOException {
                            return 200;
                        }

                        @Override
                        public String getStatusText() throws IOException {
                            return "OK";
                        }

                        @Override
                        public void close() {

                        }

                        @Override
                        public InputStream getBody() throws IOException {
                            return new ByteArrayInputStream("fallback".getBytes());
                        }

                        @Override
                        public HttpHeaders getHeaders() {
                            HttpHeaders headers = new HttpHeaders();
                            headers.setContentType(MediaType.APPLICATION_JSON);
                            return headers;
                        }
                    };
                }
            };
        }
    }

Solution

  • Each route will need a separate bean. They should return different route for getRoute method.

    Please check this- http://tachniki.blogspot.in/2017/02/generic-ZuulFallbackProvider.html. Hope it will make it slightly easier.