Javatpoint Logo
Javatpoint Logo

Implementing Zuul Logging Filter

In the previous section, we have discussed common functionality that is implemented in API Gateway. We have the Zuul server ready that acts as API Gateway.

In this section, we will implement the logging functionality in the Zuul API Gateway.

Let's implement the logging in the Zuul API Gateway.

Step 1: In the netflix-zuul-api-gateway-server project, create a new class file with the name ZuulLoggingFilter.

Step 2: Add an annotation @Componenet.

Step 3: Extend the ZuulFilter class. There are some abstract methods in the ZuulFilter class. These abstract methods must be implemented in the ZuulLoggingFilter class.

Step 4: Move your cursor over the ZuulLoggingFilter class. It suggests two quick fixes, as shown in the following image. Click on the Add unimplemented methods. We get the default implementation of all the abstract methods of the ZuulFilter class.

Zuul API Gateway

ZuulFilter class has four abstract methods that are listed below:

  • shouldFilter(): The shouldFilter() method checks the request and decides whether filter to be executed or not.
  • run(): The run() method invokes, if both !isFilterDisabled() and shouldFilter() methods returns true.
  • filterType(): The filterType() method classify a filter by type. There are four types of standard filters in Zuul: pre for pre-routing filtering, route for routing to an origin, post for post-routing filters, and error for error handling. Zuul also supports a static type for static responses. Any filter type can be created or added and run by calling the method runFilters(type).
  • filterOrder(): The filter order must be defined for a filter. Filters may have the same filter order if the precedence is not important for the filters. The filter order does not need to be sequential.

Step 5: Create the Logger class object and invoke getLogger() method to create a logger.

Remember: Import Logger class of org.slf4j package.

Step 6: Implement the logic in the run() method.

ZuulLoggingFilter.java

In the next step, we will see how to intercept the request using Zuul.









Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA