Do we build a system as microservice or monolith? The debate always comes up when we are building a system from scratch. Microservices aims to separate application into independently deployable unit of services but it is harder to develop. Monolith is build as a single unit but can quickly turn into a big ball of mud, if not developed carefully. There is another option where you can develop your monoliths in a modularized way which can help keep your architecture under control.
Creation a modularized monolith in Helidon is effortless. You should have a working Helidon MP project. Create a Helidon project by browsing to Helidon MP Quickstart and following the instructions.
Example
This section describes the implementation of a REST application which returns a fibonacci sequence of first to x. It has been split into 2 modules once which generates the sequence and the other, uses the service and consume it.
Firstly, create a directory multi-module and then cd into the newly created multi-module directory. Open a command prompt and then type the following command
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=io.helidon.archetypes -DarchetypeArtifactId=helidon-quickstart-mp -DarchetypeVersion=2.3.3 -DgroupId=com.examples.helidon -DartifactId=fibonacci-module -Dpackage=com.examples.helidon.multimodule.service
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=io.helidon.archetypes -DarchetypeArtifactId=helidon-quickstart-mp -DarchetypeVersion=2.3.3 -DgroupId=com.examples.helidon -DartifactId=resource-module -Dpackage=com.examples.helidon.multimodule.resource
Project Dependencies
To build Helidon applications in modularized monolith, simply run the following Maven archetype to create the parent project
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE -DgroupId=com.examples.helidon -DartifactId=multi-module -Dversion=1.0-SNAPSHOT -DinteractiveMode=false
Open pom.xml for multi-module parent add the following modules
<modules>
<module>fibonacci-module</module>
<module>resource-module</module>
</modules>
And open pom.xml for each module and modify the name to follow artifactid
example <name>resource-module</name>
Now while inside the parent folder multi-module, run the following command to build the service using a command prompt
mvn clean install
To build the fibonacci service, go to fibonacci-module and remove the GreetingProvider and GreetingResource and test files namely MainTest
And create FibonacciProvider file under src/main of fibonacci-module as below
Add the fibonacci-module dependency in resource-module’s pom.xml as below
Go to resource-module and remove the GreetingProvider and GreetingResource and test files namely MainTest
Create a file named AppResource under src/main of resource-module and write the code as depicted below
If you see in the file above, we are importing FibonacciProvider from fibonacci-module and using it to serve api /app/fibonacci/
Run the following command using command prompt from the root directory of the project
mvn clean install -DskipTests
Running the application
After completion of above task, run the application and serve RESTful API by typing the following command
java -jar resource-module\target\resource-module.jar
We can send HTTP GET request from browser or any HTTP client as follows:
http://localhost:8080/app/fibonacci/12
