This quick tutorial will show you how to build your next microservice with the latest version of Eclipse MicroProfile APIs.
Eclipse MicroProfile aims to deliver a growing set of APIs for Java applications composed of multiple microservices. The project has been gaining a lot of attention recently, with a growing list of corporate supporters that also includes Oracle and IBM. There are many servers and frameworks providing the API and that means you can choose the best tool to run your microservices while keeping the same familiar API and behavior. This article is a quick tutorial to using the MicroProfile API for building your next microservice.
MicroProfile is built from core JavaEE, now called Jakarta EE, technologies:
While adding to them a set of specifications that make your microservices ready for the cloud including:
- Configuration Management
- Fault Tolerance
- Health Checks
- JWT Authorization
- Type-safe REST Client
These specifications together make up Eclipse MicroProfile 1.3.
Initial project setup
So how do you use all of this? This is a quick guide to writing your first application. MicroProfile only specifies the API and the behavior but doesn’t include the specified functionality. It’s up to an implementation like Payara Micro to provide the functionality. With Payara Micro, you can run a WAR file from command line but it's also possible to assemble a single executable JAR file. There are many other implementations and you can find them in the list of MicroProfile implementations.
If you choose to run your microservice with Payara Micro, first create a web project that produces a WAR file. If you use Maven or Gradle for your projects, you would set up a standard web application project (with war packaging or war plugin). Once you build the WAR file, you can download Payara Micro from https://www.payara.fish/downloads and run your application from the command line with:
Then, add the MicroProfile dependency to your project.
This one dependency brings in all of the needed APIs to build your application. So what would a typical microservice look like?
- A JAX-RS Controller. Since we're exposing a REST API, we want a controller to handle the API calls.
- A service of some kind. You need some backing component to generate or consume data. We're going to be using some mock data, for now, just to explain the paradigm.
- Configurability. We don't want the client specifying the data volume, we want to do it declaratively.
- Security. Need both declarative and business logic driven security to know how to respond to requests.
- Fault Tolerance. We care about any services we consume and ensuring we can fail fast or recover from failures
- Monitoring. We want to know how often this service is invoked, and how long each request takes.
A REST controller and service
First, we have our rest controller, which should look very familiar to Java EE developers:
For small services, the controller can also contain the service logic. However, it would usually delegate handling of the business logic to another service bean like bookService in our example.
If we dive in further to the book service, we can start to see how configurability works.
Configuration values can be simply injected into the service using the @ConfigProperty annotation on the injection point. The configuration is supplied based on the configuration name, which is used as a key to retrieve the configuration value from the container. Other optional attributes can be supplied, such as the defaultValue, which is used if there’s no configuration for the given name. Even the name attribute is optional. If not provided, it will be generated based on the class and field names so that the configuration value can still be provided later.
So the configuration can also be injected simply like this:
If the default value isn’t provided, a configuration for the name generated according to the specified algorithm has to be available when the application starts.
The configuration is decoupled from bookService and can be supplied by the configuration inside the application or even later, from external sources such as system properties when the application is started.
Next, let's suppose we also want to handle the creation of books, the publication process. And we want to secure the service so that this process is allowed only for callers with a certain role.
MicroProfile offers a solution based on JSON tokens according to the JWT standard. We can inject a JsonWebToken object into our service and easily find out whether the caller has a required role by calling getClaim method:
And then in a method:
The caller is then required to add a valid JWT token with the required claim to the header of the REST call.
A complete publication service to support that may look like this:
For all the above to work, it’s also necessary to enable the JWT security on the JAX-RS application class with the LoginConfig annotation. It’s also important to turn that class into a CDI bean, e.g. by adding ApplicationScoped annotation, because JAX-RS classes aren’t automatically CDI-enabled.
This is how it may look like in the code:
Adding Fault Tolerance
If we consider that managing authors is a separate bounded context, we want that to be represented as a discreet service. Therefore we’ll implement it as a separate REST service in the same manner as the book service. As a result, we want the book service to check that the author exists by connecting to the new author REST service. Below is the complete code for the connector to an external author service:
Annotations Retry, CircuitBreaker, Timeout and others trigger interceptors that implement respective fault tolerance patterns in case of a failure of the underlying action. They are used on an individual method or on a class to apply them for all methods. The Fallback annotation specifies which method should be called if the interceptors cannot recover from failures. This method can provide an alternative result or notify about the error.
Configurability is also fully supported by the fault tolerance annotations. The attributes of the annotations can be overridden via the same configuration mechanism that we used earlier. When any of the interceptors is enabled for a method, it reads the configuration from configuration names generated from the class and field names. For example, to specify the number of retries for the method findAuthor, we can specify a configuration property with the name ws.ament.microprofile.gettingstarted.AuthorService/findAuthor/Retry/maxRetries.
That also means that you can use the annotations without any attributes in the code and configure them later and with different values for each environment.
In the code, we also see a REST client proxy provided by the MicroProfile container. The URL is specified by an external configuration for a generated configuration name, similar to the fault tolerance annotations. And the rest is just calling a method on the proxy which does all the work to do the remote call and return an Author instance.
Monitor what’s going on
So there you have it! A couple of rest controllers, services, and you have a microservice built with Eclipse MicroProfile to manage books.
The last thing is to find out what’s going on inside your application. Metrics and Health Check functionality in MicroProfile containers provide a lot of information out of the box. It’s available via REST endpoints.
Various metrics collected during the lifetime of the application are automatically exposed via REST over HTTP under the /metrics base path, in either JSON or Prometheus format. Common metrics about JVM, threads, loaded classes and operating system are provided out of the box. Other custom metrics can be provided by the implementation. The application can also collect metrics very easily using method interceptors or producer methods.
For example, if a service is running on localhost and port 8080, you can simply access http://localhost:8080/metrics with the HTTP header Accept = application/json and you’ll get something like this:
You can also access http://localhost:8080/health to find out whether the service is running OK or has some errors. This is a simple Yes/No check, giving HTTP 200 status code if all is OK. This is suitable in systems that can detect and restart services with failures automatically, such as Kubernetes.
There are still some more components of MicroProfile 1.3, such as Open API and Open Tracing. We won’t cover them here and let it up to you to explore the API and the documentation, which you can find at microprofile.io. You can find more documentation about Microprofile API, including additional enhancements added by Payara Micro, in the Payara MicroProfile Documentation.
You can also download the full sample code used in this article on GitHub.
Co-authored with John D Ament