Executing a Request through Zuul API GatewayStep 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. ![]() 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. ![]() 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. ![]() We can also see the content of the request that is printed on the Zuul API Gateway server. The request prints the request URI. ![]() We have sent the request through the Zuul API Gateway, instead of directly calling the microservices. Setting up Zuul API Gateway between microservices invocationsIn 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: ![]() Let's see the log for NetflixZullApiGatewayServerApplication. Step 7: Click on the arrow that is beside the console icon and select the NetflixZullApiGatewayServerApplication. ![]() It shows a couple of logs, as shown in the following image. ![]() Step 8: Refresh the URL again. It shows a single log on the console. ![]() 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.
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: 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. ![]() 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. ![]() In this section, we have executed both the services through the Zuul API Gateway server.
Next TopicIntroduction to Distributed Tracing
|