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.
We used the following items for this post.
- IntelliJ IDEA
- Micronaut 2.4.1
- Micronaut project with Consul Service Discovery feature
- JDK 14
- 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.
docker run -p 8500:8500 consul
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.
Next, click the copy icon to duplicate the default Application Run Configuration.
Create a few new Run Configurations and name them accordingly.
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.