Spring Boot: Microservice Enhanced

In early series of Spring Boot demo we have gone through basics of booting an application using spring boot. In this blog i’ll show you what is microservice pattern and how actually we can implement.

When microservice will be useful

Below is the list of scenario where microservice is useful.

  • Divide whole application in workable modules.
  • Each module works individually.
  • We can deploy it independently.
  • Provide balancing among multiple web services.
  • Adding of new service will not affect others as it is independently working and loosely coupled.
  • Scale your application across horizons.

Goal

Goal of this demo is

  • Develop 3 different microservices.
  • Deploy all of them in separate environment (for Local we are running on separate port).
  • To know how one service will talk with another.

Use case

Here we are creating 3 different microservices. Technically we are creating 3 different project and also will run on different port.

  • CartService: Managing Cart Operations, ideally contains mapping between Product and User (Customer)
  • ProductService: Expose services related to product.
  • UserService: Expose services related to user.

Let’s look all of them in detail.
You can clone repository from below git.
GitHub Repo: https://github.com/yogeshmprajapati/kode12-spring-boot.git

MicroService 1: Product Service

Git Module: spring-boot-microservice-enhanced-product
Runs on: Port 8081

ProductVO.java

ProductService.java

Service class to serve product detail, here we have hardcoded detail for product 1 and 2.

ProductController.java

Controller to expose product data.

SpringBootMicroserviceEnhancedProductApplication.java

Class with main method to start an application.

application.properties

Run MicroService 1 (Product Service)

Url 1: http://localhost:8081/product/1
Use: to get product by id 1.
Output:

Url 2: http://localhost:8081/product/2
Use: to get product by id 2.
Output:

MicroService 2: User Service

Git Module: spring-boot-microservice-enhanced-user
Runs on: Port 8082

UserVO.java

UserService.java

Service class to serve user detail, here we have hardcoded detail for user 1.

Controller to expose user data.

SpringBootMicroserviceEnhancedUserApplication.java

Class with main method to start an application.

application.properties

Run MicroService 2 (User Service)

Url 1: http://localhost:8081/user/1
to get user detail by id 1.
Output:

MicroService 3: Cart Service

Git Module: spring-boot-microservice-enhanced-cart
Runs on: Port 8080

CartVO.java

ProductVO.java

Same as Microservice 1’s ProductVO.java

UserVO.java

Same as Microservice 2’s UserVO.java

CartService.java

Service class to serve cart detail, here we have hard coded following mapping

Order#1 {User#1, Product#1}
Order#2 {User#1, Product#2}

Here we have used RestTemplate to talk with other microservices.

For Ex:
ProductVO productVO1 = restTemplate.getForEntity("http://localhost:8081/product/1", ProductVO.class).getBody();

The code above is used to request product service to provide product detail having product id is 1.

Run MicroService 3 (Cart Service)

Url: http://localhost:8080/cart/getAll
Use: to get all cart records.
Output:

Here User Name and Product Name for each user and product are not available in cart service, actually it will be from other microservices.

Issue

After looking into code you can have a doubt that here we have hard coded urls like http://localhost:8081 or http://localhost:8082 and in real time environment it may vary, so how to handle this thing in better way ?

The good answer of this is we need to use some discovery service like Eureka. It is used for middle tier load balancing and having multiple useful feature, i’ll show you how to work with eureka in upcoming posts.

Share current post by copy: https://goo.gl/5oyuwp
Happy Learning!
:)

Thanks,
Yogesh P