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.
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
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).
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,
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 .
Then JQuery ajax updates related components of the page according to the response of the request.
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 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, 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.
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.
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.
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.
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.
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.
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.