Today we will see how Red Hat approaches the creation of Microservices with Quarkus, which can also help us develop our company’s business.
Supported by Quarkus for the creation of these microservices in an agile way and with minimum weight and memory consumption. It is specially designed for their creation as containers and deployment in serverless, cloud, and Kubernetes environments.
What is Red Hat Quarkus?
Red Hat Quarkus is a Java framework specially oriented to create and deploy Java applications in the cloud, with a minimum size and consumption. Adapted to follow the Jakarta enterprise standards, it relies on the MicroProfile, RESTEasy, or Hibernate frameworks to create microservices.
All of this, of course within a support framework provided by Red Hat, a multinational provider of enterprise-level open-source software solutions. And perfectly integrated within an ecosystem of industry-leading applications such as Kubernetes, ActiveMQ, Kafka, OpenShift, etc.
We have seen how to do integrations with Red Hat in previous posts, which you can check here. Specifically Red Hat Fuse, now called Red Hat Integration, is Red Hat’s solution for integrating systems with an updated and cloud-oriented vision, commonly known as ESB, into more legacy systems.
How to create microservices with Red Hat Quarkus
Development
Starting from scratch, it is best to create the project structure with the help of an archetype. This can also be done with one of the tools provided by Red Hat. In this case, the quarkus-maven-plugin plugin and the following command:
mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.13.8.Final-redhat-00004:create. -DprojectGroupId=com.chakray.example -DprojectArtifactId=ms-with-panache
In order to make it manageable, we will add persistence to the microservice. This will allow us to generate a more complex microservice that enables us to query, create, and edit data from a database. In our case, we will have the following main libraries:
- REST API creation: quarkus-resteasy-reactive
- Data transformation and access: quarkus-hibernate-orm-panache
- Message format: quarkus-resteasy-reactive-jackson
The API generation that allows the microservice to offer an interface to third-party systems can be one of the most accessible parts, especially if you have previous experience with enterprise Java frameworks. Even so, we will give a small explanation of how to generate any method:
- @Path(“/book”): Allows us to indicate which is the root of the microservice in which context it attends calls. It is also used in each one of the resources if it has a different subsequent context.
- @GET, @DELETE, etc: Indicates the HTTP verb with which we will invoke.
- @QueryParam: Allows us to indicate parameters that travel in the URL
- @PathParam: Allows us to indicate parameters that are part of the context. They must be shown in the @Path annotation.
- @Produces and @Consumes: We can indicate the output format and the expected input.
For the persistence part, we will use Panache, which will provide us with two different patterns to carry it out.
On the one hand, we will have the Repository pattern, where we will create an intermediate class that will perform the operations against the database. And that extending the PanacheRepository class will put at our disposal a wide set of basic access methods.
@ApplicationScoped @Transactional public class BookRepository implements PanacheRepository<Book>{ }
On the other hand, we will have the Active Record pattern, where the entity itself will provide the programmer with different persistence methods.
@Entity @Table(name = "BOARDGAME") public class BoardGame extends PanacheEntity { //Careful! With PanacheEntity, we don't need either getter/setter or id public String designer; public String name; }
Here, we can see the main class of our microservice:
@Path("/boardgame") public class BoardGameResource { @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public BoardGame getBoardGameById(@PathParam("id") final Long id) { return BoardGame.findById(id); } @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Transactional public BoardGame persist(final BoardGame newBoardGame) { BoardGame.persist(newBoardGame); return newBoardGame; } }
2. Configuration
To configure the different features, we can do it through the application.properties file. This is the only configuration file; through it, we will be able to configure not only any library to use inside the project but also to indicate values for environments. Here we indicate some of them:
- http.port: We can indicate the port on which the calls are supported.
- datasource.username/password/jdbc.URL: We can indicate the connection to the DB.
- hibernate-orm: It has multiple options to indicate if we want or not to generate the DB schema when starting the application and the files for it.
3. Deployment
Once we have developed and configured the project, we will start it. As we are in development, we will use the command:
mvn quarkus:dev
Another advantage of using Red Hat Quarkus is that we can quickly reload the application and check the changes by simply pressing the ‘s’ key in the console where we have it running.
And, of course, if we add more configuration and Quarkus extensions to make it possible, we can generate native images to be used by Kubernetes.
Conclusion
As you have seen, with the support of Red Hat and Chakray, you can have microservices that help develop your business—counting on the benefits of a technology that is on the rise and generates agile, light, and oriented projects for deployment in the cloud.
If you need help, we are here to help your company; contact us today!
Talk to our experts!
Contact our team and discover the cutting-edge technologies that will empower your business.
contact us