Search code examples
spring-bootlocalnetflix-zuul

Handling of requests locally by Zuul Gateway server


How do I make Spring Boot Zuul Proxy Server handle a specific request locally, instead of proxying it to some other Server?
Let's say I want to do a custom health check of Zuul Proxy Server itself through an API which should return a local response, instead of returning a proxied response from some other remote server.

What kind of route configuration is required to forward the request?


Solution

  • Following is what I figured out and it worked for me:

    1) Create a local request handler:
    Add a regular Controller/RestController with a RequestMapping in a file in Zuul proxy Server code

    @RestController
    public class LocalRequestHandler {
        @GetMapping(path = "/status", produces = MediaType.TEXT_PLAIN_VALUE);
        private static ResponseEntity<String> getServiceStatus() {
            String status = "I am alive";
            return ResponseEntity.ok().body(status);
        }
    }
    

    2) Create a local forwarding route in the configuration file (application.properties or as the case may be) where other routes are defined. In my case, Spring Boot Zuul Proxy server is running on Jetty container with GatewaySvc as the application context.

    zuul.routes.gatewaysvc.path=/GatewaySvc/status
    zuul.routes.gatewaysvc.url=forward:/GatewaySvc/status 
    

    For a standalone Spring Boot, which I have not tested, you may have to remove the context path from the above configuration.

    zuul.routes.gatewaysvc.path=/status
    zuul.routes.gatewaysvc.url=forward:/status
    

    Reference: Strangulation Patterns and Local Forwards