Original

a Taste of Microservice Using Spring Cloud Framework


Overview

The idea of microservice is to break single application into several self-contained services, which helps to bring up the scaliability and availability. To better understand the essence of microservice, I set up tutorial application: Book reservation online using Spring cloud framework, which is a kind Hello World to microservice. The code base is maily from a course by a Chinese instructor named Qi Yi.

Requirement and analysis

The online book reservation is supposed to be a part of library system, where the readers are able to select and reserve books so that they could pick them up on a specified date. Detailed requirements are

  • verify membership using phone number,
  • select a book and date,
  • reserve the book.

Basically this is a web application and the requirement can be achieved using two microservices, member service (to verify the mobile number) and book service (to list books and reserve a book).

Frontend

We start with the frontend for better understanding the requirement. To simplifiy the situation, only one html file (index.html) is used for the frontend. The screenshots are as following,

  1. Show index page,
  2. Input phone number,
  3. Click Verify, which goes to the Member service, if the phone is registered, returns the member's name, in this case, leo5n. The system could also send a verification code to the phone (not implemented). If the phone number is not found, show Not such member,
  4. Click select a book, which goes to the Book service and gets list of books. Currently three books are listed at the bottom of the page,
  5. Select one book in the list, which is one of my favorite books from Arthur Clarke,
  6. Select a checkout date,
  7. Select a checkin date,
  8. Click Reserve,
  9. Show the result.

The frontend (index.html) uses jquery WeUI which is a easy-to-use library to build web application for mobile. JQuary ajax is used to send request to backend service .

  • To verify the phone:
http://localhost:9000/member/check?mobile=xxxxx
  • To list books:
http://localhost:9000/book/list
  • To reserver a book:
http://localhost:9000/book/reserve

Then JQuery ajax updates related components of the page according to the response of the request.

Backend

The backend is implementated using Spring Cloud/Spring Boot framework. There are totally 5 Spring Boot applications, which are shown as following. This also shows one of the trade off in microservice approch: resource intensity and complexity. This is the reason why I could only show the screenshots instead of setting up a live system, cause my VM in AWS is not powerful enough to host all these applicatons.

Eureka Registry

Eureka discovery is at the center of Spring Cloud system and each service needs to register to Eurek server in order to be discovered and used later on. There needs to be at least on Eureka server in the system, and there can be multiple Eureka servers regestered to each other to provide high availability.

Zuul Gateway

Zuul, the Gateway service, acts as a proxy and provides uniform interface for external users, such as browser on the mobile. It also provides load balance functionality when multiple instances are available for one service.

Config Server

In real world, there might be hundreds of instances for the same service, which makes it hard to configure them one by one if the configuraiton files are maintained locally on the instance. The Config Server can connect to a source control server, more specifically Git, to get the latest configuration file. Then each of instance can contact Config Server to get the config file instead of using the local configuration (applicaiton.yml/properties). In our case, configuration files for both Book and Member service are uploaded into git server (gitee.com), and Book service and Member service are geting these configuration through config server/Configuration center. In case of configuration changes, we only need to upload the latest configuration into gitee.com, then all of the instances of the service, regardless number, get the updates.

Member Service

The Member service is a traditional Spring Boot application as well as an Eureka client, which registers itself to Eureka server. It connects to member DB in Mysql(most a member table) using JPA and responses the request to check if a phone number is regestered.

Book Service

The Book service is the other microserice registered to Eureka server. It connects to book DB in Mysql(most a book table and a checkout table) using JPA and provides the functionality to list books and reserve a book.

The above are 5 main application/services in the system. Three of them, Eureka, Zuul and Config are part of the framework infrastructure. The other two, Member and Book are business services.

Communication between services - RestTemplate and Feign

There are lots of cases when the services need to talk to each other, for example, Book service needs to check if the memeber exists during reservation operation. There are basically two approaches to do this. RestTemplate is a straight forward approach where one service uses RestTemplate, a HTTP client, to invoke another service. The other approach is to use Feign library, where one service can invoke other service the way like it was a local method, and the underlining mechanism is done automatically by Feign library.

Service downgrade - Hystrix

When one service invokes another service, there is possibility that the later is busy or down. Hystrix library is used to handle this so that when the service to be invoked is not essential, the invoking service can bypass this situation and keep the normal business logic going.

Summary

This is my hello world to microservice, which contains the basis of Spring Cloud framework. The pros of microservice are scalability and availabilty, and the cons are complexity and costly. Each team and each project might need to find the balance between them.

frontend
backend