Search code examples
spring-cloudnetflix-zuulnetflix-eurekaspring-cloud-netflix

spring cloud zuul: How can return service httpstatus(or handle zuul status return)?


If use zuul routes for eureka service id,service return some httpStatus like 503

curl  10.1.0.21:5701/usr/users/xxx/a -H "Api-Version: v10" -i

Returns:

HTTP/1.1 503 Service Unavailable
Set-Cookie: Session-Token=9e66fb56-21e2-457a-a00f-f788c5ce820b; path=/; domain=.0.21:5701; HttpOnly; Max-Age=2592000; Expires=Sat, 29-Oct-2016 09:23:41 GMT
Date: Thu, 29 Sep 2016 09:23:41 GMT
Session-Token-Expires: 2592000
Connection: keep-alive
ETag:
Session-Token: 9e66fb56-21e2-457a-a00f-f788c5ce820b
Transfer-Encoding: chunked
Content-Type: application/json;charset=UTF-8
X-Application-Context: user-api-provider:5701
Content-Language: en-
Access-Control-Max-Age: 1728000

[{"key":"serviceUnavailable","message":"无效的接口"}]

But zuul reuturns ok/200, why? How can I change how Zuul handles statuses?

curl  10.1.0.19:8090/usr/users/xxx/a -H "Api-Version: v10" -i

Returns:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 0
X-Application-Context: zuul-server:8090
Date: Thu, 29 Sep 2016 09:24:10 GMT

Solution

  • Works for me with latest spring-cloud-netflix (1.2.0):

    @SpringBootApplication
    @EnableZuulProxy
    public class SimpleApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SimpleApplication.class, args);
        }
    }
    

    with application.properties:

    zuul.routes.test.url=http://httpstat.us/503
    zuul.routes.test.path=/test/**
    

    and:

    $ curl localhost:8080/test/
    > GET /test/ HTTP/1.1
    > User-Agent: curl/7.35.0
    > Host: localhost:8080
    > Accept: */*
    > 
    < HTTP/1.1 503 
    < X-Application-Context: application
    < Cache-Control: private
    < X-AspNetMvc-Version: 5.1
    < X-AspNet-Version: 4.0.30319
    < X-Powered-By: ASP.NET
    < Access-Control-Allow-Origin: *
    < Date: Thu, 29 Sep 2016 10:24:16 GMT
    < Content-Type: text/plain;charset=utf-8
    < Transfer-Encoding: chunked
    < Connection: close
    < 
    * Closing connection 0
    503 Service Unavailable$