DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • Micro Frontends Architecture
  • Unveiling the Application Modernization Roadmap: A Swift and Secure Journey to the Cloud
  • Scaling Micro-Frontends With Orchestrators

Trending

  • How to Perform Custom Error Handling With ANTLR
  • Data Quality: A Novel Perspective for 2025
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Building Micronaut Microservices Using MicrostarterCLI

Building Micronaut Microservices Using MicrostarterCLI

This article demonstrates building a simple Micronaut microservices application using MicrostarterCLI.

By 
Ahmed Al-Hashmi user avatar
Ahmed Al-Hashmi
·
Feb. 28, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.6K Views

Join the DZone community and get the full member experience.

Join For Free

MicrostarterCLI is a rapid development tool. It helps you as a developer generate standard reusable codes, configurations, or patterns you need in your application. In a previous article, I went through a basic example of creating REST and GraphQL endpoints in a Micronaut application.

This article demonstrates an example of bootstrapping a Micronaut microservices application using MicrostarterCLI. The application's architecture consists of the following:

  1. Fruit Service: A simple CRUD service for Fruit objects.
  2. Vegetable Service: A simple CRUD service for Vegetable objects.
  3. Eureka Service Discovery Server
  4. Consul Configuration Server. 
  5. Spring Cloud Gateway.

bootstrapping a Micronaut microservices application using MicrostarterCLI

Set Up the Environment

Download the MicrostarterCLI 2.5.0 or higher zip file. Then, unzip it and configure the folder in the environment variables. Then, you will be able to access the  mc.jar, mc.bat, and mc from any folder in the operating system.

To verify the configuration, check the Microstarter CLI by running the below command from the command prompt: 

PowerShell
 
mc --version


My environment details are as follows:

Operating System Windows 11
Java Version 11
IDE IntelliJ

Let's Start Development

Step 0: Create a Workspace Directory

Create a folder in your system in which you will save the projects. This step is optional, but it's helpful to organize the work. In this article, the workspace is c:\workspace. 

Step 1: Create Fruit Service 

The FruitService is a simple service with all CRUD operations to handle the Fruits objects. For simplicity, we will use the H2 database. We will use the MicrostarterCLI to generate all CRUD operation code and configuration as following steps: 

First, generate a Micronaut application from the Micronaut Launch and extract the project zip file in the c:\workspace folder. Alternatively, we can use the init Command of the MicrostarterCLI to generate the project directly from the Micronaut launch as follows:

PowerShell
 
mc init --name FruitService --package io.hashimati


After running the init command, go to the FruitService directory. Then, run the entity command to add the required dependencies, configure the service, and generate the necessary CRUD services code.   

PowerShell
 
cd fruit
mc entity -e Fruit


Once you run the command, the MicrostarterCLI will start with the configuration question.
Answer the configuration question as follows.

Option` Answer Description
Is the application monolithic? no To specify if the service is monolithic or microservice

Enter the server port number between 0 - 65535
-1 To set the server port. This service will use a random port number to run the services.
Enter the service id: fruit-service To set the service ID.
Select Reactive Framework reactor To user reactor framework in case the developer wants to user reactive data access.
Do you want to use Lombok? yes To use the Lombok library to generate the entity class.
Select Annotation: Micronaut Micronaut enables the developer to use Micronaut, JAX-RS, or Spring. This step instructs the MicrostarterCLI to use Micronaut annotation in generating the code.
Enter the database name: fruits to set the database name.
Select Database Type:
H2
to specify the database management engine.
Select Database Backend: JDBC To specify the database access
Select Data Migration: liquibase To use liquibase as a data migration tool to create the database schema.
Select Messaging type: none
Do you want to add cache-caffeine support? yes To use cache
Do you want to add Micrometer feature? yes To collect metrics on the endpoints and service calls.
Select Distributed Tracing: Jaeger To use Jaeger for distributed tracing.
Do you want to add GraphQL-Java-Tools support? yes To add GraphQL dependency to the project.
Do you want to add GRPC support? no If the answer is "yes," MicrostarterCli will prepare the project to be ready for the GRPC services. In this example, will not user GRPC,
Do you want to use File Services? no This article will not use storage services.
Do you want to configure VIEWS? yes To confirm if you want to add a "view" dependency.
Select the views configuration  views-thymeleaf To add Thymeleaf dependency.

Complete Configuration

Once you complete the configuration, the MicrostarterCLI will ask you to enter the collection's name or the table. Then, it will prompt you to enter the attribute. Enter the attributes as follows:                                                                                            

Attribute Type validation FindBy()Method FindAllBy() method UpdateBy() Method
Name String - Yes Yes No
quantity Int - No Yes No


Step 1 Output

By the end of the step, the MicrostarterCLI will generate the classes of the entity, service, controller, client, test controllers, liquibase xml files, and configurations.  

Step 2: Create Vegetable Service

The vegetable service will host the CRUD service of the Vegetable objects. To define it, we will repeat the same steps of Step 1 as follows: 

Repeat the same steps as step 1

Step 3: Create Eureka Server

In this step, we will create the Eureka service discovery server. The service will be listening on port 8761. The Fruit and Vegetable services will register in the Eureka server as we point their Eureka client to localhost and port 8761. To create the Eureka server project using MicorstarterCLI, run the below command from the c:\workspace

Shell
 
mc eureka -version 2.7.8 --javaVersion 11


create the Eureka service discovery server

Step 4: Create a Gateway Service

The last component we will create is Spring Cloud Gateway. The gateway service will listen on port 8080. To generate the Gateway project using the MicrostarterCLI, run the gateway command below: 

Shell
 
mc gateway -version 2.7.8 --javaVersion 11


create is Spring Cloud Gateway

Step 5: Gateway/Microservice Routes Configurations

In this step, we will configure the root routes for both Fruit and Vegetable APIs. As generated by the MIcrostarterCLI, the root route for the Fruit API, as in @Controller annotation of io.hashimati.controllers.FruitController a class of the Fruit Service. The Vegetable API's root route is /api/v1/vegetable as in the io.hashimati.controllers.VegetableController class of the Vegetable Service. To register the routes, we will use the register subcommand of the gateway command. To run the command go to c:\workspace\gateway and run the below command:

 
mc gateway register


When the command run, the MicrostarterCLI will prompt you to enter the Service ID, Service Name, and the routes. Run the register subcommand twice to configure the root routes for the Fruit and the Vegetable APIs. 

Run the register subcommand twice to configure the root routes for the Fruit and the Vegetable APIs

You configured the CRUD endpoints for the Fruit and Vegetable server from the Gateway by completing this step

You configured the CRUD endpoints for the Fruit and Vegetable server from the Gateway by completing this step. 

Run and Try 

To run the service, we will create a run.bat file to launch all the services as below: 

 
cd c:\workspace\eureka\
start gradlew bootRun -x test&

cd c:\workspace\FruitService
start gradlew run -x test&

cd c:\workspace\VegetableService
start gradlew run -x test&


cd c:\workspace\gateway
start gradlew bootRun -x test&


After running the run.bat file, all services start and run. wait until all the services complete their start-up process. Open this URL. You should see all services registered on the Eureka server.

You should see all services registered on the Eureka server.

To try the CRUD services, you can use the .http file of IntelliJ. We will create test.http file as follows:

HTTP
 
POST http://localhost:8080/api/v1/fruit/save
Content-Type: application/json

{
    "name": "Apple",
    "quantity": 100
}

###
GET http://localhost:8080/api/v1/fruit/findAll

###
POST http://localhost:8080/api/v1/vegetable/save
Content-Type: application/json

{
    "name": "Onion",
    "quantity": 100
}

###
GET http://localhost:8080/api/v1/vegetable/findAll


By running from IntelliJ, It works!

Conclusion

Using MicostarterCLI, you can generate the configuration of the needed component for Microservice Archtication like JWT security, distributed tracing, messaging, and observability. MicrostarterCLI also supports Groovy and Kotlin programming languages. Please visit the project repository for more information.

Check out the article example here.

Happy coding!

application Fruit (software) microservice intellij

Opinions expressed by DZone contributors are their own.

Related

  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • Micro Frontends Architecture
  • Unveiling the Application Modernization Roadmap: A Swift and Secure Journey to the Cloud
  • Scaling Micro-Frontends With Orchestrators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!