Getting started with microservices
In this part of the tutorial, you’ll learn how to run applications developed with microserivces. Although, in real scenarios, you would use an orchestration tool such as Kubernetes or Docker Swarm to automate the process, in this tutorial, you’ll run and stop microservices manually. This will give you a clear understanding of how microservices work.
In this series of tutorials, you will develop a simple news dashboard. The following is a screenshot of the finished application:
On the left side, there’s an administration view with a list of companies and their Twitter handles. The latest tweets from this companies are shown in the right side. Both sides are implemented independently and use a third microservice to read and write data.
|If you had to develop an application like the one in the previous screenshot, you would most likely go with a monolithic application! Keep in mind that the objective of this series of articles is not to teach you whether microservices suit your needs but how to use concrete techniques and patterns common in microservices.|
The functionality of the demo application is provided through 4 microservices:
biz-application: A REST API for managing data (companies).
admin-application: A web user interface to interact with data (companies).
news-application: A web user interface to display data (companies’ tweets).
website-application: A portal-like website that renders the
news-applicationin a single web page.
Additionally, the application requires three orchestration and monitoring services:
discovery-server: A service registration and discovery server.
config-server: An externalized configuration server.
proxy-server: An edge service with dynamic routing.
monitor-application: A dashboard for service monitoring.
You’ll learn about each of these applications and orchestration services throughout this tutorial series.
To get an idea of how this system runs, take a look at the following screenshot:
As you can see, each terminal is about to run a Java application as an individual process. We are using the same host machine for running all processes, but they can be configured and run in separate machines if we wanted.
The processes on the left are the orchestration services. The processes on the right are the functional microservices.
The code for this demo application is published on GitHub. Give it a try and run the application by yourself! All you need is Maven, and 8 terminals like the ones in the previous screenshot. The following are the steps to download and run the demo application:
Download the code from GitHub or from the command line with Git:
git clone https://github.com/alejandro-du/vaadin-microservices-demo.git
Compile the project with Maven:
cd vaadin-microservices-demo mvn package
Open 8 terminals and change them to the
The first service you need to get ready is the
discovery-server. It allows services to discover other services without knowing their exact location (URI). Run the
discovery-serverapplication by executing the following in one of the terminals:
cd discovery-server java -jar target/discovery-server-0.0.1-SNAPSHOT.jar
|You don’t need to wait for services to start or even run the services in any specific order. Services depending on others will eventually start correctly when their dependencies become available. However, to get a smooth run of the app and avoid waiting for service registrations to happen, run the services in the order described here and wait for the service to start before running the next. If you don’t, you might get exception messages and will have to refresh the browser until you get the app in a ready state. This is a completely normal behaviour, though.|
The second service you need in place before deploying the functional microservices is the
config-app. It serves as an external source of configuration for the microservices. Run it by executing the following in one of the free terminals:
cd config-server java -jar target/config-server-0.0.1-SNAPSHOT.jar
Use free terminals to start each of the functional microservices:
cd biz-application java -jar target/biz-application-0.0.1-SNAPSHOT.jar
cd admin-application java -jar target/admin-application-0.0.1-SNAPSHOT.jar
cd news-application java -jar target/news-application-0.0.1-SNAPSHOT.jar
cd website-application java -jar target/website-application-0.0.1-SNAPSHOT.jar
Start the edge service to allow access to the web applications through a single point:
cd proxy-server java -jar target/proxy-server-0.0.1-SNAPSHOT.jar
Point your browser to http://localhost:8080 and try adding companies with actual Twitter account names. Refresh the browser to see the latest tweets of these companies.
You can horizontally scale the system by running additional replicas of the
For example, scale the
biz-application microservice by opening a new terminal and starting an additional instance:
cd biz-application java -Dserver.port=9002 -jar target/admin-application-0.0.1-SNAPSHOT.jar
Once the new instance starts and becomes discoverable, requests (from the
news-application microservices) are load-balanced to the two running
biz-application instances using a round-robin scheduling strategy.
|Keep in mind that since this is a demo application, the current configuration aims to have a quick discovery of services and replication of data at the cost of increased network traffic and processor time.|
Go to the browser, select one of the companies and modify it’s name. Don’t click the Update button yet! Let’s simulate the scenario when one of the
admin-application instances becomes unavailable just before the user is about to save the changes.
First, start a new instance of the
admin-application as follows:
cd admin-application java -Dserver.port=9102 -jar target/admin-application-0.0.1-SNAPSHOT.jar
Make sure the new
admin-application instance is up and running and stop the other one (press Ctrl+C in the terminal). Return to the browser and click the Update button now. The web application should remain functional and eventually save the data you entered without losing the state of the UI. You might see a Please wait… message. This happens while the system updates the discovery registry and re-routes the request to an instance that is available.
Stop all the
biz-application instances and refresh the browser to see the fallback mechanisms.
admin-application will show an error message informing the data cannot be saved or loaded. In this example, the
admin-application is only the CRUD shown on the left of the page. In real-world cases, it would most likely contain many other screens. Thanks to the fallback mechanisms, users would be able to continue using the parts of the application that don’t depend on the
In the case of the
news-application, a predefined set of companies is shown when the
biz-application is not available.
When you start the
biz-application again, its services become available and other microservices will start using it.