Building micro-services, and cloud native applications, can provide some benefits at the business level, and also at the technical level. However, it brings some complexity in the technical level, for example:
- how to detect when a service is down and make other services resilient to that.
- how to provide a contract for our micro-services
- how to make one build (installer) to run on many different environments (local, staging, production), without requiring to build again.
- How to monitor the performance (requests per minute/second, response time, memory or cpu usage) of the application in real time.
Fortunately, MicroProfile helps us to reduce that complexity, providing us a lot of helpful features in our services, and making our application Cloud Native enable with minimum effort.
What is MicroProfile?
MicroProfile is an open-source initiative, also managed by the Eclipse Foundation. MicroProfile optimizes Enterprise Java for micro-services. It has full support on frameworks like: Jakarta EE, Quarkus, Helidon, and partial support on Micronaut.
The latest release at the time of this writing is MP 5.0, and it provides a lot of nice features out of the box, without complex configurations, including:
You can see the code here.
Let’s Get Started!
The best way to understand how these two technologies interact with each other and how we can take advantage of them is to look at some code! We’ll use the projects in this repository, and we’ll see how, with minimum effort, we can use them to get several functionalities.
Our example application consists of two web applications: the items-service
and the stock-service
.
- The
items-service
returns data about different types of items (books, groceries). - The
stock-service
returns stock information about the items (availability, price, status). - The
items-service
consumes thestock-service
to get the stock information of an item.
As I mentioned, MicroProfile is support on different frameworks, this example uses Jakarta EE, but the same applies to other ones.
Our Example App From the Jakarta EE Side
From the Jakarta EE side, we’ll take advantage of JAX-RS to create Restful services easily, and CDI to implement our service classes with business logic. In a real application, we can also use the JPA and JTA specifications to get access to a database. For this blog, though, we won’t dig into JPA and JTA.
<dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-api</artifactId> <version>8.0.0</version> <scope>provided</scope> </dependency>
@ApplicationScoped @Path("/books") public class BookResource{ @Inject private BookService service; @GET @Produces(MediaType.APPLICATION_JSON) public List getAll() { return service.getAll(); } @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{id}") public Book findById(@PathParam("id") Long id) { return service.findById(id) .orElseThrow(NotFoundException::new); }
Adding Micro-profile to our project
Now it’s time to MicroProfile! We enable the basic MicroProfile features by just adding the following dependency to the project.
<dependency> <groupId>org.eclipse.microprofile</groupId> <artifactId>microprofile</artifactId> <version>3.3</version> <type>pom</type> <scope>provided</scope> </dependency>
Microprofile Modules
In the next pages of this article, we will walk through some of the most important modules in MicroProfile, explaining the purpose and some examples.