Configuring Zuul properties

Let's configure Zuul properties in our application using the application.yml configuration file. These are the configurations we have created for our application configuration file:

spring:
application:
name: API-GATEWAY
server:
port: 8080
eureka:
client:
service-url:
default-zone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
prefer-ip-address: true
zuul:
ignoredServices: '*'
prefix: /api
routes:
account-service:
path: /accounts/**
serviceId: ACCOUNT-SERVICE
customer-service:
path: /customers/**
serviceId: CUSTOMER-SERVICE
host:socket-timeout-millis: 30000

In the preceding application configuration file, first, we have configured the application name as API-GATEWAY and the server port with 8080 for the Edge Service application. And we have defined configurations related to the Eureka client for registering this Edge Service application with the Eureka Server.


If you want to use routing based on service IDs, you need to provide Eureka on the classpath and have to register this service with the Eureka registry server. You can also use Zuul without the Eureka Server, but you have to provide the exact URL of the service where it will be redirected:
zuul.routes.account-service.url=http://localhost:6060.

Finally, we have configured Zuul properties in the application configuration file. First, we have to skip all the default services from the Zuul proxy by using the following configuration:

zuul:
ignoredServices: '*'
account-service:
path: /accounts/**

In the preceding example, all services are ignored except account-service. 

We can also use a common prefix for URLs, such as /api, for which we want Zuul to proxy by setting zuul.prefix property:

zuul:
prefix: /api

We can also customize the path mappings of services as follows:

zuul:
routes:
account-service:
path: /accounts/**
serviceId: ACCOUNT-SERVICE

Here, zuul.routes.account-service.path will route all traffic to request the service with the ACCOUNT-SERVICE service ID. Now, the http://localhost:8080/api/accounts/account URL will be forwarded to the ACCOUNT-SERVICE microservice. Let's configure another microservice Customer similar to Account microservice in the example.

Finally, we have configured the Zuul host socket timeout with the following configuration:

zuul:
host:
socket-timeout-millis: 30000

In the preceding configuration, we have configured it to instruct Spring Boot to wait for the response for 30000 ms.

Now, our microservice application for the API Gateway service is ready to run and test. Let's start Eureka Server, AccountService, CustomerService, and APIZuulService application.

Let's open the Eureka dashboard with the http://localhost:8761/ URL, as follows:

In the preceding screenshot, you can see our three microservices are running and registered with Eureka.

Let's hit the following URL of Customer service for customer UI application. http://localhost:8080/api/customers/customer/1001, you will see this URL will be routed to the Customer service internally with the http://localhost:6161/customer/1001 URL.

Let's see the following screenshot for the public API call for Customer service using API Gateway http://localhost:8080/api/customers/customer/1001:

As you can see, we have called the Customer microservice using the API Gateway Zuul proxy. Internally, this Zuul proxy calls the Customer service with the http://localhost:6161/customer/1001 URL. Similarly, the Account UI component can call the Account microservice using the API Gateway Zuul proxy service using the http://localhost:8080/api/accounts/account/100 URL:

As you can see, we have called the Account microservice using the API Gateway Zuul proxy. Internally, it will call the actual service with the http://localhost:6060/account/100 URL.

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

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