Building Microservices with Spring Boot — Part 01

Spring Boot | Spring Cloud Netflix Eureka

Microservices, One of the most famous topics in the modern software development industry. Since Microservices are used by Uber, Netflix, Linkedin, and many other famous companies around the globe. We can get a better understanding of the growth of microservices after 2012 by querying google trends for ‘Microservices’ Keyword.

Google Trends Chart for Microservice Keyword.

Spring microservices’, ‘Spring boot’, ‘Java Programming Language’ are the most searched queries along with microservices. So studying about Java Spring boot based microservices design and development will be a definite advantage for anyone’s software engineering career.

Most searched queries on google trends related to microservice.

Differences between Monolith application and Microservice based application

Difference Between Microservices and Monolith application.

Here I’ll explain how to design and develop microservice using below tech stack,

  • Spring Boot 2.0.6.RELEASE
  • Spring Cloud Netflix Eureka Finchley.SR2
  • Zuul API Gateway for Routing
  • JWT Authentication
  • Gradle for dependency management

Configuring Spring Eureka Service Registry

Here I’m using “Spring Cloud Netflix Eureka” to build service registry. This is a normal spring boot based application which acts as a service registry. All the client side services will register into the service registry with a unique name to identify correct service. Let’s build it using the following steps. Here I’m using spring initializr plugin in IntelliJ Idea or else you can use Spring Initializr web interface to generate spring boot based application boilerplate code easily.

Step 01 — Spring Initializr Plugin Intellij Idea
Step 02 — Create Gradle based project with spring initializr
Step 03 — Adding Dependencies to Spring Initializr Project.

Gradle Build File for Service Discovery Project

After correctly create and build the spring boot project we need to add Eureka Service Registry annotation into that application by following steps.

Adding @EnableEurekaServer annotation to the main class.

Updating application.properties

Run application and navigate your browser to http://localhost:3000. You’ll get Spring Eureka dashboard. You can easily get information like Registered eureka clients, Memory usage, CPU details and etc.

Spring Eureka Service Registry Dashboard

Writing Spring Eureka Client Service

As I mentioned above we can create another spring boot based application and register it with the Spring Eureka Service Registry. In that application, we’ve to use,

org.springframework.cloud:spring-cloud-starter-netflix-eureka-client

Complete gradle.build file for Eureka Service Client application.

After generating the boilerplate application, we can add Eureka service client behavior by completing the following steps.

Adding @EnableEurekaClient to main application class.

Now we’ve to make configurations to build communication between service clients and service registry by adding application.properties,

Fine, now the main parts of Microservice architecture is built and configured successfully. So we can run both Registry and client service and check with Spring Eureka Dashboard by accessing http://localhost:3000/ , Here I’ve built another client service with naming as ‘medium-news-service’, Both client services got registered correctly into the services registry.

Registered instances with Eureka service registry.

communication inside microservices using only Eureka service registry.

Let’s write sample @RestController and make simple communication between two instances. Here I’ve built another service and named it as ‘Medium-News-Service’.

API Endpoint in News Service,

Request and receive a response from above endpoint in user-service by using RestTemplate. The RestTemplate with @LoadBalanced annotation will internally use Ribbon LoadBalancer to resolve the ServiceID and invoke REST endpoint using one of the available servers. To do that we’ve to @LoadBalanced @Bean of RestTemplate by

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}

Since we’ve registered medium-news-service with the eureka register, We can use ‘ http://medium-news-service’ directly and call news API endpoint. To test this scenario you just need to call ‘http://localhost:3001/api/v1/find/news/users’ which is the endpoint inside medium-user-service. Then request will be captured and create another request from user service to news service. Then we’ll have a response from news service.

The problem’s to Face in Eureka Service Registry Communication

Let’s think about the microservices project which has 20–50 microservices registered to Eureka service registry.

What if we’ve to secure web services with authorization layer?

We’ve to implement same authorization in all the microservices projects, It’s time wasting and There’s no point of having the same boilerplate code in all the microservices instances.

What if we’ve to give API for consumers like mobile apps, Front End Apps etc?

Then there is no way other than giving access to all the API endpoints and consumers have to keep track of thousands of API endpoints.

Solution

We can use API gateway to create the main entry point to all the web services. So using that API gateway we can create another layer to build authentication for all the web services. So authentication will affect to API Gateway and API consumers have to call API using the gateway, Hence we can easily give solutions for above-mentioned problems by using an API gateway. I’m going to use Zuul API Gateway to build the gateway for this project.

Let’s meet with Part 2 and discuss more about applying Zuul API gateway to the microservices project and configure API security and More.

Thanks for reading… Let’s meet with,

Building Microservices with Spring Boot — Part 02

Zuul API Gateway