Microservices with Spring Boot — Creating our Microserivces & Gateway (Part 2)

Eureka Server, Client, and the Gateway

Omar Elgabry
OmarElgabry's Blog
5 min readJun 11, 2018

--

Service Registry, Image/Gallery Services, and the Gateway

🙌 The Github repository for the application: https://github.com/OmarElGabry/microservices-spring-boot

Recalling our application architecture, we have a service registry, image service, gallery service, and a gateway.

The gallery service uses the image service underneath, and retrieves a list of all images to be displayed.

The version of spring boot applications we’re going to create is: 2.0.0.RELEASE.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>

Eureka Server

It’s the naming server, or called service registry. It’s duty to give names to each microservice. Why?

  1. No need to hardcode the IP addresses of microservices.
  2. What if services use dynamic IP addresses; when autoscaling.

So, every service registers itself with Eureka, and pings Eureka server to notify that it’s alive.

If Eureka server didn’t receive any notification from a service. This service is unregistered from the Eureka server automatically.

The steps are fairly simple. 1, 2, 3, … and we’re done!.

Ok. So, as usual, create a maven project, or use spring initializr. In the pom.xml file, make sure to include these dependencies: Web, Eureka Server, and DevTools (optional).

Next, in the application.properties file, we need to set some configurations.

Finally, in your spring boot main application class, enable Eureka server using @EnableEurekaServer annotation.

So far so good?. Next, we create our services; image and gallery.

Image Service

The Eureka client service is an independent service in a microservice architecture. It could be for payment, account, notification, auth, config, etc.

The image service acts as a data source for images, each image has an id, title, and url. Simple enough?.

Ok. So, for the pom.xml file, instead of Eureka Server, use Eureka Client.

In the application.properties file, we define configurations (as before)

Then, enable eureka client using @EnableEurekaClient annotation.

Now, our image service is going to expose some data through endpoints, right?. So, we need to create a controller, and define the action methods.

Don’t forget to create the Image entity class with three fields; id, title, and url.

Gallery Service

The Eureka client service can be also a REST client that calls (consumes) other services (REST API services) in our microservice application.

So, for example, the gallery service calls image service to get a list of all images, or maybe only images created during a specific year.

The calls from this REST client to the other services can be done using:

  1. RestTemplate. An object that’s capable of sending requests to REST API services.
  2. FeignClient (acts like a proxy), and provides another approach to RestTemplate.

Both, load balance requests across the services.

— What’s load balancing?

What if more than one instance of a service running on different ports. So, we need to balance the requests among all the instances of a service.

When using ‘Ribbon’ approach (default), requests will be distributed equally among them.

So, as usual, we start by pom.xml . It’s the same as the one for image service. Next is the application.properties file.

In the spring boot main application class, besides enabling the eureka client, we need to create a bean for RestTemplate to call the image service.

In the controller, call image service using RestTemplate and return the result.

Ok, here’s one thing to note. Since we are using restTemplate — which in turn uses Eureka Server for naming of services, and Ribbon for load balancing. So, we can use the service name (like image-service) instead of localhost:port

Gateway — Zuul

When calling any service from the browser, we can’t call it by it’s name as we did from Gallery service — This is used internally between services.

And as we spin more instances of services, each with a different port numbers, So, now the question is: How can we call the services from the browser and distribute the requests among their instances running at different ports?

Well, a common solution is to use 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.

What’s Zuul?

It’s a proxy, gateway, an intermediate layer between the users and your services.

Eureka server solved the problem of giving names to services instead of hardcoding their IP addresses.

But, still, we may have more than one service (instances) running on different ports. So, Zuul …

  1. Maps between a prefix path, say/gallery/** and a service gallery-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.

In the pom.xml add dependencies: Web, Eureka Client, and Zuul

It’s worth mentioning that Zuul acts as a Eureka client. So, we give it a name, port, and link to Eureka server (same as we did with image service).

Finally, enable Zuul and Eureka Client.

Testing our Microservices

Ok. So, we have a service discovery; Eureka server. Two services; image and gallery. And a gateway; Zuul.

To test our application, run eureka server, zuul, and then the two services. Then, go to Eureka Server running at localhost:8761, you should see the running services.

To run multiple instances. In eclipse, go to Run →Configurations/Arguments →VM options and add -Dserver.port=8300

To ping the gallery service, send a request to the gateway, adding the path for the galley service localhost:8762/gallery.

You should see the below message, and if you hit the url again, the request will be routed to the second instance of gallery service; thanks to the load balancer.

Hello from Gallery Service running at port: 8100

Hello from Gallery Service running at port: 8300

To get all images, hit localhost:8762/gallery/1 in the browser

{
"id": 1,
"images": [
{
"id": 1,
"title": "Treehouse of Horror V",
"url": "https://.../rm3842005760"
},
{
"id": 2,
"title": "The Town",
"url": "https://.../rm3698134272"
},
{
"id": 3,
"title": "The Last Traction Hero",
"url": "https://.../rm1445594112"
}
]
}

But, we aren’t done yet!. We still have to authenticate the users. Next, we’ll use JSON Web Tokens (JWT) for authentication.

Thank you for reading! If you enjoyed it, please clap 👏 for it.

--

--

Omar Elgabry
OmarElgabry's Blog

Software Engineer. Going to the moon 🌑. When I die, turn my blog into a story. @https://www.linkedin.com/in/omarelgabry