Routing is an important part of a microservice architecture. In real time cloud application, service instances are added/removed dynamically based on requirement and availability in such scenario its very important to manage traffic load. In the following example you will see how to use Zuul API Gateway to enable sever side load balancing of your RESTful Web Services.
Request flow -
1. When service boots up it registers itself with discovery server. Multiple instances register itself with discovery server. Here spring.application.name is use to identify a particular service
2. Zuul gateway also registers itself with discovery server. Remember Zuul gateway is also a discovery client
3. When client sends request to zuul gateway for a particular service, Zuul gateway queries discovery server to fetch available healthy instance of the service and then accordingly send request to that service.
Please check previous example on Discovery Server.
Discovery code -
pom.xml
application.properties
ZuulServerApplication.java
Zuul Service
pom.xml
application.properties
ServiceApplication.java
Controller.java
Here is complete source code in Github
https://github.com/madan712/zuul-server
https://github.com/madan712/zuul-service
Output -
1. Our discovery server is running on port 8761
http://localhost:8761/
2. Start Zuul server, by default it will run on port 8080
3. Starting 2 instances on zuul service on port 1111, 2222
Note - you may need to pass server.port argument to start the services as shown below
--server.port=2222
Now hit zuul url
http://localhost:8080/zuul-service
On subsequent request u can see response from different instance