Docker, Micronaut, Microservices, Software Development

Run Multiple Micronaut Applications In IntelliJ Without Docker

Micronaut

Working with Microservices sometimes involves testing multiple instances of an application running on different port numbers. At times, we’d prefer to spawn those instances straight from our IDE, e.g., IntelliJ. For example, we’d like to figure out an ideal configuration for client-side load balance for our Micronaut application in QA environments. This post shows how to run multiple Micronaut applications in different port numbers from IntelliJ without using Docker.

This may also apply to other frameworks, e.g., Spring Boot, used in IntelliJ IDEA. However, the property to pass as VM option may be different.

Requisites

We used the following items for this post.

  1. IntelliJ IDEA
  2. Micronaut 2.4.1
  3. Micronaut project with Consul Service Discovery feature
  4. JDK 14
  5. Docker to run an instance of Consul

Create A Micronaut Application, Yet Again

We need an application to create multiple instances for this post. Open IntelliJ IDEA and generate a Micronaut application with Consul Service Discovery feature. First, fill in the Group and Artifact fields, as shown below.

 

Next, choose Consul Service Discovery.

The Consul Service Discovery feature allows our Micronaut application instances to register themselves with Consul. Consequently, these instances become discoverable to other applications. Although we could do without the Service Discovery, it is better to show things within Microservices’ context. Therefore, we need a running instance of Consul for a proper demonstration.

Our Micronaut application will have the following application.yml file.

Startup Consul For Service Registration And Discovery

At this point, we have not yet started any instances of our Micronaut application. Next, start up Consul for the first time using the following command.

Create Multiple Run Configurations For the Micronaut Application

When we generate a Micronaut application in IntelliJ IDEA, we get a default Application Run Configuration, along with source code and other configuration files. To run multiple instances of the Micronaut application, we need to create a few Run Configurations. We could base these on the default Application Run Configuration. To proceed, click Edit Configurations..., as shown below.

Run Multiple Micronaut Applications

 

Next, click the copy icon to duplicate the default Application Run Configuration.

Run Multiple Micronaut Applications

 

Create a few new Run Configurations and name them accordingly.

Run Multiple Micronaut Applications

Then, add the following VM option to all the new Run Configurations and set the PORT value accordingly. We need not configure the Application Run Configuration because the Micronaut application starts up at port 8080 by default.

For example, -Dmicronaut.server.port=8081 for Application at 8081, -Dmicronaut.server.port=8082 for Application at 8082, and so on.

Run Multiple Instances Of Micronaut Applications Using Different Run Configurations

Finally, we can start up multiple instances of our Micronaut application. Run each instance by choosing each Run Configuration and clicking the Play button, as shown below.

 

Then, we can check on the Consul Dashboard to see the instances of our Micronaut application.

 

Consul indicates that the runmultipleinstanceinintellij service has four instances.

That is all! We hope this post has been helpful.

You Might Also Like