Microservices (Part 3) – Gateway to Consumer Service

Prerequisite :

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 

  1. Eureka Service ( Service Registry )
  2. Demo Service ( REST Service Multiple instances registered with Eureka Service )
  3. 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

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 …

  1. Maps between a prefix path, say /consumer/** and a service ejyle-demo-consumer-service . It uses Eureka server to route the requested service.
  2. It load balances (using Ribbon) between instances of a service running on different ports.
  3. 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 initializr. 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 

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
			<version>2.2.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
			  <version>2.2.3.RELEASE</version>
		</dependency>

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.

server.port=8762
spring.application.name=ejyle-zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/ejyle-eureka-server

# A prefix that can added to beginning of all requests. 
#zuul.prefix=/api

# Disable accessing services using service name (i.e. ejyle-demo-consumer-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*

# Map paths to services
zuul.routes.consumer-service.path=/consumer/**
zuul.routes.consumer-service.service-id=ejyle-demo-consumer-service

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 

@SpringBootApplication
@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/

Conclusion

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 !



Author: Goutham Ramesh
A passionate Photo artist and a technology student ! Responsible for Ejyle's Technology Practice and Digital Solutions , Goutham has collective experience of two decades in the area of Solution Architecture, Business Analysis, execution of complex engineering projects and team management.