Home » Executing a Request through Zuul API Gateway

Executing a Request through Zuul API Gateway

by Online Tutorials Library

Executing a Request through Zuul API Gateway

Step 1: Run the netflix-eureka-naming-server.

Step 2: Run the currency-exchange-service on port 8000.

Step 3: Run the currency-conversion-service on port 8100.

Step 4: Run the netflix-zuul-api-gateway-server.

Step 5: Open the browser and invoke the URL http://localhost:8761. It shows all the services that are registered with the Eureka naming server.

Executing a Request through Zuul API Gateway

Step 6: Invoke the URL http://localhost:8000/currency-exchange/from/EUR/to/INR. We get the response, but the request does not go through the Zuul API Gateway.

Executing a Request through Zuul API Gateway

Let’s invoke the request through the Zuul API Gateway. We use the following URL: http://localhost:8765/{application-name}/{uri}. The port 8765 is the default port for the Zuul API Gateway server.

In our case, the application name is currency-exchange-service, and the URI is /currency-exchange/from/EUR/to/INR. So the complete URL will look like the following:

http://localhost:8765/currency-exchange-service/currency-exchange/from/EUR/to/INR

Step 7: Copy the above URL and paste it in the browser. We get the same response as above, but at this time, the request is going through the Zuul API Gateway.

Executing a Request through Zuul API Gateway

We can also see the content of the request that is printed on the Zuul API Gateway server. The request prints the request URI.

Executing a Request through Zuul API Gateway

We have sent the request through the Zuul API Gateway, instead of directly calling the microservices.

Setting up Zuul API Gateway between microservices invocations

In the previous step, we have used a direct URL to execute the currency-exchange-service through the Zuul API Gateway proxy. When we use the URL http://localhost:8765/currency-exchange-service/currency-exchange/from/EUR/to/INR, it uses port 8765 that is proxy for API Gateway.

In this section, we will call the currency-calculation-service (currency-conversion-service) that calls the currency-exchange-service. So far, we were calling the service directly. Now, we will call it through the Zuul API Gateway instead of directly calling the currency-exchange-service.

Step 1: Select the project currency-conversion-service.

Step 2: Open the CurrencyExchangeServiceProxy.java file.

Step 3: Enable the Feign by using the annotation @FeignClient with the attribute name=”netflix-zuul-api-gateway-server”.

Remember: Remove or comment all other annotations @FeignClient in CurrencyExchangeServiceProxy.java file.

Step 4: Define the mapping for the Zuul API Gateway server.

Remember: Remove or comment the mapping for currency-exchange-service.

Step 5: Run the netflix-eureka-naming-server, currency-exchange-service, currency-conversion-service, and netflix-zuul-api-gateway-server in the same order in which we have written.

Remember: Ensure that all four services are running properly.

Step 6: Open the browser and invoke the URL http://localhost:8100/currency-converter-feign/from/USD/to/INR/quantity/1000. It returns the following response:

Executing a Request through Zuul API Gateway

Let’s see the log for NetflixZullApiGatewayServerApplication.

Step 7: Click on the arrow that is beside the console icon and select the NetflixZullApiGatewayServerApplication.

Executing a Request through Zuul API Gateway

It shows a couple of logs, as shown in the following image.

Executing a Request through Zuul API Gateway

Step 8: Refresh the URL again. It shows a single log on the console.

Executing a Request through Zuul API Gateway

Whenever we call the CurrencyClaculationService (currency-converter-service) through Feign, it routed through the API Gateway server. The Gateway executes a filter called ZuulLoggingFilter that invokes the currency-exchange-service.

Now let’s intercept the calls between currency converter-service and currency-exchange-service. It means the API Gateway executes two times when we invoke the URL.

  • The first time, API Gateway executes when we call the currency-conversion-service. It means before the execution of the currency-conversion-service. The currency-conversion-service routed through the API Gateway.
  • The second time, API Gateway executes when the currency-conversion-service calls the currency-exchange-service. It means after the execution of currency-conversion-service and before the execution of currency-exchange-service. The currency-exchange-service also routed through the API Gateway.

Let’s implement the interception in our project.

Send the request http://localhost:8765 through the API Gateway. The URI will be /{application-name}/{uri}. The complete URL will look like the following:

http://localhost:8765/currency-conversion-service/currency-converter-feign/from/USD/to/INR/quantity/1000

Invoke the above URL. It returns the same response as the URL http://localhost:8100/currency-converter-feign/from/USD/to/INR/quantity/1000 returns.

Executing a Request through Zuul API Gateway

We can see in the log that the logging filter executes two times. The first time it calls the currency-converter-service and the second time when the currency-converter-service calls the currency-exchange-service.

Executing a Request through Zuul API Gateway

In this section, we have executed both the services through the Zuul API Gateway server.


You may also like