Creating Camel 3 CDI Microservices
In this post, I want to go through some libraries that allow creating microservices within a camel ecosystem (Pure Apache Camel 3 microservices).
Join the DZone community and get the full member experience.Join For Free
As many people already know, Apache Camel is an open-source project that makes easy implement integration projects, in this post I want to go through some libraries that allow creating microservices within a camel ecosystem (Pure Apache Camel 3 microservices).
In order to create microservices with camel we can combine some camel modules such:
- Camel-undertow — It provides an HTTP/Websockets server
- Camel-swagger-java — Enable document our services and generate automatically swagger definitions.
- Camel-cdi — Bring a dependency injection capability into Apache Camel projects.
So let's talk shop.
I used the Camel CDI archetype to create my CDI project.
The project generated is a plain Camel CDI java project, then, how I can convert this in a microservice?
First of all, we need a class with a main method like this:
Please note the above snippet:
- Use org.apache.camel.cdi.Main class instead of org.apache.camel.main.Main class.
- It does not register any route with the main.addRoutesBuilder method is because of CDI auto-scan all components in the classpath.
Secondly, we have to add Undertow, Swagger and JSON dependencies in the pom.xml.
The above dependencies are not enough, we have to add another couple to allow the main class works.
Third, we are ready to implement the Service class and the Camel Route consuming the method.
The Camel CDI detect Java Dependency Injection annotations
The camel route can use the service either making direct reference in the route or using the @Inject annotation.
Below route declare undertow as a server providing service in the port 8080 for all host's IPs available, and using the bean annotated before (bean:userService), all of this perfectly documented with swagger.
Fourth, time to package and generate the docker image.
I am conscious can be opinionated but I choose not to create a fat (uber) jar, instead of that I am using maven-dependency-plugin and manipulating the generation of the jar manifest.
Generating the Docker Image
Yes, like many others I used docker-maven-plugin with OpenJDK:8u121-alpine docker image.
To build the image please execute the following command line:
Fifth, you can start and test the docker image.
Finally, you can find some full examples at my GitHub account https://github.com/Emmerson-Miranda/camel/tree/master/camel3-cdi
Create microservices with Camel 3 is a straightforward process, and meet the requirement for those people who claim dependency injection features as a part of the development process.
I have to highlight other projects around Camel, like CamelK (Camel serverless on top of Kubernetes) and Quarkus (combined with GraalVM can create native executable services similar to golang).
Thanks for spending your time reading this.
Opinions expressed by DZone contributors are their own.