如何为多个zuul路由实现多个zuulFallbackProvider。
除了公开restcontroller并使用hystrixcommand实现方法外,我看不到如何使用属性的答案。

我可以使用自己的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:

这是我的示例应用
    @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;
                        }
                    };
                }
            };
        }
    }

最佳答案

每条路线将需要一个单独的bean。他们应该为getRoute方法返回不同的路由。

请检查this-http://tachniki.blogspot.in/2017/02/generic-ZuulFallbackProvider.html。希望它能使它稍微容易一些。

关于spring - Zuul为多个Zuul路由实现Multiple ZuulFallbackProvider,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42216079/

10-08 23:17