Configuring Zuul custom filters for logging every request

Zuul provides options to create custom filters so that you can implement typical API gateway functionality, such as authentication, security, and tracing. In this example, we will create a simple logging filter to log every request. The following snippet shows the details:

 @Component
public class SimpleLoggingFilter extends ZuulFilter {

@Override
public String filterType() {
return "pre";
}

@Override
public int filterOrder() {
return 1;
}

@Override
public boolean shouldFilter() {
return true;
}
// run method below

}

A few important things to note are as follows:

  • SimpleLoggingFilter extends ZuulFilter: ZuulFilter is the base abstract class to create filters for Zuul. Any filter should implement the four methods listed here.
  • public String filterType(): Possible return values are "pre" for prerouting filtering, "route" for routing to an origin, "post" for postrouting filters, and "error" for error handling. In this example, we want to filter before the request is executed. We return a value, that is, "pre".
  • public int filterOrder(): This defines the precedence for a filter.
  • public boolean shouldFilter(): If the filter should only be executed in certain conditions, the logic can be implemented here. If you want the filter to always be executed, return true.

We can implement the logic for the filter in the public Object run() method. In our example, we are logging the request method and the URL of the request. The following snippet shows the implementation:

      @Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest httpRequest = context.getRequest();

log.info(String.format("Request Method : %s n URL: %s",
httpRequest.getMethod(),
httpRequest.getRequestURL().toString()));
return null;
}

In the preceding method, we are logging some of the important request details.

When we start up the Zuul server by launching ZuulApiGatewayServerApplication as a Java application, you will see the following log in Eureka Name Server:

    Registered instance ZUUL-API-GATEWAY/192.168.1.5:zuul-api-
gateway:8765 with status UP (replication=false)

This shows that the Zuul API gateway is up and running. Zuul API gateway is also registered with Eureka Server. This allows microservice consumers to talk to the name server to get details about Zuul API gateway.

The following screenshot shows the Eureka dashboard at http://localhost:8761. You can see that instances of Microservice A, service consumer, and the Zuul API Gateway are now registered with Eureka Server:

The following is an extract from the Zuul API gateway log:

  Mapped URL path [/microservice-a/**] onto handler of type [
class org.springframework.cloud.netflix.zuul.web.ZuulController]
Mapped URL path [/service-consumer/**] onto handler of type [
class org.springframework.cloud.netflix.zuul.web.ZuulController]

By default, all the services in Microservice A and the Service Consumer microservice are enabled for reverse proxying by Zuul.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset