Describe the bug I tried to use fallback with zuul within service inner exception. However zuul does not work and return me its own default error page. My spring cloud bug is Edgware.SR1.

The Phenomenon is as below. pls help me check it. thx a lot. 回退失败

Sample Below is my test case. If you have any puzzle with my demo , pls contact me. Or if I have any mistaken steps / configurations pls contact me . thx.

step 1: I used maven model project. my spring cloud version is Edgware.SR1.

Zuul项目概览

step 2: I developed a demo service named user-service to stimulate a business exception. @RequestMapping("/user") @Controller public class UserController {

@RequestMapping("/hello")
@ResponseBody
public String hello() {
    throw new RuntimeException("user ex");
    //return "hello user";
}

}

  • application.yml server: port: 9794

eureka: client: serviceUrl: defaultZone: http://localhost:9791/eureka/ instance: prefer-ip-address: true lease-expiration-duration-in-seconds: 10 lease-renewal-interval-in-seconds: 5

spring: application: name: user-service

step 3: I developed an Eureka server

  • Bootstrap class @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main( String[] args ) { SpringApplication.run(EurekaApplication.class, args); System.out.println( "Eureka OK" ); } }

  • application.yml

server: port: 9791

spring: application: name: eureka-server

eureka:
instance: lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 20 client: registry-fetch-interval-seconds: 5
register-with-eureka: true
fetch-registry: true serviceUrl: defaultZone: http://localhost:9791/eureka/ server: eviction-interval-timer-in-ms: 15000

step 4: I developed zuul server and added a FallbackProvider

  • FallbackProvider class

@Component public class GatewayFallbackRouter implements FallbackProvider{

@Override
public String getRoute() {
    return "user-service";
}



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

        @Override
        public int getRawStatusCode() throws IOException {
            return HttpStatus.INTERNAL_SERVER_ERROR.value();
        }

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

        @Override
        public void close() {

        }

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("The service is unavailable.".getBytes());
        }

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



@Override
public ClientHttpResponse fallbackResponse(Throwable cause) {
     if (cause != null && cause.getCause() != null) {
           System.out.println(cause.getCause().getMessage());   
        }
        return fallbackResponse();
}
  • application.yml

server: port: 9795

eureka: client: serviceUrl: defaultZone: http://localhost:9791/eureka/ instance: prefer-ip-address: true lease-expiration-duration-in-seconds: 10 lease-renewal-interval-in-seconds: 5

spring: application: name: zuul-gateway

zuul: routes: bing: path: /bing url: http://cn.bing.com/ userService: path: /userService/** service-id: user-service strip-prefix: true

step5 : use below url to check fallback whether work or not http://localhost:9795/userService/user/hello?accessToken=1

Comment From: spencergibb

Edgware is long not supported. Please update to Hoxton.SR7 and boot 2.3.3 and try again

Comment From: kyangcmXF

Edgware is long not supported. Please update to Hoxton.SR7 and boot 2.3.3 and try again

Hi guy,

I has upgraded cloud version to Hoxton.SR7. Below is my maven project config: org.springframework.boot spring-boot-dependencies 2.3.3.RELEASE pom import

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

I found two phenomena that zuul fallback does not work. Also I used debug mode to start zuul project to improve the issue. Please check it. thx.

Issue 1: Zuul does not work when user service(just one demo service) does not start up. RibbonCommand returned null in RibbonRoutineFilter. So when it tried to invoke setResponse(), it would throw nullPoniterException.

启动截图 空指针

Issue 2: Zuul does not work when user service throw runtimeException explicitly. In this case, hystrix return a "normly" response however we can see 500 http response code in debug mode. So Zuul ignores it and runs route() routine to the end. That does not invoke fallback as expected.

启动截图 调试截图 UserService截图

Comment From: kyangcmXF

hi guys, have any feedback on it? @spencergibb @spring-issuemaster

Comment From: spencergibb

Edgware is long unsupported (and neither is boot 1.5.x). Please upgrade to boot 2.x and spring cloud Hoxton.SR8 and try again.

Comment From: kyangcmXF

Edgware is long unsupported (and neither is boot 1.5.x). Please upgrade to boot 2.x and spring cloud Hoxton.SR8 and try again.

Thanks your reply. I have upgraded my demo with Hoxton.SR7 version. You can find it in my last comment on Aug 22th. Can you try it in your free time? Let me know your puzzle and improve zuul together. :)

Comment From: OlgaMaciaszek

Please provide a minimal, complete, verifiable example that reproduces the issue.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.