In order for a deployable unit of code to be qualified as a microservice, it has to possess the following characteristics:
- The size of the source code of one microservice should be smaller than the size of a traditional application. Another size criteria is that one programmer's team should be able to write and support several of them.
- It has to be deployed independently. Naturally, one microservice typically cooperates and expects cooperation from other systems, but that should not prevent our ability to deploy it.
- If a microservice uses a database to store data, it has to have its own schema, or a set of tables. This statement is still under debate, especially in cases when several services modify the same data set or interdependent datasets. If the same team owns all of the related services, it is easier to accomplish. Otherwise, there are several possible strategies to ensure independent microservice development and deployment.
- It has to be stateless, in the sense that its state should not be kept in memory, unless the memory is shared. If one instance of the service has failed, another should be able to accomplish what was expected from the service.
- It should provide a way to check its health—that the service is up and running and ready to do the job.
That said, let's look over the field of toolkits for microservice implementation. One can definitely write microservices from scratch, but before doing that, it is always worth looking at what is out there already, even if you find that nothing fits your particular needs.
The two most popular toolkits are Spring Boot (https://projects.spring.io/spring-boot) and raw J2EE. The J2EE community founded the MicroProfile (https://microprofile.io) initiative, with a declared goal of optimizing Enterprise Java for a microservices architecture. KumuluzEE (https://ee.kumuluz.com) is a lightweight open source microservice framework, compliant with MicroProfile.
A list of some other frameworks, libraries, and toolkits includes the following (in alphabetical order):
- Akka: A toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala (https://akka.io/).
- Bootique: A minimally opinionated framework for runnable Java applications (https://bootique.io/).
- Dropwizard: A Java framework for developing ops-friendly, high-performance, RESTful web services (https://www.dropwizard.io/).
- Jodd: A set of Java microframeworks, tools, and utilities, under 1.7 MB (https://jodd.org/).
- Lightbend Lagom: An opinionated microservice framework built on Akka and Play (https://www.lightbend.com/).
- Ninja: A fullstack web framework for Java (http://www.ninjaframework.org/).
- Spotify Apollo: A set of Java libraries used by Spotify for writing microservices (http://spotify.github.io/apollo/).
- Vert.x: A toolkit for building reactive applications on the JVM (https://vertx.io/).
All of the frameworks, libraries, and toolkits listed support HTTP/JSON communication between microservices. Some of them also have an additional way of sending messages. If they do not, any lightweight messaging system can be used. We mention it here because, as you may recall, message-driven asynchronous processing is a foundation for the elasticity, responsiveness, and resilience of a reactive system composed of microservices.
To demonstrate the process of microservice building, we will use Vert.x, an event-driven non-blocking lightweight polyglot toolkit (components can be written in Java, JavaScript, Groovy, Ruby, Scala, Kotlin, or Ceylon). It supports an asynchronous programming model and a distributed event bus that reaches into in-browser JavaScript, allowing for the creation of real-time web applications.