Blog

Getting Started with Microservices in Java

By  
Alejandro Duarte
Alejandro Duarte
·
On Dec 15, 2017 8:06:00 AM
·

Microservices are single-purpose, loosely coupled applications that can be developed, scaled, and deployed independently. The sum of them constitutes a whole system. Microservices promote continuous delivery, team independence (freedom to select the right tools and processes), quick evolvability, scalability, resilience, and high availability.

In this series of articles, you’ll learn how to implement an application using a microservices architecture in plain Java with Spring for the backend and orchestration services, and Vaadin Framework for the web user interface.

The Demo Application

The following is a screenshot of the finished demo application:

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 architectures. If you had to develop an application like the one in the previous screenshot, you would most likely go with a monolithic application!

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 multiple web applications in a single web page.

Additionally, the system requires three orchestration services:

Running the Application

To get an idea of how this system runs, take a look at the following screenshot:

Think of each terminal as a separate machine. The machines on the left are the orchestration services. The machines on the right are the functional microservices. As you can see, each machine is about to run a Java application as an individual process.

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 7 terminals like the ones in the previous screenshot. The following are the steps to download and run the demo application:

1. Download the code from GitHub or from the command line with Git:

git clone https://github.com/alejandro-du/vaadin-microservices-demo.git

2. Compile the project with Maven:

cd vaadin-microservices-demo
mvn package

3. Open 7 terminals and move all of them to the vaadin-microservices-demo directory.

4. 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-server application by executing the following in one of the terminals:

cd discovery-server
java -jar target/discovery-server-0.0.1-SNAPSHOT.jar

Wait until the application is fully started.

5. 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

Again, wait until the application is fully started.

6. Use free terminals to start each of the functional microservices:

cd biz-application
java -Dserver.port=9601 -jar target/biz-application-0.0.1-SNAPSHOT.jar

cd admin-application
java -Dserver.port=9401 -jar target/admin-application-0.0.1-SNAPSHOT.jar

 
cd news-application
java -Dserver.port=9201 -jar target/news-application-0.0.1-SNAPSHOT.jar

 
cd website-application
java -Dserver.port=9001 -jar target/website-application-0.0.1-SNAPSHOT.jar

7. 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

Using the Demo Application

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 the entered companies.

Scaling Individual Microservices

You can horizontally scale the system by starting more instances of the biz-application, admin-application, news-application, and website-application microservices.

Scale the admin-application by opening a new terminal (or tab in the current admin-application terminal) and running an additional instance of the admin-application microservice:

cd admin-application
java -Dserver.port=9402 -jar target/admin-application-0.0.1-SNAPSHOT.jar

At this point, when you use the admin screen, the requests are going to be load-balanced to the two running instances of the admin-application 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. The system might use lots of resources if you run several instances of the same application in one single machine.

Testing High Availability

Make sure you are running two instances of the admin-application. Go to the browser and click the + (Add) button and enter Vaadin as the name, and vaadin as the Twitter username but don't click the Add button yet.

Let’s simulate the scenario when one of the admin-application instances becomes unavailable by stopping its instances (Ctrl+C in the terminal). Return to the browser and click the Add button. The web application should remain functional and save the data you entered without losing the state of the UI.

Testing System Resilience

Stop all the biz-application instances and refresh the browser to see the fallback mechanisms.

The admin-application will show an error message informing the data cannot be saved or loaded. In this example, the biz-application is merely the CRUD shown on the left of the page, but 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 require the biz-application.

In the case of the news-application, a predefined set of companies is shown when the biz-application is not available.

Next Steps

This was just the tip of the iceberg. There is much more to explore in this demo application. The next articles in this series will explain how to develop this application from scratch while discussing each microservices technique and pattern used.

Learn more about Vaadin & Spring

Alejandro Duarte
Alejandro Duarte
Software Engineer and Developer Advocate at MariaDB Corporation. Author of Practical Vaadin (Apress), Data-Centric Applications with Vaadin 8 (Packt), and Vaadin 7 UI Design by Example (Packt). Passionate about software development with Java and open-source technologies. Contact him on Twitter @alejandro_du or through his personal blog at www.programmingbrain.com.
Other posts by Alejandro Duarte