Microservices (Part 3) – Gateway to Consumer Service


  • Read Part 1 ( Introduction )
  • Read Part 2

Friends , Let us recall our reference architecture ( Micro services generic reference architecture ) and also the Eureka Server and Demo Service and Consumer service created in (Part2).

Recap : Our Solution till now

The solution after Part 2 had the following:
  • Eureka Service ( Service Registry )
  • Demo Service ( REST Service Multiple instances registered with Eureka Service )
  • Demo Consumer Service with REST Template and Load Balancer ( REST Service + REST Template Client to call Demo Service and Ribbon Load Balancer for Demo Service written in step 1 )
In this blog let us add the gateway to the consumer service  as sown below:

Gateway ( Zuul )

When calling any service from the browser, we can’t call it by it’s name as we did from consumer service using eureka — This is used internally between services.: even out Consumer service was called using  ip address and port  like  “http://localhost:8300/  “.  Now as we spin  more instances of services, each with a different port numbers, calling them from browser with IP and port and also managing them and maintenance becomes a nightmare. This can be overcome by using a Gateway.
A gateway is a single entry point into the system, used to handle requests by routing them to the corresponding service. It can also be used for authentication, monitoring, and more. ( We will see this in the next blog )
Let us use Zuul to create a gateway.


Zuul is  a proxy, gateway, an intermediate layer between the users and your services. It is important to note that Eureka server solved the problem of giving names to services instead of hardcoding their IP addresses, but it was not a gateway.
But, still, we may have more than one service (instances) running on different ports. So, Zuul …
  • Maps between a prefix path, say /consumer/** and a service ejyle-demo-consumer-service . It uses Eureka server to route the requested service.
  • It load balances (using Ribbon) between instances of a service running on different ports.
  • What else? We can filter requests, add authentication, etc. Will show how to add authentication in the next blog.
Create a Zuul Gateway called ( EjyleZuulGateway )
Create create a spring boot maven project, or use spring initializer. Call the artifact ‘ EjyleZuulGateway‘   and  group-d  as ‘com.ejyle‘ .
In the pom.xml file, make sure to include these dependencies: Web, Eureka Client, and Zuul as shown below: 
Remember  Zuul acts as a Eureka client. So, we give it a name, port, and link  to Eureka server (same as we did with other services). So change application.properties as shown below.
# A prefix that can added to beginning of all requests.
# Disable accessing services using service name (i.e. ejyle-demo-consumer-service).
# They should be only accessed through the path defined below.
# Map paths to services
Look at last two lines , where we configure how a gateway to Eureka name mapping is done on Zuul.
Finally, enable Zuul and Eureka Client in spring boot app as shown below  look at annotations @EnableEurekhaClient  and @EnableZuulProxy 
@EnableEurekaClient // It acts as a eureka client
@EnableZuulProxy // Enable Zuul
public class EjyleZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(EjyleZuulGatewayApplication.class, args);

Testing our Gateway

  • Run the ‘EjyleEurekaServer’  spring boot app.
  • Run EjyleSpringRestDemo multiple times  and you  can see  multiple Services instances registered under the same service name .
  • Run EjyleSpringRestDemoConsumerService as spring boot app 
  • Run EjyleZuulGateway as Spring boot app
Now the gateway can be tested using url http://localhost:8762/consumer/


Thank you for reading! If you enjoyed it and If you have any questions or comments  you know where to reach me!  Please read  Microservices (Part 4) – Circuit Breaker and Log Tracing as next step !